-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent storing page props cache when in SSG + preview mode (#30757)
In Next 12, `router.replace()` treats the page props cache as authoritative when fetching SSG props in preview mode. This makes sense outside preview mode (static props won't change), but within preview mode this prevents reloading the preview data dynamically without force-refreshing the page. This PR fixes the problem by instructing Next to bypass the props cache if it's an SSG route AND preview mode is on. Repro: https://github.com/kamsar/next-replace-previewmode-staticprops/blob/main/pages/index.js ## Bug - [x] Related issues linked using `fixes #number`: Fixes #30756 - [ ] Integration tests added: Did not find existing tests for the router, but I'd be happy to write one if someone could point me in the right direction.
- Loading branch information
Showing
3 changed files
with
37 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters