Files
codey.lol/src/middleware.js

20 lines
597 B
JavaScript
Raw Normal View History

import { defineMiddleware } from 'astro:middleware';
export const onRequest = defineMiddleware(async (context, next) => {
try {
// Let Astro handle the request first
const response = await next();
// If it's a 404, redirect to home
if (response.status === 404) {
console.log(`404 redirect: ${context.url.pathname} -> /`);
return context.redirect('/', 302);
}
return response;
} catch (error) {
// Handle any middleware errors by redirecting to home
console.error('Middleware error:', error);
return context.redirect('/', 302);
}
});