corrad-bp/plugins/route-guard.js
2025-06-16 00:52:49 +08:00

28 lines
983 B
JavaScript

export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook('app:created', () => {
// Check if we're on the client side
if (process.client) {
const router = useRouter();
// Add global navigation guard
router.beforeEach((to, from, next) => {
// Check if the route path contains literal square brackets
// This indicates a user is trying to navigate to a route with [param] directly
if (to.fullPath.includes('/[') || to.fullPath.includes(']')) {
console.warn('Invalid route detected with literal brackets:', to.fullPath);
// Extract the route pattern without the brackets
const baseRoute = to.fullPath.split('/').slice(0, -1).join('/');
// Redirect to a more appropriate page
return next({
path: baseRoute || '/',
query: { error: 'invalid_route_format' }
});
}
next();
});
}
});
});