yufan.me/src/middleware.ts

17 lines
708 B
TypeScript
Raw Normal View History

2024-06-14 02:11:26 +08:00
import { posts } from '@/helpers/schema';
import { urlJoin } from '@/helpers/tools';
import { defineMiddleware } from 'astro:middleware';
const mappings = new Map<string, string>(posts.map((post) => [urlJoin('/', post.slug), post.permalink]));
export const onRequest = defineMiddleware(({ request: { method }, url: { pathname }, redirect }, next) => {
// This is used for redirect my old blog posts to a new mapping.
const newTarget = mappings.get(pathname.endsWith('/') ? pathname.substring(0, pathname.length - 1) : pathname);
2024-06-14 02:11:26 +08:00
if (method === 'GET' && newTarget !== undefined) {
return redirect(newTarget, 301);
}
// return a Response or the result of calling `next()`
return next();
});