Unverified Commit 99083281 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[PPF] Fix unnecessary runtime prefetch with prefetch={true} (#98651)

In, we #98278 attempted to fix runtime follow-ups for shells (if a
static prefetch produced an insuffient shell).

However, it introduced a regression in this scenario, where neither
shells or prefetches use runtime data, so both of these links should use
static prefetches:
```tsx
<Link href="/static-param/one" prefetch={true} />
{/* Revealed later */}
<Link href="/static-param/two" prefetch={true} />
```

We'd correctly do a static prefetch for the first link. However, the
second link would see a `RuntimeShell`-tier shell from the first link,
and this check inside `isShellEntryEligibleForStaticAttempt` would fail
(because `RuntimeShell` was now greater than `PPR`):

https://github.com/vercel/next.js/blob/3c9d1ca77f7de845315dc166a57fc9f090c638b4/packages/next/src/client/components/segment-cache/scheduler.ts#L1261
as a result, `isShellEntryEligibleForStaticAttempt` would return
`false`, and we would deopt to a runtime prefetch for the second link
instead.

Importantly, a page with `ensureStatic = "prefetch"`(which will be
implemented in #98191) will currently be handled exactly like a page
that did not use runtime data at build -- the client doesn't know that a
page is meant to never use runtime requests at all, it's entirely based
on the existing static hints/`needsRuntimeRequest` promise mechanism. So
even if `/static/[slug]` sets `ensureStatic = "prefetch"`, we'd also do
an unnecessary runtime prefetch for it even though we really shouldn't.
Violating the `ensureStatic` contract seems worse than a missing runtime
shell follow-up , because those can currently only happen if the shell
starts using runtime data after a revalidation, and that's probably not
a common scenario. So i'm partially reverting that change until we
figure out how to handle this in a way that handles both cases correctly
(i've attempted doing that, but it seems nontrivial)

### Testing

We're now missing the runtime shell follow-up again, so tests related to
that in `prefetch-app-shell-revalidation.test.ts` are marked as failing
(but still assert on current behavior -- the follow-up failures can be
reproduced by setting `REPRODUCE_MISSING_RUNTIME_SHELL_FOLLOW_UP=1`).
Note that the runtime *prefetch* follow ups there work as expected --
those worked correctly even before #98278.

I've also added some pretty comprehensive tests in
`prefetch-static-shell.test.ts`. We now test the scenario that
regressed, which we weren't doing before. We also test scenarios where
we reveal multiple links, but some of them use runtime data in the
prefetch, and some don't. There's some failures there related to the
fact that we don't track runtime data accesses in shells and prefetches
separately (revealed by `REPRODUCE_UNNECESSARY_RUNTIME_SHELL=1`), which
will be addressed by #98129. I want to land that before doing anything
else on this topic to avoid dealing with failures that are going to be
fixed soon anyway. There's also one case
(`REPRODUCE_UNNECESSARY_RUNTIME_PREFETCH=1`) which will not be fixed by
it, and will need separate consideration.
parent 7d42c9dc
...@@ -3848,10 +3848,10 @@ function addSegmentPathToUrlInOutputExportMode( ...@@ -3848,10 +3848,10 @@ function addSegmentPathToUrlInOutputExportMode(
* - `StaticShell` provides the shell-stage variant extracted from a static response — * - `StaticShell` provides the shell-stage variant extracted from a static response —
* param-dependent content reduced to pending fallbacks, and never any content that * param-dependent content reduced to pending fallbacks, and never any content that
* depends on session data (cookies, headers) * depends on session data (cookies, headers)
* - `PPR` can provide static shells for each segment, including prerendered param-dependent
* content at concrete paths (excluding dynamic data)
* - `RuntimeShell` provides the shell stage rendered by a runtime request, which can * - `RuntimeShell` provides the shell stage rendered by a runtime request, which can
* additionally include shell-stage content that depends on session data * additionally include shell-stage content that depends on session data
* - `PPR` can provide static shells for each segment, including prerendered param-dependent
* content at concrete paths (excluding dynamic data)
* - `PPRRuntime` can additionally include content that uses searchParams, params, or cookies * - `PPRRuntime` can additionally include content that uses searchParams, params, or cookies
* - `Full` includes all the content, even if it uses dynamic data * - `Full` includes all the content, even if it uses dynamic data
* *
......
...@@ -1253,12 +1253,14 @@ function isShellEntryEligibleForStaticAttempt( ...@@ -1253,12 +1253,14 @@ function isShellEntryEligibleForStaticAttempt(
fetchStrategy: FetchStrategy.PPR | FetchStrategy.StaticShell fetchStrategy: FetchStrategy.PPR | FetchStrategy.StaticShell
): boolean { ): boolean {
if ( if (
(entry.fetchStrategy !== FetchStrategy.StaticShell && !(
entry.fetchStrategy !== FetchStrategy.RuntimeShell) || (entry.fetchStrategy === FetchStrategy.StaticShell ||
(tree.prefetchHints & PrefetchHint.ShouldAttemptStaticPrefetch) === 0 || entry.fetchStrategy === FetchStrategy.RuntimeShell) &&
// A StaticShell walk's static attempt is the shell tier itself, so it (tree.prefetchHints & PrefetchHint.ShouldAttemptStaticPrefetch) !== 0 &&
// only applies when the walk's static strategy outranks the entry. // A StaticShell walk's static attempt is the shell tier itself, so it
!canNewFetchStrategyProvideMoreContent(entry.fetchStrategy, fetchStrategy) // only applies when the walk's static strategy outranks the entry.
canNewFetchStrategyProvideMoreContent(entry.fetchStrategy, fetchStrategy)
)
) { ) {
return false return false
} }
......
...@@ -59,8 +59,8 @@ export const enum FetchStrategy { ...@@ -59,8 +59,8 @@ export const enum FetchStrategy {
// session data (cookies, headers) — and less complete than PPR at concrete // session data (cookies, headers) — and less complete than PPR at concrete
// paths, which includes prerendered param-dependent content. // paths, which includes prerendered param-dependent content.
StaticShell = 1, StaticShell = 1,
PPR = 2, RuntimeShell = 2,
RuntimeShell = 3, PPR = 3,
PPRRuntime = 4, PPRRuntime = 4,
Full = 5, Full = 5,
} }
......
#!/usr/bin/env bash
dir="$(dirname "${BASH_SOURCE[0]}")"
path="${1-}";
if [ -z "$path" ]; then
echo "Missing required pathname argument." >&2
echo "Usage: ${BASH_SOURCE[0]} <page pathname>" >&2
exit 1
fi
export CACHE_FILE="$dir/value.json";
INITIAL_CACHE_VALUE='{ "tag": "original", "timestamp": -1 }'
echo "$INITIAL_CACHE_VALUE" > "$CACHE_FILE"
rm -rf "$dir/.next";
pnpm next build "$dir" || exit 1;
export PORT=3000
pnpm next start "$dir" &
server_pid="$!"
function cleanup {
trap - SIGTERM
kill -- "$server_pid"
echo "$INITIAL_CACHE_VALUE" > "$CACHE_FILE"
}
trap 'cleanup' SIGINT SIGTERM EXIT
sleep 1;
echo "Revalidating page: $path"
curl -X POST "http://localhost:$PORT/update-cached-value";
num_attempts=10
did_revalidate=0
for (( i=0; i < "$num_attempts"; i++ )); do
content="$(curl -s "http://localhost:$PORT$path")" || break
if [[ "$content" == *'Cached value: updated'* ]]; then
did_revalidate=1
echo "Revalidation finished"
break;
else
echo "Revalidation not finished"
echo '--------------'
echo "$content"
echo '--------------'
sleep 0.5
fi
done
if [ "$did_revalidate" == '0' ]; then
echo "Revalidation not finished after $num_attempts attempts"
kill -- -$$
exit 1
fi
wait "$server_pid"
import { cookies } from 'next/headers'
import { connection } from 'next/server'
import { Suspense } from 'react'
export async function generateStaticParams() {
return [
{ slug: 'no-cookies-in-prefetch' },
{ slug: 'no-cookies-in-prefetch-2' },
{ slug: 'yes-cookies-in-prefetch' },
]
}
function shouldUseRuntimeDataForSlug(slug: string) {
return !slug.includes('no-cookies')
}
type Params = { slug: string }
export default function Page({ params }: { params: Promise<Params> }) {
return (
<main>
<p id="page-content">maybe-runtime-prefetch page shell text</p>
<Suspense fallback={<p id="param-loading">Loading param content...</p>}>
<ParamsDependent params={params} />
</Suspense>
<Suspense
fallback={<p id="dynamic-loading">Loading dynamic content...</p>}
>
<DynamicContent />
</Suspense>
</main>
)
}
async function ParamsDependent({ params }: { params: Promise<Params> }) {
const { slug } = await params
return (
<div>
<p id="param-content">{`Slug: ${slug}`}</p>
<Suspense
fallback={
<p id="maybe-runtime-content-fallback">Loading runtime data...</p>
}
>
<ConditionalCookiesUse slug={slug} />
</Suspense>
</div>
)
}
async function ConditionalCookiesUse({ slug }: { slug: string }) {
const shouldUseRuntimeData = shouldUseRuntimeDataForSlug(slug)
if (shouldUseRuntimeData) {
await cookies()
}
return (
<p id="maybe-runtime-content">{`Runtime data accessed on ${slug}: ${shouldUseRuntimeData}`}</p>
)
}
async function DynamicContent() {
await connection()
return <p id="dynamic-content">Dynamic content</p>
}
...@@ -142,6 +142,87 @@ export default function Page() { ...@@ -142,6 +142,87 @@ export default function Page() {
</LinkAccordion> </LinkAccordion>
</li> </li>
</ul> </ul>
<h2>{'Static requests with prefetch={true}'}</h2>
<ul>
<li>
<LinkAccordion href="/speculative-static-param/one" prefetch={true}>
Speculative static param one
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/speculative-static-param/two" prefetch={true}>
Speculative static param one
</LinkAccordion>
</li>
</ul>
<h2>Conditional cookies in prefetch</h2>
<ul>
<li>
<LinkAccordion href="/maybe-runtime-prefetch/no-cookies-in-prefetch">
Does not use cookies in the prefetch (prefetch=auto)
</LinkAccordion>
</li>
<li>
<LinkAccordion
href="/maybe-runtime-prefetch/no-cookies-in-prefetch"
prefetch={true}
>
Does not use cookies in the prefetch (prefetch=true)
</LinkAccordion>
</li>
</ul>
<ul>
<li>
<LinkAccordion href="/maybe-runtime-prefetch/no-cookies-in-prefetch-2">
Does not use cookies in the prefetch 2 (prefetch=auto)
</LinkAccordion>
</li>
<li>
<LinkAccordion
href="/maybe-runtime-prefetch/no-cookies-in-prefetch-2"
prefetch={true}
>
Does not use cookies in the prefetch 2 (prefetch=true)
</LinkAccordion>
</li>
</ul>
<ul>
<li>
<LinkAccordion href="/maybe-runtime-prefetch/yes-cookies-in-prefetch">
Uses cookies in the prefetch (prefetch=auto)
</LinkAccordion>
</li>
<li>
<LinkAccordion
href="/maybe-runtime-prefetch/yes-cookies-in-prefetch"
prefetch={true}
>
Uses cookies in the prefetch (prefetch=true)
</LinkAccordion>
</li>
</ul>
<ul>
<li>
<LinkAccordion href="/maybe-runtime-prefetch/yes-cookies-in-prefetch-not-prerendered">
Uses cookies in the prefetch, but was not prerendered
(prefetch=auto)
</LinkAccordion>
</li>
<li>
<LinkAccordion
href="/maybe-runtime-prefetch/yes-cookies-in-prefetch-not-prerendered"
prefetch={true}
>
Uses cookies in the prefetch, but was not prerendered
(prefetch=true)
</LinkAccordion>
</li>
</ul>
</main> </main>
) )
} }
import { connection } from 'next/server'
import { Suspense } from 'react'
export async function generateStaticParams() {
return [{ slug: 'one' }, { slug: 'two' }]
}
type Params = { slug: string }
export default function Page({ params }: { params: Promise<Params> }) {
return (
<main>
<p id="page-content">Speculative-static-param page shell text</p>
<Suspense fallback={<p id="slug-loading">Loading param content...</p>}>
<ParamsDependent params={params} />
</Suspense>
<Suspense
fallback={<p id="dynamic-loading">Loading dynamic content...</p>}
>
<DynamicContent />
</Suspense>
</main>
)
}
async function ParamsDependent({ params }: { params: Promise<Params> }) {
const { slug } = await params
return <p id="slug-content">{`Slug: ${slug}`}</p>
}
async function DynamicContent() {
await connection()
return <p id="dynamic-content">Dynamic content</p>
}
...@@ -6,19 +6,21 @@ import { useState } from 'react' ...@@ -6,19 +6,21 @@ import { useState } from 'react'
export function LinkAccordion({ export function LinkAccordion({
href, href,
children, children,
prefetch, prefetch = 'auto',
}: { }: {
href: string href: string
children: React.ReactNode children: React.ReactNode
prefetch?: LinkProps['prefetch'] prefetch?: LinkProps['prefetch']
}) { }) {
const [isVisible, setIsVisible] = useState(false) const [isVisible, setIsVisible] = useState(false)
const prefetchAttr = prefetch === null ? 'auto' : `${prefetch}`
return ( return (
<> <>
<input <input
type="checkbox" type="checkbox"
checked={isVisible} checked={isVisible}
onChange={() => setIsVisible(!isVisible)} onChange={() => setIsVisible(!isVisible)}
data-prefetch={prefetchAttr}
data-link-accordion={href} data-link-accordion={href}
/> />
{isVisible ? ( {isVisible ? (
......
...@@ -5,6 +5,7 @@ const nextConfig: NextConfig = { ...@@ -5,6 +5,7 @@ const nextConfig: NextConfig = {
// Opt every route into Partial Prefetching globally: segments without a // Opt every route into Partial Prefetching globally: segments without a
// per-segment `prefetch` export default to 'partial'. // per-segment `prefetch` export default to 'partial'.
partialPrefetching: true, partialPrefetching: true,
productionBrowserSourceMaps: true,
} }
export default nextConfig export default nextConfig
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