@@ -12,13 +12,6 @@ const apiRoutes = ['/api', '/docs']
12
12
// Function to check if a route is an API route
13
13
const isApiRoute = ( path : string ) => apiRoutes . some ( ( route ) => path . startsWith ( route ) )
14
14
15
- // Function to handle API route navigation
16
- const handleApiNavigation = ( path : string ) => {
17
- if ( typeof window !== 'undefined' ) {
18
- window . location . replace ( path )
19
- }
20
- }
21
-
22
15
export const createApp = ViteSSG (
23
16
App ,
24
17
{
@@ -37,52 +30,55 @@ export const createApp = ViteSSG(
37
30
const { router, isClient } = ctx
38
31
39
32
if ( isClient ) {
40
- console . log ( 'here11 ' )
33
+ console . log ( 'Client-side code running - v1 ' )
41
34
42
- // Handle initial load and manual URL changes
43
- const handleRouteChange = ( ) => {
44
- if ( isApiRoute ( window . location . pathname ) ) {
45
- console . log ( 'here2' )
46
- handleApiNavigation ( window . location . pathname )
47
- }
35
+ // Handle API routes
36
+ const handleApiRoute = ( path : string ) => {
37
+ console . log ( 'Handling API route:' , path )
38
+ window . location . replace ( path )
48
39
}
49
40
50
41
// Check on initial load
51
- handleRouteChange ( )
52
-
53
- // Listen for popstate events (manual URL changes )
54
- window . addEventListener ( 'popstate' , handleRouteChange )
42
+ if ( isApiRoute ( window . location . pathname ) ) {
43
+ console . log ( 'Initial load is API route' )
44
+ handleApiRoute ( window . location . pathname )
45
+ }
55
46
56
- // Modify how links are handled (only in the browser)
47
+ // Intercept link clicks
57
48
document . addEventListener (
58
49
'click' ,
59
50
( event ) => {
60
- console . log ( 'here3' )
61
51
const target = event . target as HTMLAnchorElement
62
- if ( target . tagName === 'A' && isApiRoute ( target . pathname ) ) {
52
+ if ( target . tagName === 'A' && isApiRoute ( new URL ( target . href ) . pathname ) ) {
53
+ console . log ( 'Clicked API route link:' , target . href )
63
54
event . preventDefault ( )
64
- handleApiNavigation ( target . href )
55
+ handleApiRoute ( target . href )
65
56
}
66
57
} ,
67
58
true ,
68
59
)
60
+
61
+ // Handle popstate events (browser back/forward)
62
+ window . addEventListener ( 'popstate' , ( ) => {
63
+ if ( isApiRoute ( window . location . pathname ) ) {
64
+ console . log ( 'Popstate detected API route' )
65
+ handleApiRoute ( window . location . pathname )
66
+ }
67
+ } )
69
68
}
70
69
71
- // Catch any navigation to API routes
70
+ // Router guard for API routes
72
71
router . beforeEach ( ( to , from , next ) => {
73
- console . log ( 'here4' )
72
+ console . log ( 'Router guard triggered for:' , to . fullPath )
74
73
if ( isApiRoute ( to . fullPath ) ) {
75
- console . log ( 'here5' )
76
74
if ( isClient ) {
77
- console . log ( 'here6' )
78
- handleApiNavigation ( to . fullPath )
75
+ console . log ( 'Router guard redirecting to API route:' , to . fullPath )
76
+ window . location . replace ( to . fullPath )
79
77
return false
80
78
}
81
- // For SSR, we'll let the server handle these routes
82
- console . log ( 'here7' )
79
+ // For SSR, let the server handle it
83
80
return next ( )
84
81
}
85
- console . log ( 'here8' )
86
82
next ( )
87
83
} )
88
84
} ,
0 commit comments