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.
```js
// next.config.js
module.exports = {
experimental: {
async rewrites() {
return {
source: '/feedback',
destination: '/feedback/general',
}
},
async rewrites() {
return {
source: '/feedback',
destination: '/feedback/general',
}
},
}
```
......@@ -28,15 +26,13 @@ module.exports = {
```js
module.exports = {
experimental: {
async rewrites() {
return [
{
source: '/feedback',
destination: '/feedback/general',
},
]
},
async rewrites() {
return [
{
source: '/feedback',
destination: '/feedback/general',
},
]
},
}
```
module.exports = {
experimental: {
async rewrites() {
return [
// we need to define a no-op rewrite to trigger checking
// all pages/static files before we attempt proxying
{
source: '/:path*',
destination: '/:path*',
},
{
source: '/:path*',
destination: `https://custom-routes-proxying-endpoint.vercel.app/:path*`,
},
]
},
async rewrites() {
return [
// we need to define a no-op rewrite to trigger checking
// all pages/static files before we attempt proxying
{
source: '/:path*',
destination: '/:path*',
},
{
source: '/:path*',
destination: `https://custom-routes-proxying-endpoint.vercel.app/:path*`,
},
]
},
}
......@@ -18,14 +18,12 @@ module.exports = withOffline({
},
],
},
experimental: {
async rewrites() {
return [
{
source: '/service-worker.js',
destination: '/_next/static/service-worker.js',
},
]
},
async rewrites() {
return [
{
source: '/service-worker.js',
destination: '/_next/static/service-worker.js',
},
]
},
})
......@@ -310,28 +310,28 @@ export interface CustomRoutes {
}
async function loadRedirects(config: any) {
if (typeof config.experimental.redirects !== 'function') {
if (typeof config.redirects !== 'function') {
return []
}
const _redirects = await config.experimental.redirects()
const _redirects = await config.redirects()
checkCustomRoutes(_redirects, 'redirect')
return _redirects
}
async function loadRewrites(config: any) {
if (typeof config.experimental.rewrites !== 'function') {
if (typeof config.rewrites !== 'function') {
return []
}
const _rewrites = await config.experimental.rewrites()
const _rewrites = await config.rewrites()
checkCustomRoutes(_rewrites, 'rewrite')
return _rewrites
}
async function loadHeaders(config: any) {
if (typeof config.experimental.headers !== 'function') {
if (typeof config.headers !== 'function') {
return []
}
const _headers = await config.experimental.headers()
const _headers = await config.headers()
checkCustomRoutes(_headers, 'header')
return _headers
}
......
......@@ -341,15 +341,14 @@ export async function renderToHTML(
pageIsDynamic &&
didRewrite
) {
// TODO: add err.sh when rewrites go stable
// Behavior might change before then (prefer SSR in this case).
// If we decide to ship rewrites to the client we could solve this
// by running over the rewrites and getting the params.
// TODO: 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(
`Rewrites don't support${
isFallback ? ' ' : ' auto-exported '
}dynamic pages${isFallback ? ' with getStaticProps ' : ' '}yet. ` +
`Using this will cause the page to fail to parse the params on the client`
}dynamic pages${isFallback ? ' with getStaticProps ' : ' '}yet.\n` +
`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 = {
experimental: {
rewrites() {
return [
{
source: '/docs/:path*',
destination: '/:path*',
},
]
},
rewrites() {
return [
{
source: '/docs/:path*',
destination: '/:path*',
},
]
},
}
......@@ -19,6 +19,11 @@ let appPort
let app
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 () => {
const bundlePath = await join(
'/docs/_next/static/',
......@@ -29,11 +34,6 @@ const runTests = () => {
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 () => {
const data = await renderViaHTTP(appPort, '/docs/static/data.json')
expect(data).toContain('some data...')
......
module.exports = {
// update me
experimental: {
async redirects() {
return [
{
source: '/hello',
permanent: false,
destination: `/${process.env.NEXT_PUBLIC_TEST_DEST}`,
},
]
},
async redirects() {
return [
{
source: '/hello',
permanent: false,
destination: `/${process.env.NEXT_PUBLIC_TEST_DEST}`,
},
]
},
}
......@@ -14,10 +14,8 @@ const writeConfig = async (routes, type = 'redirects') => {
nextConfigPath,
`
module.exports = {
experimental: {
async ${type}() {
return ${JSON.stringify(routes)}
}
async ${type}() {
return ${JSON.stringify(routes)}
}
}
`
......
module.exports = {
experimental: {
rewrites() {
return [
{
source: '/:hello*',
destination: '/:hello',
},
]
},
rewrites() {
return [
{
source: '/:hello*',
destination: '/:hello',
},
]
},
}
module.exports = {
experimental: {
optionalCatchAll: true,
rewrites() {
return [
{
source: '/about',
destination: '/lang/en/about',
},
]
},
},
rewrites() {
return [
{
source: '/about',
destination: '/lang/en/about',
},
]
},
}
......@@ -608,7 +608,7 @@ const runTests = (dev = false, isEmulatedServerless = false) => {
const item = Math.round(Math.random() * 100)
const html = await renderViaHTTP(appPort, `/some-rewrite/${item}`)
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', () => {
module.exports = {
experimental: {
optionalCatchAll: true,
rewrites() {
return [
{
source: "/some-rewrite/:item",
destination: "/blog/post-:item"
},
{
source: '/about',
destination: '/lang/en/about'
}
]
}
},
rewrites() {
return [
{
source: "/some-rewrite/:item",
destination: "/blog/post-:item"
},
{
source: '/about',
destination: '/lang/en/about'
}
]
}
}
`
......@@ -1401,14 +1401,14 @@ describe('SSG Prerender', () => {
target: 'serverless',
experimental: {
optionalCatchAll: true,
rewrites() {
return [
{
source: '/about',
destination: '/lang/en/about'
}
]
}
},
rewrites() {
return [
{
source: '/about',
destination: '/lang/en/about'
}
]
}
}`,
'utf8'
......
......@@ -3,15 +3,13 @@ module.exports = {
// Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60,
},
experimental: {
redirects() {
return [
{
source: '/redirect/me/to-about/:lang',
destination: '/:lang/about',
permanent: false,
},
]
},
redirects() {
return [
{
source: '/redirect/me/to-about/:lang',
destination: '/:lang/about',
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