From 0c202704631aa865bd664278f4bf5b50667b07d8 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Wed, 8 Apr 2020 12:30:34 -0500 Subject: [PATCH] Fix just headers not being applied in dev mode --- packages/next/server/next-dev-server.ts | 4 +- .../custom-routes/test/index.test.js | 104 ++++++++++++++++++ 2 files changed, 106 insertions(+), 2 deletions(-) diff --git a/packages/next/server/next-dev-server.ts b/packages/next/server/next-dev-server.ts index dd149a2999053..a03faa8dd3e61 100644 --- a/packages/next/server/next-dev-server.ts +++ b/packages/next/server/next-dev-server.ts @@ -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()) } } diff --git a/test/integration/custom-routes/test/index.test.js b/test/integration/custom-routes/test/index.test.js index 04e77f31b7d55..d931611ce49fc 100644 --- a/test/integration/custom-routes/test/index.test.js +++ b/test/integration/custom-routes/test/index.test.js @@ -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() + }) + }) })