Unverified Commit 7af04db4 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix just headers not being applied in dev mode (#11755)

parent fa167e33
......@@ -237,9 +237,9 @@ export default class DevServer extends Server {
await this.loadCustomRoutes()
if (this.customRoutes) {
const { redirects, rewrites } = this.customRoutes
const { redirects, rewrites, headers } = this.customRoutes
if (redirects.length || rewrites.length) {
if (redirects.length || rewrites.length || headers.length) {
this.router = new Router(this.generateRoutes())
}
}
......
......@@ -885,4 +885,108 @@ describe('Custom routes', () => {
})
})
})
describe('should load custom routes when only one type is used', () => {
const runSoloTests = isDev => {
const buildAndStart = async () => {
if (isDev) {
appPort = await findPort()
app = await launchApp(appDir, appPort)
} else {
const { code } = await nextBuild(appDir)
if (code !== 0) throw new Error(`failed to build, got code ${code}`)
appPort = await findPort()
app = await nextStart(appDir, appPort)
}
}
it('should work with just headers', async () => {
nextConfigContent = await fs.readFile(nextConfigPath, 'utf8')
await fs.writeFile(
nextConfigPath,
nextConfigContent.replace(/(async (?:redirects|rewrites))/g, '$1s')
)
await buildAndStart()
const res = await fetchViaHTTP(appPort, '/add-header')
const res2 = await fetchViaHTTP(appPort, '/docs/github', undefined, {
redirect: 'manual',
})
const res3 = await fetchViaHTTP(appPort, '/hello-world')
await fs.writeFile(nextConfigPath, nextConfigContent)
await killApp(app)
expect(res.headers.get('x-custom-header')).toBe('hello world')
expect(res.headers.get('x-another-header')).toBe('hello again')
expect(res2.status).toBe(404)
expect(res3.status).toBe(404)
})
it('should work with just rewrites', async () => {
nextConfigContent = await fs.readFile(nextConfigPath, 'utf8')
await fs.writeFile(
nextConfigPath,
nextConfigContent.replace(/(async (?:redirects|headers))/g, '$1s')
)
await buildAndStart()
const res = await fetchViaHTTP(appPort, '/add-header')
const res2 = await fetchViaHTTP(appPort, '/docs/github', undefined, {
redirect: 'manual',
})
const res3 = await fetchViaHTTP(appPort, '/hello-world')
await fs.writeFile(nextConfigPath, nextConfigContent)
await killApp(app)
expect(res.headers.get('x-custom-header')).toBeFalsy()
expect(res.headers.get('x-another-header')).toBeFalsy()
expect(res2.status).toBe(404)
expect(res3.status).toBe(200)
expect(await res3.text()).toContain('hello world!')
})
it('should work with just redirects', async () => {
nextConfigContent = await fs.readFile(nextConfigPath, 'utf8')
await fs.writeFile(
nextConfigPath,
nextConfigContent.replace(/(async (?:rewrites|headers))/g, '$1s')
)
await buildAndStart()
const res = await fetchViaHTTP(appPort, '/add-header')
const res2 = await fetchViaHTTP(appPort, '/docs/github', undefined, {
redirect: 'manual',
})
const res3 = await fetchViaHTTP(appPort, '/hello world')
await fs.writeFile(nextConfigPath, nextConfigContent)
await killApp(app)
expect(res.headers.get('x-custom-header')).toBeFalsy()
expect(res.headers.get('x-another-header')).toBeFalsy()
const { pathname } = url.parse(res2.headers.get('location'))
expect(res2.status).toBe(301)
expect(pathname).toBe('/docs/v2/advanced/now-for-github')
expect(res3.status).toBe(404)
})
}
describe('dev mode', () => {
runSoloTests(true)
})
describe('production mode', () => {
runSoloTests()
})
})
})
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