Skip to content

Commit

Permalink
Merge pull request #287 from blz-it/fix-links
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasWanke authored Oct 14, 2024
2 parents b987f2c + 250cdaf commit 96e1fd7
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ export const onRequest = defineMiddleware(async (ctx, next) => {
}

const [, lang] = ctx.url.pathname.split("/");
if (!(lang in languages)) {
return Response.redirect(
new URL(`/${defaultLang}${ctx.url.pathname}`, ctx.url),
301,
);
}
const isValidLanguage = lang in languages;

const response = await next();
const response = await next(
isValidLanguage
? undefined
: new URL(`/${defaultLang}${ctx.url.pathname}`, ctx.url),
);

if (response.status === 404) {
return next(new URL(`/${lang}/404`, ctx.url));
return next(
new URL(`/${isValidLanguage ? lang : defaultLang}/404`, ctx.url),
);
}

return response;
Expand Down

0 comments on commit 96e1fd7

Please sign in to comment.