npm i -D @netlify/edge-functions
netlify/edge-functions/legacy-category-redirect.ts
import type { Config } from '@netlify/edge-functions';
const MAP: Record<string, string> = {
'/hello-world': '/category/hello-world',
'/sample-post': '/category/sample-post',
};
export default async (request: Request, context: { next: () => Promise<Response> }) => {
const url = new URL(request.url);
// /something/ -> /somethin
const path = url.pathname.replace(/\/+$/, '') || '/';
const target = MAP[path];
if (!target) {
return context.next();
}
url.pathname = target;
return new Response(null, {
status: 301,
headers: {
location: url.toString(),
'x-legacy-redirect': '1', // debug
},
});
};
export const config = {
path: [
'/hello-world',
'/sample-post',
],
};