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 just headers not being applied in dev mode #11755

Merged
merged 1 commit into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/next/server/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down
104 changes: 104 additions & 0 deletions test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})