Skip to content

Commit

Permalink
fix: Dynamic Usage Error when using previewData with generateStaticPa…
Browse files Browse the repository at this point in the history
…rams 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: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
brvnonascimento and ijjk committed Dec 1, 2022
1 parent 30c9627 commit 1c89da6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
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
29 changes: 29 additions & 0 deletions test/e2e/app-dir/app-static/app/ssg-preview/[[...route]]/page.js
@@ -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
}

0 comments on commit 1c89da6

Please sign in to comment.