Unverified Commit 1c89da60 authored by Bruno Nascimento's avatar Bruno Nascimento Committed by GitHub

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

fixes #43392 

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [] Related issues linked using `fixes #number`
- []
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 30c96271
...@@ -16,10 +16,6 @@ export function headers() { ...@@ -16,10 +16,6 @@ export function headers() {
} }
export function previewData() { export function previewData() {
if (staticGenerationBailout('previewData')) {
return {}
}
const requestStore = const requestStore =
requestAsyncStorage && 'getStore' in requestAsyncStorage requestAsyncStorage && 'getStore' in requestAsyncStorage
? requestAsyncStorage.getStore()! ? requestAsyncStorage.getStore()!
......
...@@ -66,6 +66,13 @@ describe('app-dir static/dynamic handling', () => { ...@@ -66,6 +66,13 @@ describe('app-dir static/dynamic handling', () => {
'hooks/use-pathname/slug.html', 'hooks/use-pathname/slug.html',
'hooks/use-pathname/slug.rsc', 'hooks/use-pathname/slug.rsc',
'hooks/use-search-params/[slug]/page.js', '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/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',
...@@ -145,6 +152,21 @@ describe('app-dir static/dynamic handling', () => { ...@@ -145,6 +152,21 @@ describe('app-dir static/dynamic handling', () => {
initialRevalidateSeconds: false, initialRevalidateSeconds: false,
srcRoute: '/force-static/[slug]', 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({ expect(manifest.dynamicRoutes).toEqual({
'/blog/[author]/[slug]': { '/blog/[author]/[slug]': {
...@@ -171,10 +193,26 @@ describe('app-dir static/dynamic handling', () => { ...@@ -171,10 +193,26 @@ describe('app-dir static/dynamic handling', () => {
fallback: null, fallback: null,
routeRegex: '^\\/force\\-static\\/([^\\/]+?)(?:\\/)?$', 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 () => { it('should force SSR correctly for headers usage', async () => {
const res = await fetchViaHTTP(next.url, '/force-static', undefined, { const res = await fetchViaHTTP(next.url, '/force-static', undefined, {
headers: { headers: {
......
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
}
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