Skip to content

Commit

Permalink
Add warning when exporting with custom routes (#17538)
Browse files Browse the repository at this point in the history
This adds a warning when `next export` and custom routes are defined  outside of a platform that supports them since they won't be applied anymore.
  • Loading branch information
ijjk committed Oct 5, 2020
1 parent d32b195 commit 782b7e4
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
19 changes: 19 additions & 0 deletions errors/export-no-custom-routes.md
@@ -0,0 +1,19 @@
# `next export` No Custom Routes

#### Why This Error Occurred

In your `next.config.js` `rewrites`, `redirects`, or `headers` were defined while `next export` was being run outside of a platform that supports them.

These configs do not apply when exporting your Next.js application manually.

#### Possible Ways to Fix It

Disable the `rewrites`, `redirects`, and `headers` from your `next.config.js` when using `next export` to deploy your application or deploy your application using [a method](https://nextjs.org/docs/deployment#vercel-recommended) that supports these configs.

### Useful Links

- [Deployment Documentation](https://nextjs.org/docs/deployment#vercel-recommended)
- [`next export` Documentation](https://nextjs.org/docs/advanced-features/static-html-export)
- [Rewrites Documentation](https://nextjs.org/docs/api-reference/next.config.js/rewrites)
- [Redirects Documentation](https://nextjs.org/docs/api-reference/next.config.js/redirects)
- [Headers Documentation](https://nextjs.org/docs/api-reference/next.config.js/headers)
16 changes: 16 additions & 0 deletions packages/next/export/index.ts
Expand Up @@ -165,6 +165,22 @@ export default async function exportApp(
)
}

const customRoutesDetected = ['rewrites', 'redirects', 'headers'].filter(
(config) => typeof nextConfig[config] === 'function'
)

if (
!process.env.NOW_BUILDER &&
!options.buildExport &&
customRoutesDetected.length > 0
) {
Log.warn(
`rewrites, redirects, and headers are not applied when exporting your application, detected (${customRoutesDetected.join(
', '
)}). See more info here: https://err.sh/next.js/export-no-custom-routes`
)
}

const buildId = readFileSync(join(distDir, BUILD_ID_FILE), 'utf8')
const pagesManifest =
!options.pages &&
Expand Down
74 changes: 71 additions & 3 deletions test/integration/custom-routes/test/index.test.js
Expand Up @@ -19,6 +19,7 @@ import {
waitFor,
normalizeRegEx,
initNextServerScript,
nextExport,
} from 'next-test-utils'

jest.setTimeout(1000 * 60 * 2)
Expand All @@ -31,6 +32,7 @@ let nextConfigContent
let externalServerPort
let externalServer
let stdout = ''
let stderr = ''
let buildId
let appPort
let app
Expand Down Expand Up @@ -1119,16 +1121,82 @@ describe('Custom routes', () => {

describe('server mode', () => {
beforeAll(async () => {
const { stdout: buildStdout } = await nextBuild(appDir, ['-d'], {
stdout: true,
})
const { stdout: buildStdout, stderr: buildStderr } = await nextBuild(
appDir,
['-d'],
{
stdout: true,
stderr: true,
}
)
stdout = buildStdout
stderr = buildStderr
appPort = await findPort()
app = await nextStart(appDir, appPort)
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
})
afterAll(() => killApp(app))
runTests()

it('should not show warning for custom routes when not next export', async () => {
expect(stderr).not.toContain(
`rewrites, redirects, and headers are not applied when exporting your application detected`
)
})
})

describe('export', () => {
let exportStderr = ''
let exportVercelStderr = ''

beforeAll(async () => {
const { stdout: buildStdout, stderr: buildStderr } = await nextBuild(
appDir,
['-d'],
{
stdout: true,
stderr: true,
}
)
const exportResult = await nextExport(
appDir,
{ outdir: join(appDir, 'out') },
{ stderr: true }
)
const exportVercelResult = await nextExport(
appDir,
{ outdir: join(appDir, 'out') },
{
stderr: true,
env: {
NOW_BUILDER: '1',
},
}
)

stdout = buildStdout
stderr = buildStderr
exportStderr = exportResult.stderr
exportVercelStderr = exportVercelResult.stderr
})

it('should not show warning for custom routes when not next export', async () => {
expect(stderr).not.toContain(
`rewrites, redirects, and headers are not applied when exporting your application detected`
)
})

it('should not show warning for custom routes when next export on Vercel', async () => {
expect(exportVercelStderr).not.toContain(
`rewrites, redirects, and headers are not applied when exporting your application detected`
)
})

it('should show warning for custom routes with next export', async () => {
expect(exportStderr).toContain(
`rewrites, redirects, and headers are not applied when exporting your application, detected (rewrites, redirects, headers)`
)
})
})

describe('serverless mode', () => {
Expand Down

0 comments on commit 782b7e4

Please sign in to comment.