Skip to content

fix: override headers within req.rewrite() #2189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cypress/e2e/middleware/enhanced.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ describe('Enhanced middleware', () => {
})
})

it.skip('passes in headers within request.rewrite()', () => {
cy.request('/request-rewrite').then((response) => {
expect(response.headers).to.have.property('x-rewrite-test', 'hello')
})
})

it('rewrites the response body using request.next()', () => {
cy.visit('/static')
cy.get('#message').contains('This was static (& escaping test &) but has been transformed in')
Expand Down
8 changes: 7 additions & 1 deletion demos/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ export async function middleware(req: NextRequest) {
// skipMiddlewareUrlNormalize next config option is used so we have to try to match both html path and data blob path
if (pathname.startsWith('/request-rewrite') || pathname.endsWith('/request-rewrite.json')) {
// request.rewrite() should return the MiddlewareResponse object instead of the Response object.
const res = await request.rewrite('/static-rewrite')
const res = await request.rewrite('/static-rewrite',
{
headers: {
'x-rewrite-test': 'hello',
'x-rewrite-test-2': 'hello-2'
}
})
const message = `This was static (& escaping test &) but has been transformed in ${req.geo?.city}`

// Transform the response HTML and props
Expand Down
7 changes: 6 additions & 1 deletion packages/next/src/middleware/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class MiddlewareResponse extends NextResponse {
private readonly dataTransforms: NextDataTransform[]
private readonly elementHandlers: Array<[selector: string, handlers: ElementHandlers]>

// eslint-disable-next-line @typescript-eslint/no-unused-vars
constructor(public originResponse: Response, init?: ResponseInit) {
// we need to propagate the set-cookie header, so response.cookies.get works correctly
const initHeaders = new Headers()
Expand All @@ -23,6 +22,12 @@ export class MiddlewareResponse extends NextResponse {
headers: initHeaders,
})

if (init?.headers) {
Object.entries(init.headers).forEach(([key, value]) => {
this.headers.set(key, value)
})
}

// These are private in Node when compiling, but we access them in Deno at runtime
Object.defineProperty(this, 'dataTransforms', {
value: [],
Expand Down