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
  • Loading branch information
brvnonascimento committed Nov 25, 2022
1 parent c4b6bb8 commit ce52d11
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 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
30 changes: 30 additions & 0 deletions test/e2e/app-dir/app-preview-static.test.ts
@@ -0,0 +1,30 @@
import { NextInstance } from 'test/lib/next-modes/base'
import { createNext, FileRef } from 'e2e-utils'
import path from 'path'
import webdriver from 'next-webdriver'

describe('app-dir previewData with SSG', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}

let next: NextInstance

afterAll(() => next.destroy())
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'app-preview-static')),
})
})

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

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

expect(content).toContain('previewData')
})
})
29 changes: 29 additions & 0 deletions test/e2e/app-dir/app-preview-static/app/[[...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
}
@@ -1,9 +1,10 @@
export default function Layout({ children }) {
export default function RootLayout({ children }) {
return (
<html lang="en">
<head>
<title>my static blog</title>
<title>static with previewData</title>
</head>

<body>{children}</body>
</html>
)
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/app-dir/app-preview-static/next.config.js
@@ -0,0 +1,7 @@
const nextConfig = {
experimental: {
appDir: true,
},
}

module.exports = nextConfig

0 comments on commit ce52d11

Please sign in to comment.