File tree 3 files changed +19
-2
lines changed
packages/next/src/middleware
3 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,12 @@ describe('Enhanced middleware', () => {
13
13
} )
14
14
} )
15
15
16
+ it ( 'passes in headers within request.rewrite()' , ( ) => {
17
+ cy . request ( '/request-rewrite' ) . then ( ( response ) => {
18
+ expect ( response . headers ) . to . have . property ( 'x-rewrite-test' , 'hello' )
19
+ } )
20
+ } )
21
+
16
22
it ( 'rewrites the response body using request.next()' , ( ) => {
17
23
cy . visit ( '/static' )
18
24
cy . get ( '#message' ) . contains ( 'This was static (& escaping test &) but has been transformed in' )
Original file line number Diff line number Diff line change @@ -41,7 +41,13 @@ export async function middleware(req: NextRequest) {
41
41
// skipMiddlewareUrlNormalize next config option is used so we have to try to match both html path and data blob path
42
42
if ( pathname . startsWith ( '/request-rewrite' ) || pathname . endsWith ( '/request-rewrite.json' ) ) {
43
43
// request.rewrite() should return the MiddlewareResponse object instead of the Response object.
44
- const res = await request . rewrite ( '/static-rewrite' )
44
+ const res = await request . rewrite ( '/static-rewrite' ,
45
+ {
46
+ headers : {
47
+ 'x-rewrite-test' : 'hello' ,
48
+ 'x-rewrite-test-2' : 'hello-2'
49
+ }
50
+ } )
45
51
const message = `This was static (& escaping test &) but has been transformed in ${ req . geo ?. city } `
46
52
47
53
// Transform the response HTML and props
Original file line number Diff line number Diff line change @@ -11,7 +11,6 @@ export class MiddlewareResponse extends NextResponse {
11
11
private readonly dataTransforms : NextDataTransform [ ]
12
12
private readonly elementHandlers : Array < [ selector : string , handlers : ElementHandlers ] >
13
13
14
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
14
constructor ( public originResponse : Response , init ?: ResponseInit ) {
16
15
// we need to propagate the set-cookie header, so response.cookies.get works correctly
17
16
const initHeaders = new Headers ( )
@@ -23,6 +22,12 @@ export class MiddlewareResponse extends NextResponse {
23
22
headers : initHeaders ,
24
23
} )
25
24
25
+ if ( init ?. headers ) {
26
+ Object . entries ( init . headers ) . forEach ( ( [ key , value ] ) => {
27
+ this . headers . set ( key , value )
28
+ } )
29
+ }
30
+
26
31
// These are private in Node when compiling, but we access them in Deno at runtime
27
32
Object . defineProperty ( this , 'dataTransforms' , {
28
33
value : [ ] ,
You can’t perform that action at this time.
0 commit comments