Skip to content

Commit cbb9c2c

Browse files
ijjkTimer
andauthored
Add error message when rewriting to dynamic SSG page (vercel#10458)
* Add error message when rewriting to dynamic SSG page * Update comment Co-authored-by: Joe Haddad <timer150@gmail.com>
1 parent f6e7a38 commit cbb9c2c

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

packages/next/next-server/server/render.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ export async function renderToHTML(
297297
const bodyTags = (...args: any) => callMiddleware('bodyTags', args)
298298
const htmlProps = (...args: any) => callMiddleware('htmlProps', args, true)
299299

300+
const didRewrite = (req as any)._nextDidRewrite
300301
const isFallback = !!query.__nextFallback
301302
delete query.__nextFallback
302303

@@ -314,15 +315,21 @@ export async function renderToHTML(
314315

315316
if (
316317
process.env.NODE_ENV !== 'production' &&
317-
isAutoExport &&
318+
(isAutoExport || isFallback) &&
318319
isDynamicRoute(pathname) &&
319-
(req as any)._nextDidRewrite
320+
didRewrite
320321
) {
321322
// TODO: add err.sh when rewrites go stable
322-
// Behavior might change before then (prefer SSR in this case)
323+
// Behavior might change before then (prefer SSR in this case).
324+
// If we decide to ship rewrites to the client we could solve this
325+
// by running over the rewrites and getting the params.
323326
throw new Error(
324-
`Rewrites don't support auto-exported dynamic pages yet. ` +
325-
`Using this will cause the page to fail to parse the params on the client`
327+
`Rewrites don't support${
328+
isFallback ? ' ' : ' auto-exported '
329+
}dynamic pages${isFallback ? ' with getStaticProps ' : ' '}yet. ` +
330+
`Using this will cause the page to fail to parse the params on the client${
331+
isFallback ? ' for the fallback page ' : ''
332+
}`
326333
)
327334
}
328335

test/integration/prerender/test/index.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,14 @@ const runTests = (dev = false) => {
378378
})
379379

380380
if (dev) {
381+
it('should show error when rewriting to dynamic SSG page', async () => {
382+
const item = Math.round(Math.random() * 100)
383+
const html = await renderViaHTTP(appPort, `/some-rewrite/${item}`)
384+
expect(html).toContain(
385+
`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`
386+
)
387+
})
388+
381389
it('should always call getStaticProps without caching in dev', async () => {
382390
const initialRes = await fetchViaHTTP(appPort, '/something')
383391
expect(initialRes.headers.get('cache-control')).toBeFalsy()
@@ -595,8 +603,27 @@ const runTests = (dev = false) => {
595603
}
596604

597605
describe('SPR Prerender', () => {
606+
afterAll(() => fs.remove(nextConfig))
607+
598608
describe('dev mode', () => {
599609
beforeAll(async () => {
610+
await fs.writeFile(
611+
nextConfig,
612+
`
613+
module.exports = {
614+
experimental: {
615+
rewrites() {
616+
return [
617+
{
618+
source: "/some-rewrite/:item",
619+
destination: "/blog/post-:item"
620+
}
621+
]
622+
}
623+
}
624+
}
625+
`
626+
)
600627
appPort = await findPort()
601628
app = await launchApp(appDir, appPort, {
602629
onStderr: msg => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy