next
Version:
The React Framework
100 lines (99 loc) • 5.31 kB
JavaScript
import { resolveRouteParamsFromTree } from '../../build/static-paths/utils';
import { parseNormalizedAppRoute } from '../../shared/lib/router/routes/app';
import { extractPathnameRouteParamSegmentsFromLoaderTree } from '../../build/static-paths/app/extract-pathname-route-param-segments-from-loader-tree';
import { getParamProperties } from '../../shared/lib/router/utils/get-segment-param';
import { InvariantError } from '../../shared/lib/invariant-error';
/**
* Creates an opaque fallback route params object from the fallback route params.
*
* @param fallbackRouteParams the fallback route params
* @returns the opaque fallback route params
*/ export function createOpaqueFallbackRouteParams(fallbackRouteParams) {
// If there are no fallback route params, we can return early.
if (fallbackRouteParams.length === 0) return null;
// As we're creating unique keys for each of the dynamic route params, we only
// need to generate a unique ID once per request because each of the keys will
// be also be unique.
const uniqueID = Math.random().toString(16).slice(2);
const keys = new Map();
// Generate a unique key for the fallback route param, if this key is found
// in the static output, it represents a bug in cache components.
for (const { paramName } of fallbackRouteParams){
keys.set(paramName, `%%drp:${paramName}:${uniqueID}%%`);
}
return keys;
}
/**
* Selects the params that a staged render defers for the shell target that a
* request selects. Dev static-shell validation stages the same set, so it
* checks the shell that the build validates.
*
* The set derives from the build's shell metadata, not from the serving mode. A
* required shell (`throwOnEmptyStaticShell`) is the most specific shell the
* build generated for its params, and the build fails when its prelude is
* empty. Staging defers all of its fallback params, because the build validated
* exactly that shape. Any other shell may be empty, and a request completes it
* with the params that `generateStaticParams` can still supply. Only the params
* that completion never resolves stay deferred.
*/ export function getStagedFallbackParams(route) {
const { fallbackRouteParams, remainingPrerenderableParams } = route;
if (!(fallbackRouteParams == null ? void 0 : fallbackRouteParams.length)) {
return null;
}
if (route.throwOnEmptyStaticShell === undefined) {
throw new InvariantError('Expected throwOnEmptyStaticShell for a route with fallback params');
}
// A required shell keeps all of its fallback params deferred.
if (route.throwOnEmptyStaticShell || !(remainingPrerenderableParams == null ? void 0 : remainingPrerenderableParams.length)) {
return createOpaqueFallbackRouteParams(fallbackRouteParams);
}
return createOpaqueFallbackRouteParams(fallbackRouteParams.filter((param)=>!remainingPrerenderableParams.some((prerenderableParam)=>prerenderableParam.paramName === param.paramName)));
}
export function buildDynamicSegmentPlaceholder(param) {
const { repeat, optional } = getParamProperties(param.paramType);
if (optional) {
return `[[...${param.paramName}]]`;
}
if (repeat) {
return `[...${param.paramName}]`;
}
return `[${param.paramName}]`;
}
export function getPlaceholderFallbackRouteParams(params, fallbackRouteParams) {
return fallbackRouteParams.filter((param)=>{
const placeholder = buildDynamicSegmentPlaceholder(param);
const value = params == null ? void 0 : params[param.paramName];
return value === placeholder || Array.isArray(value) && value.length === 1 && value[0] === placeholder;
});
}
/**
* Gets the fallback route params for a given page. This is an expensive
* operation because it requires parsing the loader tree to extract the fallback
* route params.
*
* @param page the page
* @param routeModule the route module
* @returns the opaque fallback route params
*/ export function getFallbackRouteParams(page, routeModule) {
const route = parseNormalizedAppRoute(page);
// Extract the pathname-contributing segments from the loader tree. This
// mirrors the logic in buildAppStaticPaths where we determine which segments
// actually contribute to the pathname.
const { pathnameRouteParamSegments, params } = extractPathnameRouteParamSegmentsFromLoaderTree(routeModule.userland.loaderTree, route);
// Create fallback route params for the pathname segments.
const fallbackRouteParams = pathnameRouteParamSegments.map(({ paramName, paramType })=>({
paramName,
paramType
}));
// Resolve route params from the loader tree. This mutates the
// fallbackRouteParams array to add any route params that are
// unknown at request time.
//
// The page parameter contains placeholders like [slug], which helps
// resolveRouteParamsFromTree determine which params are unknown.
resolveRouteParamsFromTree(routeModule.userland.loaderTree, params, route, fallbackRouteParams // Will be mutated to add route params
);
// Track the unknown params using the same opaque placeholders as prerenders.
return createOpaqueFallbackRouteParams(fallbackRouteParams);
}
//# sourceMappingURL=fallback-params.js.map