next
Version:
The React Framework
79 lines (78 loc) • 4.02 kB
JavaScript
import { workAsyncStorage } from '../app-render/work-async-storage.external';
import { throwInvariantForMissingStore, workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external';
import { makeDynamicHangingPromise, makeFallbackParamsHangingPromise, RENDER_STAGES_BY_DATA_KIND } from '../dynamic-rendering-utils';
import { InvariantError } from '../../shared/lib/invariant-error';
export function createServerPathnameForMetadata(underlyingPathname) {
const workStore = workAsyncStorage.getStore();
if (!workStore) {
throw new InvariantError('Expected workStore to be initialized');
}
const workUnitStore = workUnitAsyncStorage.getStore();
if (workUnitStore) {
switch(workUnitStore.type){
case 'prerender':
case 'prerender-legacy':
{
return createPrerenderPathname(underlyingPathname, workStore, workUnitStore);
}
case 'prerender-client':
case 'validation-client':
throw new InvariantError('createServerPathnameForMetadata should not be called in client contexts.');
case 'cache':
case 'private-cache':
case 'unstable-cache':
throw new InvariantError('createServerPathnameForMetadata should not be called in cache contexts.');
case 'build-time-generator':
throw new InvariantError(`createServerPathnameForMetadata should not be called inside ${workUnitStore.functionName}.`);
case 'prerender-runtime':
{
// TODO(app-shells): whether or not this is included in the shell
// should depend on whether this route has params.
// if there's no params, it can be included.
// for now, we defensively exclude it to match the earlier pessimistic
// behavior of always resolving in the PrefetchRuntime stage
// (i.e. assuming that we have non-static params in the pathname)
const { stagedRendering } = workUnitStore;
const pathnameStage = RENDER_STAGES_BY_DATA_KIND.runtimeUrlData;
if (stagedRendering) {
return stagedRendering.delayUntilStage(pathnameStage, undefined, underlyingPathname);
} else {
if (workUnitStore.finalStage < pathnameStage) {
return makeDynamicHangingPromise(workUnitStore.renderSignal, workStore.route, '`pathname`');
} else {
return createRenderPathname(underlyingPathname);
}
}
}
case 'request':
// TODO(app-shells): this should be delayed if there's non-static params
return createRenderPathname(underlyingPathname);
default:
workUnitStore;
}
}
throwInvariantForMissingStore();
}
function createPrerenderPathname(underlyingPathname, workStore, prerenderStore) {
switch(prerenderStore.type){
case 'prerender':
{
const fallbackParams = prerenderStore.fallbackRouteParams;
if (fallbackParams && fallbackParams.size > 0) {
// The pathname depends on params, so we track it like a fallback params access.
return makeFallbackParamsHangingPromise(prerenderStore.renderSignal, workStore.route, '`pathname`', prerenderStore);
}
break;
}
case 'prerender-legacy':
break;
default:
prerenderStore;
}
// We don't have any fallback params so we have an entirely static safe params object
return Promise.resolve(underlyingPathname);
}
function createRenderPathname(underlyingPathname) {
return Promise.resolve(underlyingPathname);
}
//# sourceMappingURL=pathname.js.map