Unverified Commit 6b7c69fa authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add proper error when app path switches static to dynamic (#44989)

parent cefb30b3
# `app/` Static to Dynamic Error
#### Why This Error Occurred
Inside of one of your `app/` pages, the page was initially generated statically at build time and then during runtime either a fallback path or path being revalidated attempted to leverage dynamic server values e.g. `cookies()` or `headers()`.
This is a hard error by default as a path generated statically can switch between types during runtime currently.
#### Possible Ways to Fix It
Prevent usage of these dynamic server values conditionally which can cause the static/dynamic mode of the page to differ between build time and runtime.
Leverage `export const dynamic = 'force-static'` to ensure the page is handled statically regardless of the usage of dynamic server values. Alternatively if you prefer your page to allows be dynamic you can set `export const dynamic = 'force-dynamic'` and it won't attempt to have the page be statically generated.
### Useful Links
- [static/dynamic rendering](https://beta.nextjs.org/docs/rendering/static-and-dynamic-rendering)
- [dynamic server value methods](https://beta.nextjs.org/docs/data-fetching/fetching#server-component-functions)
...@@ -785,6 +785,10 @@ ...@@ -785,6 +785,10 @@
{ {
"title": "class-component-in-server-component", "title": "class-component-in-server-component",
"path": "/errors/class-component-in-server-component.md" "path": "/errors/class-component-in-server-component.md"
},
{
"title": "app-static-to-dynamic-error",
"path": "/errors/app-static-to-dynamic-error.md"
} }
] ]
} }
......
...@@ -1373,6 +1373,12 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1373,6 +1373,12 @@ export default abstract class Server<ServerOptions extends Options = Options> {
isNotFound = (renderOpts as any).isNotFound isNotFound = (renderOpts as any).isNotFound
isRedirect = (renderOpts as any).isRedirect isRedirect = (renderOpts as any).isRedirect
if (isAppPath && isSSG && isrRevalidate === 0) {
throw new Error(
`Page changed from static to dynamic at runtime ${urlPathname}, see more here https://nextjs.org/docs/messages/app-static-to-dynamic-error`
)
}
let value: ResponseCacheValue | null let value: ResponseCacheValue | null
if (isNotFound) { if (isNotFound) {
value = null value = null
......
...@@ -74,6 +74,12 @@ createNextDescribe( ...@@ -74,6 +74,12 @@ createNextDescribe(
'ssr-auto/cache-no-store/page.js', 'ssr-auto/cache-no-store/page.js',
'ssr-auto/fetch-revalidate-zero/page.js', 'ssr-auto/fetch-revalidate-zero/page.js',
'ssr-forced/page.js', 'ssr-forced/page.js',
'static-to-dynamic-error-forced/[id].html',
'static-to-dynamic-error-forced/[id].rsc',
'static-to-dynamic-error-forced/[id]/page.js',
'static-to-dynamic-error/[id].html',
'static-to-dynamic-error/[id].rsc',
'static-to-dynamic-error/[id]/page.js',
'variable-revalidate/no-store/page.js', 'variable-revalidate/no-store/page.js',
'variable-revalidate/revalidate-3.html', 'variable-revalidate/revalidate-3.html',
'variable-revalidate/revalidate-3.rsc', 'variable-revalidate/revalidate-3.rsc',
...@@ -215,26 +221,93 @@ createNextDescribe( ...@@ -215,26 +221,93 @@ createNextDescribe(
}, },
'/hooks/use-pathname/[slug]': { '/hooks/use-pathname/[slug]': {
dataRoute: '/hooks/use-pathname/[slug].rsc', dataRoute: '/hooks/use-pathname/[slug].rsc',
dataRouteRegex: '^\\/hooks\\/use\\-pathname\\/([^\\/]+?)\\.rsc$', dataRouteRegex: normalizeRegEx(
'^\\/hooks\\/use\\-pathname\\/([^\\/]+?)\\.rsc$'
),
fallback: null, fallback: null,
routeRegex: '^\\/hooks\\/use\\-pathname\\/([^\\/]+?)(?:\\/)?$', routeRegex: normalizeRegEx(
'^\\/hooks\\/use\\-pathname\\/([^\\/]+?)(?:\\/)?$'
),
}, },
'/force-static/[slug]': { '/force-static/[slug]': {
dataRoute: '/force-static/[slug].rsc', dataRoute: '/force-static/[slug].rsc',
dataRouteRegex: '^\\/force\\-static\\/([^\\/]+?)\\.rsc$', dataRouteRegex: normalizeRegEx(
'^\\/force\\-static\\/([^\\/]+?)\\.rsc$'
),
fallback: null, fallback: null,
routeRegex: '^\\/force\\-static\\/([^\\/]+?)(?:\\/)?$', routeRegex: normalizeRegEx(
'^\\/force\\-static\\/([^\\/]+?)(?:\\/)?$'
),
}, },
'/ssg-preview/[[...route]]': { '/ssg-preview/[[...route]]': {
dataRoute: '/ssg-preview/[[...route]].rsc', dataRoute: '/ssg-preview/[[...route]].rsc',
dataRouteRegex: '^\\/ssg\\-preview(?:\\/(.+?))?\\.rsc$', dataRouteRegex: normalizeRegEx(
'^\\/ssg\\-preview(?:\\/(.+?))?\\.rsc$'
),
fallback: null, fallback: null,
routeRegex: '^\\/ssg\\-preview(?:\\/(.+?))?(?:\\/)?$', routeRegex: normalizeRegEx(
'^\\/ssg\\-preview(?:\\/(.+?))?(?:\\/)?$'
),
},
'/static-to-dynamic-error-forced/[id]': {
dataRoute: '/static-to-dynamic-error-forced/[id].rsc',
dataRouteRegex: normalizeRegEx(
'^\\/static\\-to\\-dynamic\\-error\\-forced\\/([^\\/]+?)\\.rsc$'
),
fallback: null,
routeRegex: normalizeRegEx(
'^\\/static\\-to\\-dynamic\\-error\\-forced\\/([^\\/]+?)(?:\\/)?$'
),
},
'/static-to-dynamic-error/[id]': {
dataRoute: '/static-to-dynamic-error/[id].rsc',
dataRouteRegex: normalizeRegEx(
'^\\/static\\-to\\-dynamic\\-error\\/([^\\/]+?)\\.rsc$'
),
fallback: null,
routeRegex: normalizeRegEx(
'^\\/static\\-to\\-dynamic\\-error\\/([^\\/]+?)(?:\\/)?$'
),
}, },
}) })
}) })
} }
if (!isDev) {
it('should properly error when static page switches to dynamic at runtime', async () => {
const res = await next.fetch(
'/static-to-dynamic-error/static-bailout-1'
)
expect(res.status).toBe(500)
if (isNextStart) {
await check(
() => next.cliOutput,
/Page changed from static to dynamic at runtime \/static-to-dynamic-error\/static-bailout-1/
)
}
})
it('should not error with dynamic server usage with force-static', async () => {
const res = await next.fetch(
'/static-to-dynamic-error-forced/static-bailout-1'
)
const outputIndex = next.cliOutput.length
const html = await res.text()
expect(res.status).toBe(200)
expect(html).toContain('/static-to-dynamic-error-forced')
expect(html).toMatch(/id:.*?static-bailout-1/)
if (isNextStart) {
expect(next.cliOutput.substring(outputIndex)).not.toMatch(
/Page changed from static to dynamic at runtime \/static-to-dynamic-error\/static-bailout-1/
)
}
})
}
it('Should not throw Dynamic Server Usage error when using generateStaticParams with previewData', async () => { it('Should not throw Dynamic Server Usage error when using generateStaticParams with previewData', async () => {
const browserOnIndexPage = await next.browser('/ssg-preview') const browserOnIndexPage = await next.browser('/ssg-preview')
......
import { cookies } from 'next/headers'
export const dynamic = 'force-static'
export default function Page({ params }) {
if (params.id.includes('static-bailout')) {
console.log('calling cookies', cookies())
}
return (
<>
<p>/static-to-dynamic-error-forced</p>
<p>id: {params.id}</p>
<p id="now">{Date.now()}</p>
</>
)
}
import { cookies } from 'next/headers'
export default function Page({ params }) {
if (params.id.includes('static-bailout')) {
console.log('calling cookies', cookies())
}
return (
<>
<p>/static-to-dynamic-error</p>
<p>id: {params.id}</p>
<p id="now">{Date.now()}</p>
</>
)
}
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