Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error message when rewriting to dynamic SSG page #10458

Merged
merged 3 commits into from Feb 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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