feat: add trailingSlash middleware
This commit is contained in:
@@ -22,8 +22,9 @@ export default async (req: Request) => {
|
||||
data.shift();
|
||||
}
|
||||
data.push(chunk.toString());
|
||||
|
||||
const re = new RegExp(`"${body.pathname}":"([^"]+)"`);
|
||||
// Since we strip trailing slash (in the trailingSlash middleware) before entering this middleware,
|
||||
// we need check matching paths both including and excluding trailing slash.
|
||||
const re = new RegExp(`"${body.pathname}\/?":"([^"]+)"`);
|
||||
|
||||
const match = data.join("").match(re);
|
||||
if (match?.[1]) {
|
||||
@@ -46,10 +47,14 @@ export default async (req: Request) => {
|
||||
);
|
||||
|
||||
if (redirectUrl) {
|
||||
if (redirectUrl === body.pathname) {
|
||||
// Make sure to exclude trailing slash in the redirectUrl to avoid an extra middleware roundtrip
|
||||
const redirectUrlWithoutTrailingSlash = redirectUrl.endsWith("/")
|
||||
? redirectUrl.slice(0, -1)
|
||||
: redirectUrl;
|
||||
if (redirectUrlWithoutTrailingSlash === body.pathname) {
|
||||
return new Response("Not Found", { status: 404 });
|
||||
}
|
||||
return new Response(redirectUrl);
|
||||
return new Response(redirectUrlWithoutTrailingSlash);
|
||||
}
|
||||
}
|
||||
return new Response("Not Found", { status: 404 });
|
||||
|
||||
Reference in New Issue
Block a user