Skip to content

Commit 2ec93fd

Browse files
committed
chore: wip
1 parent 2e0e01c commit 2ec93fd

File tree

1 file changed

+57
-11
lines changed
  • storage/framework/views/web/src

1 file changed

+57
-11
lines changed

storage/framework/views/web/src/main.ts

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@ import '../../../../../resources/assets/styles/main.css'
77
import App from './App.vue'
88
import type { UserModule } from './types'
99

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+
}
1221

1322
export const createApp = ViteSSG(
1423
App,
@@ -18,25 +27,62 @@ export const createApp = ViteSSG(
1827
},
1928
(ctx) => {
2029
// install all modules under `modules/`
21-
// Object.values(import.meta.glob<{ install: UserModule }>('../../../../../resources/modules/*.ts'))
22-
// .forEach(i => i.install?.(ctx))
2330
;(async () => {
2431
const modules = import.meta.glob<{ install: UserModule }>('../../../../../resources/modules/*.ts')
2532
const promises = Object.values(modules).map((func) => func())
2633
const modulesArray = await Promise.all(promises)
2734
for (const module of modulesArray) module.install?.(ctx)
2835
})()
2936

30-
// ctx.app.use(Previewer)
37+
const { router, isClient } = ctx
3138

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+
}
3848
}
3949

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')
4086
next()
4187
})
4288
},

0 commit comments

Comments
 (0)