Skip to content

Commit 4199c23

Browse files
committed
fix: pass in new headers
1 parent 243330d commit 4199c23

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

demos/middleware/middleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export async function middleware(req: NextRequest) {
4040
// request.rewrite() should return the MiddlewareResponse obj instead of the Response obj
4141
const res = await request.rewrite('/static-rewrite', {
4242
headers: {
43-
'x-rewrite-test': 'hello'
43+
'x-rewrite-test': 'hello',
44+
'x-rewrite-test-2': 'hello-2'
4445
}
4546
})
4647
const message = `This was static (& escaping test &) but has been transformed in ${req.geo?.city}`
@@ -52,6 +53,7 @@ export async function middleware(req: NextRequest) {
5253

5354
res.headers.set('x-modified-edge', 'true')
5455
res.headers.set('x-is-deno', 'Deno' in globalThis ? 'true' : 'false')
56+
console.log({res})
5557
return res
5658
}
5759

packages/next/src/middleware/response.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type NextDataTransform = <T extends { pageProps?: Record<string, any> }>(
1010
export class MiddlewareResponse extends NextResponse {
1111
private readonly dataTransforms: NextDataTransform[]
1212
private readonly elementHandlers: Array<[selector: string, handlers: ElementHandlers]>
13-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
13+
1414
constructor(public originResponse: Response, init?: ResponseInit) {
1515
// we need to propagate the set-cookie header, so response.cookies.get works correctly
1616
const initHeaders = new Headers()
@@ -22,6 +22,12 @@ export class MiddlewareResponse extends NextResponse {
2222
headers: initHeaders,
2323
})
2424

25+
if (init?.headers) {
26+
Object.entries(init.headers).forEach(([key, value]) => {
27+
this.headers.set(key, value)
28+
})
29+
}
30+
2531
// These are private in Node when compiling, but we access them in Deno at runtime
2632
Object.defineProperty(this, 'dataTransforms', {
2733
value: [],

0 commit comments

Comments
 (0)