chore: better 301 redirect for old posts.

This commit is contained in:
Yufan Sheng 2024-06-17 04:56:49 +08:00
parent d78b1f51be
commit de93b65386
Signed by: syhily
GPG Key ID: DEB186763C308C31

View File

@ -6,7 +6,7 @@ const mappings = new Map<string, string>(posts.map((post) => [urlJoin('/', post.
export const onRequest = defineMiddleware(({ request: { method }, url: { pathname }, redirect }, next) => { export const onRequest = defineMiddleware(({ request: { method }, url: { pathname }, redirect }, next) => {
// This is used for redirect my old blog posts to a new mapping. // This is used for redirect my old blog posts to a new mapping.
const newTarget = mappings.get(pathname); const newTarget = mappings.get(pathname.endsWith('/') ? pathname.substring(0, pathname.length - 1) : pathname);
if (method === 'GET' && newTarget !== undefined) { if (method === 'GET' && newTarget !== undefined) {
return redirect(newTarget, 301); return redirect(newTarget, 301);
} }