26 lines
823 B
TypeScript
26 lines
823 B
TypeScript
import { defineMiddleware } from 'astro:middleware';
|
|
import { SUPPORTED_LANGUAGES, DEFAULT_LANGUAGE, isSupportedLanguage } from './lib/i18n';
|
|
|
|
export const onRequest = defineMiddleware(async (context, next) => {
|
|
const { pathname } = context.url;
|
|
|
|
// Skip middleware for static assets
|
|
if (pathname.startsWith('/_astro/') || pathname.startsWith('/assets/') || pathname.includes('.')) {
|
|
return next();
|
|
}
|
|
|
|
const pathSegments = pathname.split('/').filter(Boolean);
|
|
const firstSegment = pathSegments[0];
|
|
|
|
// If the first segment is a supported language, continue
|
|
if (isSupportedLanguage(firstSegment)) {
|
|
return next();
|
|
}
|
|
|
|
// If no language prefix, it's Spanish (default) - continue
|
|
if (pathname === '/' || !isSupportedLanguage(firstSegment)) {
|
|
return next();
|
|
}
|
|
|
|
return next();
|
|
});
|