Unverified Commit 02d020c4 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

actions: fill prefetchCache with revalidation payload (#49576)

This fixes a bug where the revalidation value was not stored into the
prefetch cache, leading you to potentially see stale value when
re-reading from the prefetch cache.

<!-- 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(s) that you're making:

## For Contributors

### Improving Documentation or adding/fixing Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

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



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

link NEXT-1134
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent 1f561a67
...@@ -103,6 +103,7 @@ export function serverActionReducer( ...@@ -103,6 +103,7 @@ export function serverActionReducer(
if (!action.mutable.inFlightServerAction) { if (!action.mutable.inFlightServerAction) {
action.mutable.previousTree = state.tree action.mutable.previousTree = state.tree
action.mutable.previousUrl = state.canonicalUrl
action.mutable.inFlightServerAction = createRecordFromThenable( action.mutable.inFlightServerAction = createRecordFromThenable(
fetchServerAction(state, action) fetchServerAction(state, action)
) )
...@@ -117,11 +118,8 @@ export function serverActionReducer( ...@@ -117,11 +118,8 @@ export function serverActionReducer(
if (redirectLocation) { if (redirectLocation) {
// the redirection might have a flight data associated with it, so we'll populate the cache with it // the redirection might have a flight data associated with it, so we'll populate the cache with it
if (actionFlightData) { if (actionFlightData) {
const href = createHrefFromUrl( const href = createHrefFromUrl(redirectLocation, false)
redirectLocation, const previousCacheEntry = state.prefetchCache.get(href)
// Ensures the hash is not part of the cache key as it does not affect fetching the server
false
)
state.prefetchCache.set(href, { state.prefetchCache.set(href, {
data: createRecordFromThenable( data: createRecordFromThenable(
Promise.resolve([ Promise.resolve([
...@@ -130,7 +128,7 @@ export function serverActionReducer( ...@@ -130,7 +128,7 @@ export function serverActionReducer(
undefined, undefined,
]) ])
), ),
kind: PrefetchKind.TEMPORARY, //TODO-APP: maybe this could cached longer? kind: previousCacheEntry?.kind ?? PrefetchKind.TEMPORARY,
prefetchTime: Date.now(), prefetchTime: Date.now(),
treeAtTimeOfPrefetch: action.mutable.previousTree!, treeAtTimeOfPrefetch: action.mutable.previousTree!,
lastUsedTime: null, lastUsedTime: null,
...@@ -142,8 +140,31 @@ export function serverActionReducer( ...@@ -142,8 +140,31 @@ export function serverActionReducer(
getRedirectError(redirectLocation.toString(), RedirectType.push) getRedirectError(redirectLocation.toString(), RedirectType.push)
) )
} else { } else {
// TODO-APP: populate the prefetch cache with the new flight data
if (actionFlightData) { if (actionFlightData) {
const href = createHrefFromUrl(
new URL(action.mutable.previousUrl!, window.location.origin),
false
)
const previousCacheEntry = state.prefetchCache.get(href)
state.prefetchCache.set(
createHrefFromUrl(
new URL(action.mutable.previousUrl!, window.location.origin),
false
),
{
data: createRecordFromThenable(
Promise.resolve([
actionFlightData,
// TODO-APP: verify the logic around canonical URL overrides
undefined,
])
),
kind: previousCacheEntry?.kind ?? PrefetchKind.TEMPORARY,
prefetchTime: Date.now(),
treeAtTimeOfPrefetch: action.mutable.previousTree!,
lastUsedTime: null,
}
)
// this is an intentional hack around React: we want to update the tree in a new render // this is an intentional hack around React: we want to update the tree in a new render
setTimeout(() => { setTimeout(() => {
action.changeByServerResponse( action.changeByServerResponse(
......
...@@ -42,6 +42,7 @@ export interface ServerActionMutable { ...@@ -42,6 +42,7 @@ export interface ServerActionMutable {
inFlightServerAction?: Promise<any> | null inFlightServerAction?: Promise<any> | null
serverActionApplied?: boolean serverActionApplied?: boolean
previousTree?: FlightRouterState previousTree?: FlightRouterState
previousUrl?: string
} }
/** /**
......
...@@ -431,6 +431,32 @@ createNextDescribe( ...@@ -431,6 +431,32 @@ createNextDescribe(
}, 'success') }, 'success')
}) })
it('should store revalidation data in the prefetch cache', async () => {
const browser = await next.browser('/client')
await browser.elementByCss('#navigate-revalidate').click()
const justPutIt = await browser.elementByCss('#justputit').text()
await browser.elementByCss('#revalidate-justputit').click()
await check(async () => {
const newJustPutIt = await browser.elementByCss('#justputit').text()
return newJustPutIt !== justPutIt ? 'success' : 'failure'
}, 'success')
const newJustPutIt = await browser.elementByCss('#justputit').text()
await browser
.elementByCss('#navigate-client')
.click()
.waitForElementByCss('#inc')
await browser
.elementByCss('#navigate-revalidate')
.click()
.waitForElementByCss('#revalidate-justputit')
const newJustPutIt2 = await browser.elementByCss('#justputit').text()
expect(newJustPutIt).toEqual(newJustPutIt2)
})
it('should revalidate when cookies.set is called', async () => { it('should revalidate when cookies.set is called', async () => {
const browser = await next.browser('/revalidate') const browser = await next.browser('/revalidate')
const randomNumber = await browser.elementByCss('#random-cookie').text() const randomNumber = await browser.elementByCss('#random-cookie').text()
......
import Link from 'next/link'
export default function RootLayout({ children }) { export default function RootLayout({ children }) {
return ( return (
<html> <html>
<head /> <head />
<body> <body>
<div id="random-number">{Math.random()}</div> <div id="random-number">{Math.random()}</div>
<div>
<div>
<Link id="navigate-client" href="/client">
Client
</Link>
</div>
<div>
<Link id="navigate-server" href="/server">
Server
</Link>
</div>
<div>
<Link id="navigate-revalidate" href="/revalidate">
Client and Server
</Link>
</div>
</div>
{children} {children}
</body> </body>
</html> </html>
......
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