@@ -7,8 +7,17 @@ import '../../../../../resources/assets/styles/main.css'
7
7
import App from './App.vue'
8
8
import type { UserModule } from './types'
9
9
10
- // import Previewer from 'virtual:vue-component-preview'
11
- // const routes = setupLayouts(generatedRoutes)
10
+ const apiRoutes = [ '/api' , '/docs' ]
11
+
12
+ // Function to check if a route is an API route
13
+ const isApiRoute = ( path : string ) => apiRoutes . some ( ( route ) => path . startsWith ( route ) )
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
+ }
12
21
13
22
export const createApp = ViteSSG (
14
23
App ,
@@ -18,25 +27,62 @@ export const createApp = ViteSSG(
18
27
} ,
19
28
( ctx ) => {
20
29
// install all modules under `modules/`
21
- // Object.values(import.meta.glob<{ install: UserModule }>('../../../../../resources/modules/*.ts'))
22
- // .forEach(i => i.install?.(ctx))
23
30
; ( async ( ) => {
24
31
const modules = import . meta. glob < { install : UserModule } > ( '../../../../../resources/modules/*.ts' )
25
32
const promises = Object . values ( modules ) . map ( ( func ) => func ( ) )
26
33
const modulesArray = await Promise . all ( promises )
27
34
for ( const module of modulesArray ) module . install ?.( ctx )
28
35
} ) ( )
29
36
30
- // ctx.app.use(Previewer)
37
+ const { router , isClient } = ctx
31
38
32
- const { router } = ctx
33
- router . beforeEach ( ( to , from , next ) => {
34
- if ( to . fullPath . startsWith ( '/api' ) || to . fullPath . startsWith ( '/docs' ) ) {
35
- console . log ( 'redirecting to' , to . fullPath )
36
- window . location . href = to . fullPath
37
- return
39
+ if ( isClient ) {
40
+ console . log ( 'here11' )
41
+
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
+ }
38
48
}
39
49
50
+ // Check on initial load
51
+ handleRouteChange ( )
52
+
53
+ // Listen for popstate events (manual URL changes)
54
+ window . addEventListener ( 'popstate' , handleRouteChange )
55
+
56
+ // Modify how links are handled (only in the browser)
57
+ document . addEventListener (
58
+ 'click' ,
59
+ ( event ) => {
60
+ console . log ( 'here3' )
61
+ const target = event . target as HTMLAnchorElement
62
+ if ( target . tagName === 'A' && isApiRoute ( target . pathname ) ) {
63
+ event . preventDefault ( )
64
+ handleApiNavigation ( target . href )
65
+ }
66
+ } ,
67
+ true ,
68
+ )
69
+ }
70
+
71
+ // Catch any navigation to API routes
72
+ router . beforeEach ( ( to , from , next ) => {
73
+ console . log ( 'here4' )
74
+ if ( isApiRoute ( to . fullPath ) ) {
75
+ console . log ( 'here5' )
76
+ if ( isClient ) {
77
+ console . log ( 'here6' )
78
+ handleApiNavigation ( to . fullPath )
79
+ return false
80
+ }
81
+ // For SSR, we'll let the server handle these routes
82
+ console . log ( 'here7' )
83
+ return next ( )
84
+ }
85
+ console . log ( 'here8' )
40
86
next ( )
41
87
} )
42
88
} ,
0 commit comments