Unverified Commit d50d2ce8 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Prefer non-partial entries regardless of FetchStrategy (#99705)

If an entry is not partial, it means nothing was omittted during
rendering. Regardless of the strategy used to fetch it — static vs
dynamic, shell vs prefetch vs navigation, etc — it's the most complete
possible representation of the data. No other fetch strategy could
return a more complete version.

This PR updates the precedence logic to take `isPartial` into account. I
discovered this while working on the next PR, which replaces `isPartial`
with a new "completeness" concept. I extracted it into this earlier PR
to show how it would be expressed in the current model, so the next one
is easier to review.
parent 06136c4a
...@@ -71,7 +71,4 @@ export const enum FetchStrategy { ...@@ -71,7 +71,4 @@ export const enum FetchStrategy {
* until we complete the initial tree prefetch request, so we use `PPR` to signal both cases * until we complete the initial tree prefetch request, so we use `PPR` to signal both cases
* and adjust it based on the route when actually fetching. * and adjust it based on the route when actually fetching.
* */ * */
export type PrefetchTaskFetchStrategy = export type PrefetchTaskFetchStrategy = FetchStrategy.PPR | FetchStrategy.Full
| FetchStrategy.PPR
| FetchStrategy.PPRRuntime
| FetchStrategy.Full
...@@ -214,19 +214,16 @@ export function getSegmentVaryPathForRequest<TData>( ...@@ -214,19 +214,16 @@ export function getSegmentVaryPathForRequest<TData>(
tree: RouteTree<TData> tree: RouteTree<TData>
): VaryPath { ): VaryPath {
// This is used for storing pending requests in the cache. We want to choose // This is used for storing pending requests in the cache. We want to choose
// the most generic vary path based on the strategy used to fetch it, i.e. // the most generic vary path based on the strategy used to fetch it, so
// static/PPR versus runtime prefetching, so that it can be reused as much // that it can be reused as much as possible.
// as possible.
// //
// We may be able to re-key the response to something even more generic once // We may be able to re-key the response to something even more generic once
// we receive it — for example, if the server tells us that the response // we receive it — for example, if the server tells us that the response
// doesn't vary on a particular param — but even before we send the request, // doesn't vary on a particular param — but even before we send the request,
// we know some params are reusable based on the fetch strategy alone. For // we know some params are reusable based on the fetch strategy alone.
// example, a static prefetch will never vary on search params.
// //
// The original vary path with all the params filled in is stored on the // The original vary path with all the params filled in is stored on the
// route tree object. We will clone this one to create a new vary path // route tree object. The shell vary path is precomputed there too.
// where certain params are replaced with Fallback.
// //
// This result of this function is not stored anywhere. It's only used to // This result of this function is not stored anywhere. It's only used to
// access the cache a single time. // access the cache a single time.
...@@ -234,8 +231,6 @@ export function getSegmentVaryPathForRequest<TData>( ...@@ -234,8 +231,6 @@ export function getSegmentVaryPathForRequest<TData>(
// TODO: Rather than create a new list object just to access the cache, the // TODO: Rather than create a new list object just to access the cache, the
// plan is to add the concept of a "vary mask". This will represent all the // plan is to add the concept of a "vary mask". This will represent all the
// params that can be treated as Fallback. (Or perhaps the inverse.) // params that can be treated as Fallback. (Or perhaps the inverse.)
const originalVaryPath = tree.varyPath
if ( if (
fetchStrategy === FetchStrategy.RuntimeShell || fetchStrategy === FetchStrategy.RuntimeShell ||
fetchStrategy === FetchStrategy.StaticShell fetchStrategy === FetchStrategy.StaticShell
...@@ -249,36 +244,43 @@ export function getSegmentVaryPathForRequest<TData>( ...@@ -249,36 +244,43 @@ export function getSegmentVaryPathForRequest<TData>(
// params keep their concrete value). // params keep their concrete value).
return tree.shellVaryPath return tree.shellVaryPath
} }
return tree.varyPath
}
/**
* The vary path a pending entry is stored under when its response never
* varies on search params: every strategy except Full and PPRRuntime. Like
* getSegmentVaryPathForRequest, except that the entry can be reused across
* all possible search param values.
*/
export function getStaticSegmentVaryPathForRequest<TData>(
fetchStrategy: FetchStrategy,
tree: RouteTree<TData>
): VaryPath {
if (
fetchStrategy === FetchStrategy.RuntimeShell ||
fetchStrategy === FetchStrategy.StaticShell
) {
return tree.shellVaryPath
}
// The vary path includes a search params entry only when the segment varies // The vary path includes a search params entry only when the segment varies
// on search params. // on search params.
const originalVaryPath = tree.varyPath
const searchParamsVaryPath = originalVaryPath.parent const searchParamsVaryPath = originalVaryPath.parent
if ( if (
searchParamsVaryPath !== null && searchParamsVaryPath !== null &&
searchParamsVaryPath.id === SEARCH_PARAMS_VARY_ID searchParamsVaryPath.id === SEARCH_PARAMS_VARY_ID
) { ) {
// Only a runtime prefetch will include search params in the vary path. // Rebuild the vary path with the search params replaced by Fallback.
// Static prefetches never include search params, so they can be reused //
// across all possible search param values. // requestKey -> searchParams -> pathParams
const doesVaryOnSearchParams = // ^ This part gets replaced with Fallback
fetchStrategy === FetchStrategy.Full || return finalizeVaryPath(
fetchStrategy === FetchStrategy.PPRRuntime originalVaryPath.value,
Fallback,
if (!doesVaryOnSearchParams) { getPartialVaryPath(originalVaryPath)
// The response from the the server will not vary on search params. )
// Rebuild the vary path with the search params replaced by Fallback.
//
// requestKey -> searchParams -> pathParams
// ^ This part gets replaced with Fallback
return finalizeVaryPath(
originalVaryPath.value,
Fallback,
getPartialVaryPath(originalVaryPath)
)
}
} }
// The request does vary on search params. We don't need to modify anything.
return originalVaryPath return originalVaryPath
} }
......
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