corrad-af-2024/server/middleware/spa-fallback.js

38 lines
931 B
JavaScript

export default defineEventHandler(async (event) => {
// Only handle GET requests
if (getMethod(event) !== 'GET') {
return
}
const url = getRequestURL(event)
const path = url.pathname
// Skip API routes and static assets
if (
path.startsWith('/api/') ||
path.startsWith('/_nuxt/') ||
path.startsWith('/icons/') ||
path.startsWith('/img/') ||
path.startsWith('/uploads/') ||
path.includes('.') // Files with extensions (css, js, images, etc.)
) {
return
}
// For client-side routes that don't exist as files, serve the main app
// This ensures routes like /api-platform work on refresh
const clientRoutes = [
'/api-platform',
'/dashboard',
'/devtool'
]
const isClientRoute = clientRoutes.some(route =>
path === route || path.startsWith(route + '/')
)
if (isClientRoute) {
// Let Nuxt handle the routing client-side
return
}
})