Unverified Commit 61b68730 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

De-experimentalize custom-routes (#14602)

This moves the custom-routes configs outside of the experimental section to prepare them for being made stable

Fixes: https://github.com/vercel/next.js/issues/14184
parent 166834e5
# Rewriting to Auto Export or Fallback Dynamic Route
#### Why This Error Occurred
One of your rewrites in your `next.config.js` point to a [dynamic route](https://nextjs.org/docs/routing/dynamic-routes) that is automatically statically optimized or is a [fallback SSG page](https://nextjs.org/docs/basic-features/data-fetching#the-fallback-key-required).
Rewriting to these pages are not yet supported since rewrites are not available client-side and the dynamic route params are unable to be parsed. Support for this may be added in a future release.
#### Possible Ways to Fix It
For fallback SSG pages you can add the page to the list of [prerendered paths](https://nextjs.org/docs/basic-features/data-fetching#the-paths-key-required).
For static dynamic routes, you will currently need to either rewrite to non-dynamic route or opt the page out of the static optimization with [`getServerSideProps`](https://nextjs.org/docs/basic-features/data-fetching#getserversideprops-server-side-rendering)
### Useful Links
- [Dynamic Routes Documentation](https://nextjs.org/docs/routing/dynamic-routes)
- [Fallback Documentation](https://nextjs.org/docs/basic-features/data-fetching#the-fallback-key-required)
...@@ -13,14 +13,12 @@ Make sure to return an array that contains the routes. ...@@ -13,14 +13,12 @@ Make sure to return an array that contains the routes.
```js ```js
// next.config.js // next.config.js
module.exports = { module.exports = {
experimental: {
async rewrites() { async rewrites() {
return { return {
source: '/feedback', source: '/feedback',
destination: '/feedback/general', destination: '/feedback/general',
} }
}, },
},
} }
``` ```
...@@ -28,7 +26,6 @@ module.exports = { ...@@ -28,7 +26,6 @@ module.exports = {
```js ```js
module.exports = { module.exports = {
experimental: {
async rewrites() { async rewrites() {
return [ return [
{ {
...@@ -37,6 +34,5 @@ module.exports = { ...@@ -37,6 +34,5 @@ module.exports = {
}, },
] ]
}, },
},
} }
``` ```
module.exports = { module.exports = {
experimental: {
async rewrites() { async rewrites() {
return [ return [
// we need to define a no-op rewrite to trigger checking // we need to define a no-op rewrite to trigger checking
...@@ -14,5 +13,4 @@ module.exports = { ...@@ -14,5 +13,4 @@ module.exports = {
}, },
] ]
}, },
},
} }
...@@ -18,7 +18,6 @@ module.exports = withOffline({ ...@@ -18,7 +18,6 @@ module.exports = withOffline({
}, },
], ],
}, },
experimental: {
async rewrites() { async rewrites() {
return [ return [
{ {
...@@ -27,5 +26,4 @@ module.exports = withOffline({ ...@@ -27,5 +26,4 @@ module.exports = withOffline({
}, },
] ]
}, },
},
}) })
...@@ -310,28 +310,28 @@ export interface CustomRoutes { ...@@ -310,28 +310,28 @@ export interface CustomRoutes {
} }
async function loadRedirects(config: any) { async function loadRedirects(config: any) {
if (typeof config.experimental.redirects !== 'function') { if (typeof config.redirects !== 'function') {
return [] return []
} }
const _redirects = await config.experimental.redirects() const _redirects = await config.redirects()
checkCustomRoutes(_redirects, 'redirect') checkCustomRoutes(_redirects, 'redirect')
return _redirects return _redirects
} }
async function loadRewrites(config: any) { async function loadRewrites(config: any) {
if (typeof config.experimental.rewrites !== 'function') { if (typeof config.rewrites !== 'function') {
return [] return []
} }
const _rewrites = await config.experimental.rewrites() const _rewrites = await config.rewrites()
checkCustomRoutes(_rewrites, 'rewrite') checkCustomRoutes(_rewrites, 'rewrite')
return _rewrites return _rewrites
} }
async function loadHeaders(config: any) { async function loadHeaders(config: any) {
if (typeof config.experimental.headers !== 'function') { if (typeof config.headers !== 'function') {
return [] return []
} }
const _headers = await config.experimental.headers() const _headers = await config.headers()
checkCustomRoutes(_headers, 'header') checkCustomRoutes(_headers, 'header')
return _headers return _headers
} }
......
...@@ -341,15 +341,14 @@ export async function renderToHTML( ...@@ -341,15 +341,14 @@ export async function renderToHTML(
pageIsDynamic && pageIsDynamic &&
didRewrite didRewrite
) { ) {
// TODO: add err.sh when rewrites go stable // TODO: If we decide to ship rewrites to the client we could
// Behavior might change before then (prefer SSR in this case). // solve this by running over the rewrites and getting the params.
// If we decide to ship rewrites to the client we could solve this
// by running over the rewrites and getting the params.
throw new Error( throw new Error(
`Rewrites don't support${ `Rewrites don't support${
isFallback ? ' ' : ' auto-exported ' isFallback ? ' ' : ' auto-exported '
}dynamic pages${isFallback ? ' with getStaticProps ' : ' '}yet. ` + }dynamic pages${isFallback ? ' with getStaticProps ' : ' '}yet.\n` +
`Using this will cause the page to fail to parse the params on the client` `Using this will cause the page to fail to parse the params on the client\n` +
`See more info: https://err.sh/next.js/rewrite-auto-export-fallback`
) )
} }
......
module.exports = { module.exports = {
experimental: {
rewrites() { rewrites() {
return [ return [
{ {
...@@ -8,5 +7,4 @@ module.exports = { ...@@ -8,5 +7,4 @@ module.exports = {
}, },
] ]
}, },
},
} }
...@@ -19,6 +19,11 @@ let appPort ...@@ -19,6 +19,11 @@ let appPort
let app let app
const runTests = () => { const runTests = () => {
it('should rewrite and render page correctly', async () => {
const html = await renderViaHTTP(appPort, '/docs/hello')
expect(html).toMatch(/hello world/)
})
it('should rewrite to /_next/static correctly', async () => { it('should rewrite to /_next/static correctly', async () => {
const bundlePath = await join( const bundlePath = await join(
'/docs/_next/static/', '/docs/_next/static/',
...@@ -29,11 +34,6 @@ const runTests = () => { ...@@ -29,11 +34,6 @@ const runTests = () => {
expect(data).toContain('/hello') expect(data).toContain('/hello')
}) })
it('should rewrite and render page correctly', async () => {
const html = await renderViaHTTP(appPort, '/docs/hello')
expect(html).toMatch(/hello world/)
})
it('should rewrite to public/static correctly', async () => { it('should rewrite to public/static correctly', async () => {
const data = await renderViaHTTP(appPort, '/docs/static/data.json') const data = await renderViaHTTP(appPort, '/docs/static/data.json')
expect(data).toContain('some data...') expect(data).toContain('some data...')
......
module.exports = { module.exports = {
// target: 'serverless', // target: 'serverless',
experimental: {
async rewrites() { async rewrites() {
return [ return [
{ {
...@@ -298,5 +297,4 @@ module.exports = { ...@@ -298,5 +297,4 @@ module.exports = {
}, },
] ]
}, },
},
} }
module.exports = { module.exports = {
// update me // update me
experimental: {
async redirects() { async redirects() {
return [ return [
{ {
...@@ -10,5 +9,4 @@ module.exports = { ...@@ -10,5 +9,4 @@ module.exports = {
}, },
] ]
}, },
},
} }
...@@ -14,12 +14,10 @@ const writeConfig = async (routes, type = 'redirects') => { ...@@ -14,12 +14,10 @@ const writeConfig = async (routes, type = 'redirects') => {
nextConfigPath, nextConfigPath,
` `
module.exports = { module.exports = {
experimental: {
async ${type}() { async ${type}() {
return ${JSON.stringify(routes)} return ${JSON.stringify(routes)}
} }
} }
}
` `
) )
} }
......
module.exports = { module.exports = {
experimental: {
rewrites() { rewrites() {
return [ return [
{ {
...@@ -8,5 +7,4 @@ module.exports = { ...@@ -8,5 +7,4 @@ module.exports = {
}, },
] ]
}, },
},
} }
module.exports = { module.exports = {
experimental: { experimental: {
optionalCatchAll: true, optionalCatchAll: true,
},
rewrites() { rewrites() {
return [ return [
{ {
...@@ -9,5 +10,4 @@ module.exports = { ...@@ -9,5 +10,4 @@ module.exports = {
}, },
] ]
}, },
},
} }
...@@ -608,7 +608,7 @@ const runTests = (dev = false, isEmulatedServerless = false) => { ...@@ -608,7 +608,7 @@ const runTests = (dev = false, isEmulatedServerless = false) => {
const item = Math.round(Math.random() * 100) const item = Math.round(Math.random() * 100)
const html = await renderViaHTTP(appPort, `/some-rewrite/${item}`) const html = await renderViaHTTP(appPort, `/some-rewrite/${item}`)
expect(html).toContain( expect(html).toContain(
`Rewrites don't support dynamic pages with getStaticProps yet. Using this will cause the page to fail to parse the params on the client` `Rewrites don't support dynamic pages with getStaticProps yet`
) )
}) })
...@@ -1292,6 +1292,7 @@ describe('SSG Prerender', () => { ...@@ -1292,6 +1292,7 @@ describe('SSG Prerender', () => {
module.exports = { module.exports = {
experimental: { experimental: {
optionalCatchAll: true, optionalCatchAll: true,
},
rewrites() { rewrites() {
return [ return [
{ {
...@@ -1305,7 +1306,6 @@ describe('SSG Prerender', () => { ...@@ -1305,7 +1306,6 @@ describe('SSG Prerender', () => {
] ]
} }
} }
}
` `
) )
appPort = await findPort() appPort = await findPort()
...@@ -1401,6 +1401,7 @@ describe('SSG Prerender', () => { ...@@ -1401,6 +1401,7 @@ describe('SSG Prerender', () => {
target: 'serverless', target: 'serverless',
experimental: { experimental: {
optionalCatchAll: true, optionalCatchAll: true,
},
rewrites() { rewrites() {
return [ return [
{ {
...@@ -1409,7 +1410,6 @@ describe('SSG Prerender', () => { ...@@ -1409,7 +1410,6 @@ describe('SSG Prerender', () => {
} }
] ]
} }
}
}`, }`,
'utf8' 'utf8'
) )
......
...@@ -3,7 +3,6 @@ module.exports = { ...@@ -3,7 +3,6 @@ module.exports = {
// Make sure entries are not getting disposed. // Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60, maxInactiveAge: 1000 * 60 * 60,
}, },
experimental: {
redirects() { redirects() {
return [ return [
{ {
...@@ -13,5 +12,4 @@ module.exports = { ...@@ -13,5 +12,4 @@ module.exports = {
}, },
] ]
}, },
},
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment