Skip to content

Commit 1315a37

Browse files
committed
chore: wip
1 parent 2ec93fd commit 1315a37

File tree

1 file changed

+26
-30
lines changed
  • storage/framework/views/web/src

1 file changed

+26
-30
lines changed

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

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ const apiRoutes = ['/api', '/docs']
1212
// Function to check if a route is an API route
1313
const isApiRoute = (path: string) => apiRoutes.some((route) => path.startsWith(route))
1414

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-
2215
export const createApp = ViteSSG(
2316
App,
2417
{
@@ -37,52 +30,55 @@ export const createApp = ViteSSG(
3730
const { router, isClient } = ctx
3831

3932
if (isClient) {
40-
console.log('here11')
33+
console.log('Client-side code running - v1')
4134

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)
4839
}
4940

5041
// 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+
}
5546

56-
// Modify how links are handled (only in the browser)
47+
// Intercept link clicks
5748
document.addEventListener(
5849
'click',
5950
(event) => {
60-
console.log('here3')
6151
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)
6354
event.preventDefault()
64-
handleApiNavigation(target.href)
55+
handleApiRoute(target.href)
6556
}
6657
},
6758
true,
6859
)
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+
})
6968
}
7069

71-
// Catch any navigation to API routes
70+
// Router guard for API routes
7271
router.beforeEach((to, from, next) => {
73-
console.log('here4')
72+
console.log('Router guard triggered for:', to.fullPath)
7473
if (isApiRoute(to.fullPath)) {
75-
console.log('here5')
7674
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)
7977
return false
8078
}
81-
// For SSR, we'll let the server handle these routes
82-
console.log('here7')
79+
// For SSR, let the server handle it
8380
return next()
8481
}
85-
console.log('here8')
8682
next()
8783
})
8884
},

0 commit comments

Comments
 (0)