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,13 +13,11 @@ Make sure to return an array that contains the routes. ...@@ -13,13 +13,11 @@ 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,15 +26,13 @@ module.exports = { ...@@ -28,15 +26,13 @@ module.exports = {
```js ```js
module.exports = { module.exports = {
experimental: { async rewrites() {
async rewrites() { return [
return [ {
{ source: '/feedback',
source: '/feedback', destination: '/feedback/general',
destination: '/feedback/general', },
}, ]
]
},
}, },
} }
``` ```
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 // all pages/static files before we attempt proxying
// all pages/static files before we attempt proxying {
{ source: '/:path*',
source: '/:path*', destination: '/:path*',
destination: '/:path*', },
}, {
{ source: '/:path*',
source: '/:path*', destination: `https://custom-routes-proxying-endpoint.vercel.app/:path*`,
destination: `https://custom-routes-proxying-endpoint.vercel.app/:path*`, },
}, ]
]
},
}, },
} }
...@@ -18,14 +18,12 @@ module.exports = withOffline({ ...@@ -18,14 +18,12 @@ module.exports = withOffline({
}, },
], ],
}, },
experimental: { async rewrites() {
async rewrites() { return [
return [ {
{ source: '/service-worker.js',
source: '/service-worker.js', destination: '/_next/static/service-worker.js',
destination: '/_next/static/service-worker.js', },
}, ]
]
},
}, },
}) })
...@@ -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 [ {
{ source: '/docs/:path*',
source: '/docs/:path*', destination: '/:path*',
destination: '/:path*', },
}, ]
]
},
}, },
} }
...@@ -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 = {
// update me // update me
experimental: { async redirects() {
async redirects() { return [
return [ {
{ source: '/hello',
source: '/hello', permanent: false,
permanent: false, destination: `/${process.env.NEXT_PUBLIC_TEST_DEST}`,
destination: `/${process.env.NEXT_PUBLIC_TEST_DEST}`, },
}, ]
]
},
}, },
} }
...@@ -14,10 +14,8 @@ const writeConfig = async (routes, type = 'redirects') => { ...@@ -14,10 +14,8 @@ 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 [ {
{ source: '/:hello*',
source: '/:hello*', destination: '/:hello',
destination: '/:hello', },
}, ]
]
},
}, },
} }
module.exports = { module.exports = {
experimental: { experimental: {
optionalCatchAll: true, optionalCatchAll: true,
rewrites() { },
return [ rewrites() {
{ return [
source: '/about', {
destination: '/lang/en/about', source: '/about',
}, destination: '/lang/en/about',
] },
}, ]
}, },
} }
...@@ -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,18 +1292,18 @@ describe('SSG Prerender', () => { ...@@ -1292,18 +1292,18 @@ describe('SSG Prerender', () => {
module.exports = { module.exports = {
experimental: { experimental: {
optionalCatchAll: true, optionalCatchAll: true,
rewrites() { },
return [ rewrites() {
{ return [
source: "/some-rewrite/:item", {
destination: "/blog/post-:item" source: "/some-rewrite/:item",
}, destination: "/blog/post-:item"
{ },
source: '/about', {
destination: '/lang/en/about' source: '/about',
} destination: '/lang/en/about'
] }
} ]
} }
} }
` `
...@@ -1401,14 +1401,14 @@ describe('SSG Prerender', () => { ...@@ -1401,14 +1401,14 @@ describe('SSG Prerender', () => {
target: 'serverless', target: 'serverless',
experimental: { experimental: {
optionalCatchAll: true, optionalCatchAll: true,
rewrites() { },
return [ rewrites() {
{ return [
source: '/about', {
destination: '/lang/en/about' source: '/about',
} destination: '/lang/en/about'
] }
} ]
} }
}`, }`,
'utf8' 'utf8'
......
...@@ -3,15 +3,13 @@ module.exports = { ...@@ -3,15 +3,13 @@ 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 [ {
{ source: '/redirect/me/to-about/:lang',
source: '/redirect/me/to-about/:lang', destination: '/:lang/about',
destination: '/:lang/about', permanent: false,
permanent: false, },
}, ]
]
},
}, },
} }
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