Commit 39ccd8ff authored by JJ Kasper's avatar JJ Kasper Committed by Joe Haddad

Add error for rewrite to auto-exported dynamic page (#9816)

* Add error for rewrite to auto-exported dynamic page

* Update packages/next/next-server/server/render.tsx
Co-authored-by: 's avatarJoe Haddad <timer150@gmail.com>
parent 31f3bfa1
......@@ -463,6 +463,8 @@ export default class Server {
return {
finished: true,
}
} else {
;(_req as any)._nextDidRewrite = true
}
return {
......
......@@ -317,6 +317,20 @@ export async function renderToHTML(
const isAutoExport =
!hasPageGetInitialProps && defaultAppGetInitialProps && !isSpr
if (
process.env.NODE_ENV !== 'production' &&
isAutoExport &&
isDynamicRoute(pathname) &&
(req as any)._nextDidRewrite
) {
// TODO: add err.sh when rewrites go stable
// Behavior might change before then (prefer SSR in this case)
throw new Error(
`Rewrites don't support auto-exported dynamic pages yet. ` +
`Using this will cause the page to fail to parse the params on the client`
)
}
if (hasPageGetInitialProps && isSpr) {
throw new Error(SPR_GET_INITIAL_PROPS_CONFLICT + ` ${pathname}`)
}
......
......@@ -3,6 +3,10 @@ module.exports = {
experimental: {
async rewrites() {
return [
{
source: '/to-another',
destination: '/another/one',
},
{
source: '/hello-world',
destination: '/static/hello.txt',
......
......@@ -193,7 +193,7 @@ const runTests = (isDev = false) => {
join(appDir, '.next/routes-manifest.json')
)
for (const route of manifest.dynamicRoutes) {
for (const route of [...manifest.dynamicRoutes, ...manifest.rewrites]) {
route.regex = normalizeRegEx(route.regex)
}
......@@ -287,6 +287,12 @@ const runTests = (isDev = false) => {
},
],
rewrites: [
{
destination: '/another/one',
regex: normalizeRegEx('^\\/to-another$'),
regexKeys: [],
source: '/to-another',
},
{
source: '/hello-world',
destination: '/static/hello.txt',
......@@ -359,9 +365,13 @@ const runTests = (isDev = false) => {
},
],
dynamicRoutes: [
{
page: '/another/[id]',
regex: normalizeRegEx('^\\/another\\/([^\\/]+?)(?:\\/)?$'),
},
{
page: '/blog/[post]',
regex: normalizeRegEx('^\\/blog\\/([^/]+?)(?:\\/)?$'),
regex: normalizeRegEx('^\\/blog\\/([^\\/]+?)(?:\\/)?$'),
},
],
})
......@@ -382,6 +392,13 @@ const runTests = (isDev = false) => {
)
}
})
} else {
it('should show error for dynamic auto export rewrite', async () => {
const html = await renderViaHTTP(appPort, '/to-another')
expect(html).toContain(
`Rewrites don't support auto-exported dynamic pages yet`
)
})
}
}
......
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