Skip to content

Commit 9e9e135

Browse files
authored
fix: pass through headers on middleware redirect (#273)
1 parent 48c556d commit 9e9e135

File tree

6 files changed

+43
-27
lines changed

6 files changed

+43
-27
lines changed

examples/app-pages-router/middleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export function middleware(request: NextRequest) {
77
const protocol = host?.startsWith("localhost") ? "http" : "https";
88
if (path === "/redirect") {
99
const u = new URL("/redirect-destination", `${protocol}://${host}`);
10-
return NextResponse.redirect(u);
10+
return NextResponse.redirect(u, {
11+
headers: { "set-cookie": "test=success" },
12+
});
1113
} else if (path === "/rewrite") {
1214
const u = new URL("/rewrite-destination", `${protocol}://${host}`);
1315
return NextResponse.rewrite(u);

examples/app-router/middleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export function middleware(request: NextRequest) {
77
const protocol = host?.startsWith("localhost") ? "http" : "https";
88
if (path === "/redirect") {
99
const u = new URL("/redirect-destination", `${protocol}://${host}`);
10-
return NextResponse.redirect(u);
10+
return NextResponse.redirect(u, {
11+
headers: { "set-cookie": "test=success" },
12+
});
1113
} else if (path === "/rewrite") {
1214
const u = new URL("/rewrite-destination", `${protocol}://${host}`);
1315
return NextResponse.rewrite(u);

packages/open-next/src/adapters/routing/middleware.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,6 @@ export async function handleMiddleware(
8686
onWarning: console.warn,
8787
});
8888
res.statusCode = result.response.status;
89-
// If the middleware returned a Redirect, we set the `Location` header with
90-
// the redirected url and end the response.
91-
if (res.statusCode >= 300 && res.statusCode < 400) {
92-
const location = result.response.headers
93-
.get("location")
94-
?.replace("http://localhost:3000", `https://${req.headers.host}`);
95-
// res.setHeader("Location", location);
96-
return {
97-
body: "",
98-
type: internalEvent.type,
99-
statusCode: res.statusCode,
100-
headers: {
101-
Location: location,
102-
},
103-
isBase64Encoded: false,
104-
};
105-
}
10689

10790
/* Apply override headers from middleware
10891
NextResponse.next({
@@ -133,6 +116,25 @@ export async function handleMiddleware(
133116
}
134117
});
135118

119+
// If the middleware returned a Redirect, we set the `Location` header with
120+
// the redirected url and end the response.
121+
if (res.statusCode >= 300 && res.statusCode < 400) {
122+
const location = result.response.headers
123+
.get("location")
124+
?.replace("http://localhost:3000", `https://${req.headers.host}`);
125+
// res.setHeader("Location", location);
126+
return {
127+
body: "",
128+
type: internalEvent.type,
129+
statusCode: res.statusCode,
130+
headers: {
131+
...resHeaders,
132+
Location: location,
133+
},
134+
isBase64Encoded: false,
135+
};
136+
}
137+
136138
// If the middleware returned a Rewrite, set the `url` to the pathname of the rewrite
137139
// NOTE: the header was added to `req` from above
138140
const rewriteUrl = responseHeaders.get("x-middleware-rewrite");
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { expect, test } from "@playwright/test";
22

3-
test("Middleware Redirect", async ({ page }) => {
3+
test("Middleware Redirect", async ({ page, context }) => {
44
await page.goto("/");
55
await page.locator('[href="/redirect"]').click();
66

77
// URL is immediately redirected
8-
await page.waitForURL(`/redirect-destination`);
8+
await page.waitForURL("/redirect-destination");
99
let el = page.getByText("Redirect Destination", { exact: true });
1010
await expect(el).toBeVisible();
1111

1212
// Loading page should also redirect
13-
await page.goto(`/redirect`);
14-
await page.waitForURL(`/redirect-destination`);
13+
await page.goto("/redirect");
14+
await page.waitForURL("/redirect-destination");
15+
expect(
16+
await context
17+
.cookies()
18+
.then((res) => res.find((cookie) => cookie.name === "test")?.value),
19+
).toBe("success");
1520
el = page.getByText("Redirect Destination", { exact: true });
1621
await expect(el).toBeVisible();
1722
});
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, test } from "@playwright/test";
22

3-
test("Middleware Redirect", async ({ page }) => {
3+
test("Middleware Redirect", async ({ page, context }) => {
44
await page.goto("/");
55
await page.getByRole("link", { name: "/Redirect" }).click();
66

@@ -10,8 +10,13 @@ test("Middleware Redirect", async ({ page }) => {
1010
await expect(el).toBeVisible();
1111

1212
// Loading page should also redirect
13-
await page.goto(`/redirect`);
14-
await page.waitForURL(`/redirect-destination`);
13+
await page.goto("/redirect");
14+
await page.waitForURL("/redirect-destination");
15+
expect(
16+
await context
17+
.cookies()
18+
.then((res) => res.find((cookie) => cookie.name === "test")?.value),
19+
).toBe("success");
1520
el = page.getByText("Redirect Destination", { exact: true });
1621
await expect(el).toBeVisible();
1722
});

packages/tests-unit/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
"exclude": [
2929
"dist",
3030
"build",
31-
"node_modules",
31+
"node_modules"
3232
]
3333
}

0 commit comments

Comments
 (0)