Skip to content

Commit

Permalink
Add error message when rewriting to dynamic SSG page (#10458)
Browse files Browse the repository at this point in the history
* Add error message when rewriting to dynamic SSG page

* Update comment

Co-authored-by: Joe Haddad <timer150@gmail.com>
  • Loading branch information
ijjk and Timer committed Feb 8, 2020
1 parent f6e7a38 commit cbb9c2c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/next/next-server/server/render.tsx
Expand Up @@ -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

Expand All @@ -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 ' : ''
}`
)
}

Expand Down
27 changes: 27 additions & 0 deletions test/integration/prerender/test/index.test.js
Expand Up @@ -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()
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit cbb9c2c

Please sign in to comment.