diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index 5227476196bf77f..50d5326a7954bc4 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -297,6 +297,7 @@ export async function renderToHTML( const bodyTags = (...args: any) => callMiddleware('bodyTags', args) const htmlProps = (...args: any) => callMiddleware('htmlProps', args, true) + const didRewrite = (req as any)._nextDidRewrite const isFallback = !!query.__nextFallback delete query.__nextFallback @@ -314,15 +315,21 @@ export async function renderToHTML( if ( process.env.NODE_ENV !== 'production' && - isAutoExport && + (isAutoExport || isFallback) && isDynamicRoute(pathname) && - (req as any)._nextDidRewrite + didRewrite ) { // TODO: add err.sh when rewrites go stable - // Behavior might change before then (prefer SSR in this case) + // 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. 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` + `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${ + isFallback ? ' for the fallback page ' : '' + }` ) } diff --git a/test/integration/prerender/test/index.test.js b/test/integration/prerender/test/index.test.js index 65aefa6a3adafa7..c2d98dbf756950e 100644 --- a/test/integration/prerender/test/index.test.js +++ b/test/integration/prerender/test/index.test.js @@ -378,6 +378,14 @@ const runTests = (dev = false) => { }) if (dev) { + it('should show error when rewriting to dynamic SSG page', async () => { + 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 for the fallback page` + ) + }) + it('should always call getStaticProps without caching in dev', async () => { const initialRes = await fetchViaHTTP(appPort, '/something') expect(initialRes.headers.get('cache-control')).toBeFalsy() @@ -595,8 +603,27 @@ const runTests = (dev = false) => { } describe('SPR Prerender', () => { + afterAll(() => fs.remove(nextConfig)) + describe('dev mode', () => { beforeAll(async () => { + await fs.writeFile( + nextConfig, + ` + module.exports = { + experimental: { + rewrites() { + return [ + { + source: "/some-rewrite/:item", + destination: "/blog/post-:item" + } + ] + } + } + } + ` + ) appPort = await findPort() app = await launchApp(appDir, appPort, { onStderr: msg => {