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

Prevent storing page props cache when in SSG + preview mode #30757

Merged
merged 5 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ export default class Router implements BaseRouter {
this.isSsr,
false,
__N_SSG ? this.sdc : this.sdr,
!!__N_SSG
!!__N_SSG && !this.isPreview
)
: this.getInitialProps(
Component,
Expand Down
20 changes: 18 additions & 2 deletions test/integration/prerender-preview/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
import { useState } from 'react'
import { useRouter } from 'next/router'

export function getStaticProps({ preview, previewData }) {
return {
props: {
hasProps: true,
random: Math.random(),
preview: !!preview,
previewData: previewData || null,
},
}
}

export default function ({ hasProps, preview, previewData }) {
export default function ({ hasProps, preview, previewData, random }) {
const router = useRouter()
const [reloaded, setReloaded] = useState(false)

return (
<>
<pre id="props-pre">
{hasProps
? JSON.stringify(preview) + ' and ' + JSON.stringify(previewData)
: 'Has No Props'}
</pre>
<p id="router">{JSON.stringify(useRouter())}</p>
<pre id="ssg-random">{random}</pre>
{reloaded ? <pre id="ssg-reloaded">Reloaded</pre> : null}
<button
id="reload-props"
onClick={async () => {
await router.replace(router.asPath)
setReloaded(true)
}}
>
Reload static props
</button>
<p id="router">{JSON.stringify(router)}</p>
</>
)
}
18 changes: 18 additions & 0 deletions test/integration/prerender-preview/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,24 @@ describe('Prerender Preview Mode', () => {
)
})

it('should fetch live static props with preview active', async () => {
await browser.get(`http://localhost:${appPort}/`)

await browser.waitForElementByCss('#ssg-random')
const initialRandom = await browser.elementById('ssg-random').text()

// reload static props with router.replace
await browser.elementById('reload-props').click()

// wait for route change to complete and set updated state
await browser.waitForElementByCss('#ssg-reloaded')

// assert that the random number from static props has changed (thus, was re-evaluated)
expect(await browser.elementById('ssg-random').text()).not.toBe(
initialRandom
)
})

afterAll(async () => {
await browser.close()
await killApp(app)
Expand Down