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

fix: Dynamic Usage Error when using previewData with generateStaticParams and appDir #43395

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
4 changes: 0 additions & 4 deletions packages/next/client/components/headers.ts
Expand Up @@ -16,10 +16,6 @@ export function headers() {
}

export function previewData() {
if (staticGenerationBailout('previewData')) {
return {}
}

const requestStore =
requestAsyncStorage && 'getStore' in requestAsyncStorage
? requestAsyncStorage.getStore()!
Expand Down
38 changes: 38 additions & 0 deletions test/e2e/app-dir/app-static.test.ts
Expand Up @@ -66,6 +66,13 @@ describe('app-dir static/dynamic handling', () => {
'hooks/use-pathname/slug.html',
'hooks/use-pathname/slug.rsc',
'hooks/use-search-params/[slug]/page.js',
'ssg-preview.html',
'ssg-preview.rsc',
'ssg-preview/[[...route]]/page.js',
'ssg-preview/test-2.html',
'ssg-preview/test-2.rsc',
'ssg-preview/test.html',
'ssg-preview/test.rsc',
'ssr-auto/cache-no-store/page.js',
'ssr-auto/fetch-revalidate-zero/page.js',
'ssr-forced/page.js',
Expand Down Expand Up @@ -145,6 +152,21 @@ describe('app-dir static/dynamic handling', () => {
initialRevalidateSeconds: false,
srcRoute: '/force-static/[slug]',
},
'/ssg-preview': {
dataRoute: '/ssg-preview.rsc',
initialRevalidateSeconds: false,
srcRoute: '/ssg-preview/[[...route]]',
},
'/ssg-preview/test': {
dataRoute: '/ssg-preview/test.rsc',
initialRevalidateSeconds: false,
srcRoute: '/ssg-preview/[[...route]]',
},
'/ssg-preview/test-2': {
dataRoute: '/ssg-preview/test-2.rsc',
initialRevalidateSeconds: false,
srcRoute: '/ssg-preview/[[...route]]',
},
})
expect(manifest.dynamicRoutes).toEqual({
'/blog/[author]/[slug]': {
Expand All @@ -171,10 +193,26 @@ describe('app-dir static/dynamic handling', () => {
fallback: null,
routeRegex: '^\\/force\\-static\\/([^\\/]+?)(?:\\/)?$',
},
'/ssg-preview/[[...route]]': {
dataRoute: '/ssg-preview/[[...route]].rsc',
dataRouteRegex: '^\\/ssg\\-preview(?:\\/(.+?))?\\.rsc$',
fallback: null,
routeRegex: '^\\/ssg\\-preview(?:\\/(.+?))?(?:\\/)?$',
},
})
})
}

it('Should not throw Dynamic Server Usage error when using generateStaticParams with previewData', async () => {
const browserOnIndexPage = await webdriver(next.url, '/ssg-preview')

const content = await browserOnIndexPage
.elementByCss('#preview-data')
.text()

expect(content).toContain('previewData')
})

it('should force SSR correctly for headers usage', async () => {
const res = await fetchViaHTTP(next.url, '/force-static', undefined, {
headers: {
Expand Down
@@ -0,0 +1,29 @@
import { previewData } from 'next/headers'

export default function Page() {
const previewDataResult = previewData()

return (
<main>
<pre id="preview-data">
{JSON.stringify({ previewData: previewDataResult })}
</pre>
</main>
)
}

export const generateStaticParams = async () => {
const paths = [
{
route: [],
},
{
route: ['test'],
},
{
route: ['test-2'],
},
]

return paths
}