Skip to content

Commit 782b7e4

Browse files
authored
Add warning when exporting with custom routes (vercel#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.
1 parent d32b195 commit 782b7e4

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

errors/export-no-custom-routes.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# `next export` No Custom Routes
2+
3+
#### Why This Error Occurred
4+
5+
In your `next.config.js` `rewrites`, `redirects`, or `headers` were defined while `next export` was being run outside of a platform that supports them.
6+
7+
These configs do not apply when exporting your Next.js application manually.
8+
9+
#### Possible Ways to Fix It
10+
11+
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.
12+
13+
### Useful Links
14+
15+
- [Deployment Documentation](https://nextjs.org/docs/deployment#vercel-recommended)
16+
- [`next export` Documentation](https://nextjs.org/docs/advanced-features/static-html-export)
17+
- [Rewrites Documentation](https://nextjs.org/docs/api-reference/next.config.js/rewrites)
18+
- [Redirects Documentation](https://nextjs.org/docs/api-reference/next.config.js/redirects)
19+
- [Headers Documentation](https://nextjs.org/docs/api-reference/next.config.js/headers)

packages/next/export/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,22 @@ export default async function exportApp(
165165
)
166166
}
167167

168+
const customRoutesDetected = ['rewrites', 'redirects', 'headers'].filter(
169+
(config) => typeof nextConfig[config] === 'function'
170+
)
171+
172+
if (
173+
!process.env.NOW_BUILDER &&
174+
!options.buildExport &&
175+
customRoutesDetected.length > 0
176+
) {
177+
Log.warn(
178+
`rewrites, redirects, and headers are not applied when exporting your application, detected (${customRoutesDetected.join(
179+
', '
180+
)}). See more info here: https://err.sh/next.js/export-no-custom-routes`
181+
)
182+
}
183+
168184
const buildId = readFileSync(join(distDir, BUILD_ID_FILE), 'utf8')
169185
const pagesManifest =
170186
!options.pages &&

test/integration/custom-routes/test/index.test.js

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
waitFor,
2020
normalizeRegEx,
2121
initNextServerScript,
22+
nextExport,
2223
} from 'next-test-utils'
2324

2425
jest.setTimeout(1000 * 60 * 2)
@@ -31,6 +32,7 @@ let nextConfigContent
3132
let externalServerPort
3233
let externalServer
3334
let stdout = ''
35+
let stderr = ''
3436
let buildId
3537
let appPort
3638
let app
@@ -1119,16 +1121,82 @@ describe('Custom routes', () => {
11191121

11201122
describe('server mode', () => {
11211123
beforeAll(async () => {
1122-
const { stdout: buildStdout } = await nextBuild(appDir, ['-d'], {
1123-
stdout: true,
1124-
})
1124+
const { stdout: buildStdout, stderr: buildStderr } = await nextBuild(
1125+
appDir,
1126+
['-d'],
1127+
{
1128+
stdout: true,
1129+
stderr: true,
1130+
}
1131+
)
11251132
stdout = buildStdout
1133+
stderr = buildStderr
11261134
appPort = await findPort()
11271135
app = await nextStart(appDir, appPort)
11281136
buildId = await fs.readFile(join(appDir, '.next/BUILD_ID'), 'utf8')
11291137
})
11301138
afterAll(() => killApp(app))
11311139
runTests()
1140+
1141+
it('should not show warning for custom routes when not next export', async () => {
1142+
expect(stderr).not.toContain(
1143+
`rewrites, redirects, and headers are not applied when exporting your application detected`
1144+
)
1145+
})
1146+
})
1147+
1148+
describe('export', () => {
1149+
let exportStderr = ''
1150+
let exportVercelStderr = ''
1151+
1152+
beforeAll(async () => {
1153+
const { stdout: buildStdout, stderr: buildStderr } = await nextBuild(
1154+
appDir,
1155+
['-d'],
1156+
{
1157+
stdout: true,
1158+
stderr: true,
1159+
}
1160+
)
1161+
const exportResult = await nextExport(
1162+
appDir,
1163+
{ outdir: join(appDir, 'out') },
1164+
{ stderr: true }
1165+
)
1166+
const exportVercelResult = await nextExport(
1167+
appDir,
1168+
{ outdir: join(appDir, 'out') },
1169+
{
1170+
stderr: true,
1171+
env: {
1172+
NOW_BUILDER: '1',
1173+
},
1174+
}
1175+
)
1176+
1177+
stdout = buildStdout
1178+
stderr = buildStderr
1179+
exportStderr = exportResult.stderr
1180+
exportVercelStderr = exportVercelResult.stderr
1181+
})
1182+
1183+
it('should not show warning for custom routes when not next export', async () => {
1184+
expect(stderr).not.toContain(
1185+
`rewrites, redirects, and headers are not applied when exporting your application detected`
1186+
)
1187+
})
1188+
1189+
it('should not show warning for custom routes when next export on Vercel', async () => {
1190+
expect(exportVercelStderr).not.toContain(
1191+
`rewrites, redirects, and headers are not applied when exporting your application detected`
1192+
)
1193+
})
1194+
1195+
it('should show warning for custom routes with next export', async () => {
1196+
expect(exportStderr).toContain(
1197+
`rewrites, redirects, and headers are not applied when exporting your application, detected (rewrites, redirects, headers)`
1198+
)
1199+
})
11321200
})
11331201

11341202
describe('serverless mode', () => {

0 commit comments

Comments
 (0)