managarten/memoro/apps/landing/src/middleware.ts
Till-JS e7f5f942f3 chore: initial commit - consolidate 4 projects into monorepo
Projects included:
- maerchenzauber (NestJS backend + Expo mobile + SvelteKit web + Astro landing)
- manacore (Expo mobile + SvelteKit web + Astro landing)
- manadeck (NestJS backend + Expo mobile + SvelteKit web)
- memoro (Expo mobile + SvelteKit web + Astro landing)

This commit preserves the current state before monorepo restructuring.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-22 23:38:24 +01:00

40 lines
1.4 KiB
TypeScript

import { defineMiddleware } from 'astro/middleware';
import { getRedirectUrl, getRedirectStatus } from './utils/redirects';
export const onRequest = defineMiddleware(async (context, next) => {
const { url } = context;
const { pathname } = url;
// 1. Prüfe zuerst die Redirect-Map für alte URLs
const redirectUrl = getRedirectUrl(pathname);
if (redirectUrl) {
const status = getRedirectStatus(pathname);
return context.redirect(redirectUrl, status);
}
// 2. Handle root path - redirect to /de
if (pathname === '/') {
return context.redirect('/de', 301);
}
// 3. Handle paths without language prefix (except special paths)
const segments = pathname.split('/').filter(Boolean);
const firstSegment = segments[0];
const validLanguages = ['de', 'en'];
const specialPaths = ['admin', 'api', '_astro', 'images', 'favicon', '404'];
// If no language prefix and not a special path, redirect to /de/...
if (!validLanguages.includes(firstSegment) && !specialPaths.includes(firstSegment) && !pathname.includes('.')) {
return context.redirect(`/de${pathname}`, 301);
}
// 4. Lasse alle anderen Requests durch
const response = await next();
// 5. Log 404 errors für Monitoring (nur in Production)
if (response.status === 404 && import.meta.env.PROD) {
console.warn(`404 Error: ${pathname} | Referrer: ${context.request.headers.get('referer') || 'direct'}`);
}
return response;
});