Unverified Commit d12493b7 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

bugfix: propagate staleTime to seeded prefetch entry (#81263) (#81266)

Backports:
- #81263
parent 632d4abc
...@@ -4,7 +4,10 @@ import type { FlightDataPath } from '../../../server/app-render/types' ...@@ -4,7 +4,10 @@ import type { FlightDataPath } from '../../../server/app-render/types'
import { createHrefFromUrl } from './create-href-from-url' import { createHrefFromUrl } from './create-href-from-url'
import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head' import { fillLazyItemsTillLeafWithHead } from './fill-lazy-items-till-leaf-with-head'
import { extractPathFromFlightRouterState } from './compute-changed-path' import { extractPathFromFlightRouterState } from './compute-changed-path'
import { createSeededPrefetchCacheEntry } from './prefetch-cache-utils' import {
createSeededPrefetchCacheEntry,
STATIC_STALETIME_MS,
} from './prefetch-cache-utils'
import { PrefetchKind, type PrefetchCacheEntry } from './router-reducer-types' import { PrefetchKind, type PrefetchCacheEntry } from './router-reducer-types'
import { addRefreshMarkerToActiveParallelSegments } from './refetch-inactive-parallel-segments' import { addRefreshMarkerToActiveParallelSegments } from './refetch-inactive-parallel-segments'
import { getFlightDataPartsFromPath } from '../../flight-data-helpers' import { getFlightDataPartsFromPath } from '../../flight-data-helpers'
...@@ -132,7 +135,13 @@ export function createInitialRouterState({ ...@@ -132,7 +135,13 @@ export function createInitialRouterState({
// add a way to split a single Flight stream into static and dynamic // add a way to split a single Flight stream into static and dynamic
// parts. But in the meantime we should at least make this work for // parts. But in the meantime we should at least make this work for
// fully static pages. // fully static pages.
staleTime: -1, staleTime:
// In the old router, there was only a single configurable staleTime (experimental.staleTimes)
// As an abundance of caution, this will only set the initial staleTime to the configured value
// if we're not leveraging the segment cache, which has its own prefetching semantics.
prerendered && !process.env.__NEXT_CLIENT_SEGMENT_CACHE
? STATIC_STALETIME_MS
: -1,
}, },
tree: initialState.tree, tree: initialState.tree,
prefetchCache: initialState.prefetchCache, prefetchCache: initialState.prefetchCache,
......
...@@ -291,7 +291,7 @@ export function createSeededPrefetchCacheEntry({ ...@@ -291,7 +291,7 @@ export function createSeededPrefetchCacheEntry({
kind, kind,
prefetchTime: Date.now(), prefetchTime: Date.now(),
lastUsedTime: Date.now(), lastUsedTime: Date.now(),
staleTime: -1, staleTime: data.staleTime,
key: prefetchCacheKey, key: prefetchCacheKey,
status: PrefetchCacheEntryStatus.fresh, status: PrefetchCacheEntryStatus.fresh,
url, url,
......
...@@ -8,6 +8,9 @@ export default function HomePage() { ...@@ -8,6 +8,9 @@ export default function HomePage() {
<Link href="/static-page" id="to-static-page"> <Link href="/static-page" id="to-static-page">
To Static Page To Static Page
</Link> </Link>
<Link href="/static-page-no-prefetch" id="to-static-page-no-prefetch">
To Static Page No Prefetch
</Link>
<Link href="/dynamic-page" id="to-dynamic-page-no-params"> <Link href="/dynamic-page" id="to-dynamic-page-no-params">
To Dynamic Page To Dynamic Page
</Link> </Link>
......
import Link from 'next/link'
export default async function Page() {
return (
<>
<p id="static-page-no-prefetch">Static Page No Prefetch</p>
<p>
<Link href="/" id="to-home">
To home
</Link>
</p>
</>
)
}
...@@ -8,8 +8,8 @@ describe('app dir - prefetching (custom staleTime)', () => { ...@@ -8,8 +8,8 @@ describe('app dir - prefetching (custom staleTime)', () => {
nextConfig: { nextConfig: {
experimental: { experimental: {
staleTimes: { staleTimes: {
static: 30, static: 10,
dynamic: 20, dynamic: 5,
}, },
}, },
}, },
...@@ -86,7 +86,7 @@ describe('app dir - prefetching (custom staleTime)', () => { ...@@ -86,7 +86,7 @@ describe('app dir - prefetching (custom staleTime)', () => {
.waitForElementByCss('#to-static-page') .waitForElementByCss('#to-static-page')
// Wait for the stale time to pass. // Wait for the stale time to pass.
await waitFor(30000) await waitFor(10000)
// Click on the link to the static page again // Click on the link to the static page again
await linkToStaticPage.click() await linkToStaticPage.click()
// Wait for the static page to load again // Wait for the static page to load again
...@@ -131,4 +131,37 @@ describe('app dir - prefetching (custom staleTime)', () => { ...@@ -131,4 +131,37 @@ describe('app dir - prefetching (custom staleTime)', () => {
// the initial fetch, 2 sub-page fetches, and a final fetch when reloading the page // the initial fetch, 2 sub-page fetches, and a final fetch when reloading the page
expect(await browser.elementById('count').text()).toBe('4') expect(await browser.elementById('count').text()).toBe('4')
}) })
it('should fetch again when the initially visited static page is visited after the stale time has passed', async () => {
const browser = await next.browser('/404')
let requests: string[] = []
browser.on('request', (req) => {
// only consider requests that have RSC header but not the prefetch header
if (req.headers()['rsc'] && !req.headers()['next-router-prefetch']) {
requests.push(new URL(req.url()).pathname)
}
})
await browser.eval('location.href = "/static-page-no-prefetch"')
await browser
.elementByCss('#to-home')
.click()
.waitForElementByCss('#to-static-page')
// Wait for the stale time to pass.
await waitFor(10000)
await browser.elementByCss('#to-static-page-no-prefetch').click()
// Wait for the static page to load again
await browser.waitForElementByCss('#static-page-no-prefetch')
await retry(async () => {
expect(
requests.filter((request) => request === '/static-page-no-prefetch')
).toHaveLength(1)
})
})
}) })
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