mirror of
https://github.com/cloudflare/vinext.git
synced 2026-09-14 19:04:59 +08:00
f0f3e35947
Plumb `trailingSlash` from the resolved next.config into NextURL via NextRequest, so `NextResponse.redirect(request.nextUrl)` (and any other stringification of a NextURL) emits a Location header that matches the user's slash policy. Mirrors Next.js's `formatNextPathnameInfo`. Pre-route trailing-slash normalization (e.g. `/foo` -> `/foo/` when trailingSlash:true) already runs at every entry point via `normalizeTrailingSlash` in request-pipeline. This commit closes the remaining gap by making redirect Locations produced from inside middleware (the common one-hop `request.nextUrl` redirect pattern in the Next.js middleware-trailing-slash fixture) carry the slash too. Refs #1332. Sub-problem 3 (shallow navigation triggering a server roundtrip) is a separate Pages Router client-side concern and deferred as follow-up.
101 lines
4.1 KiB
TypeScript
101 lines
4.1 KiB
TypeScript
import { describe, expect, it } from "vite-plus/test";
|
|
import { executeMiddleware } from "../packages/vinext/src/server/middleware-runtime.js";
|
|
import { NextResponse } from "../packages/vinext/src/shims/server.js";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Regression for issue #1332 sub-problem 1: middleware redirect Location
|
|
// headers must honour `trailingSlash` from next.config.js.
|
|
//
|
|
// When a middleware does `NextResponse.redirect(request.nextUrl)` or
|
|
// `NextResponse.redirect(new URL('/x', req.url))`, the Location header should
|
|
// reflect the user's trailingSlash policy. Mirrors Next.js behaviour where
|
|
// NextURL.href is formatted via formatNextPathnameInfo at stringification time.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
describe("executeMiddleware propagates trailingSlash to NextURL", () => {
|
|
it("emits Location with trailing slash when middleware redirects via request.nextUrl (trailingSlash: true)", async () => {
|
|
const result = await executeMiddleware({
|
|
isProxy: false,
|
|
module: {
|
|
middleware: (request: { nextUrl: { clone(): unknown } }) => {
|
|
// Mirrors the Next.js fixture in test/e2e/middleware-trailing-slash:
|
|
// mutate the URL and redirect to it.
|
|
const url = (request.nextUrl as unknown as URL & { clone(): URL }).clone();
|
|
url.pathname = "/somewhere";
|
|
return NextResponse.redirect(url as unknown as URL);
|
|
},
|
|
},
|
|
request: new Request("http://localhost/redirect-to-somewhere/"),
|
|
trailingSlash: true,
|
|
});
|
|
|
|
expect(result.continue).toBe(false);
|
|
expect(result.redirectStatus).toBe(307);
|
|
expect(result.redirectUrl).not.toBeUndefined();
|
|
expect(new URL(result.redirectUrl!, "http://localhost").pathname).toBe("/somewhere/");
|
|
});
|
|
|
|
it("emits Location without trailing slash when trailingSlash: false", async () => {
|
|
const result = await executeMiddleware({
|
|
isProxy: false,
|
|
module: {
|
|
middleware: (request: { nextUrl: { clone(): unknown } }) => {
|
|
const url = (request.nextUrl as unknown as URL & { clone(): URL }).clone();
|
|
url.pathname = "/somewhere/";
|
|
return NextResponse.redirect(url as unknown as URL);
|
|
},
|
|
},
|
|
request: new Request("http://localhost/redirect-to-somewhere"),
|
|
trailingSlash: false,
|
|
});
|
|
|
|
expect(result.continue).toBe(false);
|
|
expect(result.redirectUrl).not.toBeUndefined();
|
|
expect(new URL(result.redirectUrl!, "http://localhost").pathname).toBe("/somewhere");
|
|
});
|
|
|
|
it("does not touch the root path regardless of trailingSlash", async () => {
|
|
const result = await executeMiddleware({
|
|
isProxy: false,
|
|
module: {
|
|
middleware: (request: { nextUrl: { clone(): unknown } }) => {
|
|
const url = (request.nextUrl as unknown as URL & { clone(): URL }).clone();
|
|
url.pathname = "/";
|
|
return NextResponse.redirect(url as unknown as URL);
|
|
},
|
|
},
|
|
request: new Request("http://localhost/somewhere"),
|
|
trailingSlash: true,
|
|
});
|
|
|
|
expect(result.redirectUrl).not.toBeUndefined();
|
|
expect(new URL(result.redirectUrl!, "http://localhost").pathname).toBe("/");
|
|
});
|
|
|
|
it("preserves search params when applying trailingSlash to redirect Location", async () => {
|
|
const result = await executeMiddleware({
|
|
isProxy: false,
|
|
module: {
|
|
middleware: (request: { nextUrl: { clone(): unknown } }) => {
|
|
const url = (
|
|
request.nextUrl as unknown as URL & {
|
|
clone(): URL;
|
|
searchParams: URLSearchParams;
|
|
}
|
|
).clone();
|
|
url.pathname = "/dest";
|
|
url.searchParams.set("foo", "bar");
|
|
return NextResponse.redirect(url as unknown as URL);
|
|
},
|
|
},
|
|
request: new Request("http://localhost/src"),
|
|
trailingSlash: true,
|
|
});
|
|
|
|
expect(result.redirectUrl).not.toBeUndefined();
|
|
const loc = new URL(result.redirectUrl!, "http://localhost");
|
|
expect(loc.pathname).toBe("/dest/");
|
|
expect(loc.searchParams.get("foo")).toBe("bar");
|
|
});
|
|
});
|