personal-website/src/middleware.js
Cyril Šebek 7a71a66dc7
Enabled SSR with pre-renders
- Enable server output mode
- Pre-render for (almost) every .astro file
- Fixed custom middleware
2024-07-08 23:01:44 +02:00

32 lines
898 B
JavaScript

import { defineMiddleware, sequence } from "astro:middleware";
import { middleware } from "astro:i18n"; // Astro's own i18n routing config
export const prerender = false
const langs = ["en", "de", "cs", "fr"]
export const userMiddleware = defineMiddleware((ctx, next) => {
const exceptions = ["/api", "/public"]
// If path is in exceptions (i.e. api or public) return default middleware to handle it
if (exceptions.includes(ctx.url.pathname) ) {
return next();
// If no lang is set => set to english (some pages are language-les exceptions)
} else if ( !langs.some(item => ctx.url.href.includes(item)) ) {
return Response.redirect(`${ctx.url.protocol}${ctx.url.host}/en${ctx.url.pathname}`);
} else {
return next();
}
});
export const onRequest = sequence(
userMiddleware,
middleware({
redirectToDefaultLocale: false,
prefixDefaultLocale: true,
})
);