Skip to content
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

Fix duplicate headers in response to static assets if overrides specified by user #70127

Merged
merged 16 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
18 changes: 18 additions & 0 deletions packages/next/src/server/send-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export async function sendResponse(
res.statusCode = response.status
res.statusMessage = response.statusText

const headersWithNoMultipleValuesAllowed = [
abhi12299 marked this conversation as resolved.
Show resolved Hide resolved
'content-type',
'content-length',
'content-encoding',
'cache-control',
'expires',
'etag',
]

// Copy over the response headers.
response.headers?.forEach((value, name) => {
// The append handling is special cased for `set-cookie`.
Expand All @@ -36,6 +45,15 @@ export async function sendResponse(
res.appendHeader(name, cookie)
}
} else {
// if there is a common header from the list of headers which should not
// have multiple values do not overwrite it
const isHeaderPresentInRes = typeof res.getHeader(name) !== 'undefined'
if (
headersWithNoMultipleValuesAllowed.includes(name.toLowerCase()) &&
isHeaderPresentInRes
) {
return
}
res.appendHeader(name, value)
}
})
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
3 changes: 3 additions & 0 deletions test/e2e/app-dir/no-duplicate-headers-middleware/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p>hello world</p>
}
13 changes: 13 additions & 0 deletions test/e2e/app-dir/no-duplicate-headers-middleware/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
if (request.nextUrl.pathname === '/favicon.ico') {
return NextResponse.next({
headers: {
'Cache-Control': 'max-age=1234',
'Content-Type': 'image/vnd.microsoft.icon',
},
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { nextTestSetup } from 'e2e-utils'

describe('no-duplicate-headers-next-config', () => {
const { next } = nextTestSetup({
files: __dirname,
})

it('should prioritise headers in middleware for static assets', async () => {
const res = await next.fetch('favicon.ico')
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe('max-age=1234')
expect(res.headers.get('content-type')).toBe('image/vnd.microsoft.icon')
})
})
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <p>hello world</p>
}
24 changes: 24 additions & 0 deletions test/e2e/app-dir/no-duplicate-headers-next-config/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
async headers() {
return [
{
source: '/favicon.ico',
headers: [
{
key: 'cache-control',
value: 'max-age=1234',
},
{
key: 'content-type',
value: 'image/vnd.microsoft.icon',
},
],
},
]
},
}

module.exports = nextConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { nextTestSetup } from 'e2e-utils'

describe('no-duplicate-headers-next-config', () => {
const { next } = nextTestSetup({
files: __dirname,
})

it('should prioritise headers in next config for static assets', async () => {
const res = await next.fetch('favicon.ico')
expect(res.status).toBe(200)
expect(res.headers.get('cache-control')).toBe('max-age=1234')
expect(res.headers.get('content-type')).toBe('image/vnd.microsoft.icon')
})
})
Loading