未验证 提交 782b7e48 编写于 作者: J JJ Kasper 提交者: GitHub

Add warning when exporting with custom routes (#17538)

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. 
上级 d32b1955
# `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)
......@@ -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 &&
......
......@@ -19,6 +19,7 @@ import {
waitFor,
normalizeRegEx,
initNextServerScript,
nextExport,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
......@@ -31,6 +32,7 @@ let nextConfigContent
let externalServerPort
let externalServer
let stdout = ''
let stderr = ''
let buildId
let appPort
let app
......@@ -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', () => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册