Commit 850a552c authored by Zack Tanner's avatar Zack Tanner

fix nextConfig headers not applying to static resources with i18n

parent 708f85b9
...@@ -547,9 +547,10 @@ function processRoutes<T>( ...@@ -547,9 +547,10 @@ function processRoutes<T>(
}) })
} }
r.source = `/:nextInternalLocale(${config.i18n.locales const locales = config.i18n.locales.map(escapeStringRegexp).join('|')
.map((locale: string) => escapeStringRegexp(locale)) // locale is intentionally marked optional here so that custom route headers
.join('|')})${ // are applied to static resources (which don't have a locale prefix)
r.source = `/:nextInternalLocale(${locales})?${
r.source === '/' && !config.trailingSlash ? '' : r.source r.source === '/' && !config.trailingSlash ? '' : r.source
}` }`
......
...@@ -81,4 +81,17 @@ module.exports = { ...@@ -81,4 +81,17 @@ module.exports = {
}, },
] ]
}, },
async headers() {
return [
{
source: '/:path*',
headers: [
{
key: 'X-Custom-Header',
value: 'my custom header value',
},
],
},
]
},
} }
...@@ -145,6 +145,32 @@ const runTests = () => { ...@@ -145,6 +145,32 @@ const runTests = () => {
expect(await browser.eval('window.beforeNav')).toBe(1) expect(await browser.eval('window.beforeNav')).toBe(1)
} }
}) })
it('should apply custom headers to all responses', async () => {
const staticHeaders = []
const otherHeaders = []
const browser = await webdriver(appPort, '/', {
beforePageLoad: (page) => {
page.on('response', (response) => {
const url = new URL(response.url())
if (url.pathname.includes('_next/static/')) {
staticHeaders.push(response.headers())
} else {
otherHeaders.push(response.headers())
}
})
},
})
await check(() => browser.elementById('links').text(), 'Links')
expect(staticHeaders.length).toBeGreaterThan(0)
expect(otherHeaders.length).toBeGreaterThan(0)
expect(
[...staticHeaders, ...otherHeaders].every(
(headers) => headers['x-custom-header'] === 'my custom header value'
)
).toBe(true)
})
} }
describe('Custom routes i18n', () => { describe('Custom routes i18n', () => {
......
...@@ -755,6 +755,15 @@ module.exports = { ...@@ -755,6 +755,15 @@ module.exports = {
}, },
], ],
}, },
{
source: '/:path*',
headers: [
{
key: 'X-Global-Custom-Header',
value: 'my custom header value',
},
],
},
] ]
}, },
} }
...@@ -1475,6 +1475,33 @@ const runTests = (isDev = false) => { ...@@ -1475,6 +1475,33 @@ const runTests = (isDev = false) => {
expect(res2.headers.get('x-is-host')).toBe(null) expect(res2.headers.get('x-is-host')).toBe(null)
}) })
it('should apply custom headers to all responses', async () => {
const staticHeaders = []
const otherHeaders = []
const browser = await webdriver(appPort, '/nav', {
beforePageLoad: (page) => {
page.on('response', (response) => {
const url = new URL(response.url())
if (url.pathname.includes('_next/static/')) {
staticHeaders.push(response.headers())
} else {
otherHeaders.push(response.headers())
}
})
},
})
await check(() => browser.elementById('nav').text(), 'Nav')
expect(staticHeaders.length).toBeGreaterThan(0)
expect(otherHeaders.length).toBeGreaterThan(0)
expect(
[...staticHeaders, ...otherHeaders].every(
(headers) =>
headers['x-global-custom-header'] === 'my custom header value'
)
).toBe(true)
})
if (!isDev) { if (!isDev) {
it('should output routes-manifest successfully', async () => { it('should output routes-manifest successfully', async () => {
const manifest = await fs.readJSON( const manifest = await fs.readJSON(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment