File size: 13,689 Bytes
fea495a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | import { InvariantError } from '../../shared/lib/invariant-error';
import { postponeWithTracking, throwToInterruptStaticGeneration } from '../app-render/dynamic-rendering';
import { workAsyncStorage } from '../app-render/work-async-storage.external';
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external';
import { makeHangingPromise } from '../dynamic-rendering-utils';
import { describeStringPropertyAccess, wellKnownProperties } from '../../shared/lib/utils/reflect-utils';
import { actionAsyncStorage } from '../app-render/action-async-storage.external';
import { warnOnce } from '../../build/output/log';
const CachedParams = new WeakMap();
/**
* @deprecated import specific root params from `next/root-params` instead.
*/ export async function unstable_rootParams() {
warnOnce('`unstable_rootParams()` is deprecated and will be removed in an upcoming major release. Import specific root params from `next/root-params` instead.');
const workStore = workAsyncStorage.getStore();
if (!workStore) {
throw Object.defineProperty(new InvariantError('Missing workStore in unstable_rootParams'), "__NEXT_ERROR_CODE", {
value: "E615",
enumerable: false,
configurable: true
});
}
const workUnitStore = workUnitAsyncStorage.getStore();
if (!workUnitStore) {
throw Object.defineProperty(new Error(`Route ${workStore.route} used \`unstable_rootParams()\` in Pages Router. This API is only available within App Router.`), "__NEXT_ERROR_CODE", {
value: "E641",
enumerable: false,
configurable: true
});
}
switch(workUnitStore.type){
case 'cache':
case 'unstable-cache':
{
throw Object.defineProperty(new Error(`Route ${workStore.route} used \`unstable_rootParams()\` inside \`"use cache"\` or \`unstable_cache\`. Support for this API inside cache scopes is planned for a future version of Next.js.`), "__NEXT_ERROR_CODE", {
value: "E642",
enumerable: false,
configurable: true
});
}
case 'prerender':
case 'prerender-client':
case 'prerender-ppr':
case 'prerender-legacy':
return createPrerenderRootParams(workUnitStore.rootParams, workStore, workUnitStore);
case 'private-cache':
case 'prerender-runtime':
case 'request':
return Promise.resolve(workUnitStore.rootParams);
default:
return workUnitStore;
}
}
function createPrerenderRootParams(underlyingParams, workStore, prerenderStore) {
switch(prerenderStore.type){
case 'prerender-client':
{
const exportName = '`unstable_rootParams`';
throw Object.defineProperty(new InvariantError(`${exportName} must not be used within a client component. Next.js should be preventing ${exportName} from being included in client components statically, but did not in this case.`), "__NEXT_ERROR_CODE", {
value: "E693",
enumerable: false,
configurable: true
});
}
case 'prerender':
{
const fallbackParams = prerenderStore.fallbackRouteParams;
if (fallbackParams) {
for(const key in underlyingParams){
if (fallbackParams.has(key)) {
const cachedParams = CachedParams.get(underlyingParams);
if (cachedParams) {
return cachedParams;
}
const promise = makeHangingPromise(prerenderStore.renderSignal, workStore.route, '`unstable_rootParams`');
CachedParams.set(underlyingParams, promise);
return promise;
}
}
}
break;
}
case 'prerender-ppr':
{
const fallbackParams = prerenderStore.fallbackRouteParams;
if (fallbackParams) {
for(const key in underlyingParams){
if (fallbackParams.has(key)) {
// We have fallback params at this level so we need to make an erroring
// params object which will postpone if you access the fallback params
return makeErroringRootParams(underlyingParams, fallbackParams, workStore, 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(underlyingParams);
}
function makeErroringRootParams(underlyingParams, fallbackParams, workStore, prerenderStore) {
const cachedParams = CachedParams.get(underlyingParams);
if (cachedParams) {
return cachedParams;
}
const augmentedUnderlying = {
...underlyingParams
};
// We don't use makeResolvedReactPromise here because params
// supports copying with spread and we don't want to unnecessarily
// instrument the promise with spreadable properties of ReactPromise.
const promise = Promise.resolve(augmentedUnderlying);
CachedParams.set(underlyingParams, promise);
Object.keys(underlyingParams).forEach((prop)=>{
if (wellKnownProperties.has(prop)) {
// These properties cannot be shadowed because they need to be the
// true underlying value for Promises to work correctly at runtime
} else {
if (fallbackParams.has(prop)) {
Object.defineProperty(augmentedUnderlying, prop, {
get () {
const expression = describeStringPropertyAccess('unstable_rootParams', prop);
// In most dynamic APIs we also throw if `dynamic = "error"` however
// for params is only dynamic when we're generating a fallback shell
// and even when `dynamic = "error"` we still support generating dynamic
// fallback shells
// TODO remove this comment when cacheComponents is the default since there
// will be no `dynamic = "error"`
if (prerenderStore.type === 'prerender-ppr') {
// PPR Prerender (no cacheComponents)
postponeWithTracking(workStore.route, expression, prerenderStore.dynamicTracking);
} else {
// Legacy Prerender
throwToInterruptStaticGeneration(expression, workStore, prerenderStore);
}
},
enumerable: true
});
} else {
;
promise[prop] = underlyingParams[prop];
}
}
});
return promise;
}
/**
* Used for the compiler-generated `next/root-params` module.
* @internal
*/ export function getRootParam(paramName) {
const apiName = `\`import('next/root-params').${paramName}()\``;
const workStore = workAsyncStorage.getStore();
if (!workStore) {
throw Object.defineProperty(new InvariantError(`Missing workStore in ${apiName}`), "__NEXT_ERROR_CODE", {
value: "E764",
enumerable: false,
configurable: true
});
}
const workUnitStore = workUnitAsyncStorage.getStore();
if (!workUnitStore) {
throw Object.defineProperty(new Error(`Route ${workStore.route} used ${apiName} outside of a Server Component. This is not allowed.`), "__NEXT_ERROR_CODE", {
value: "E774",
enumerable: false,
configurable: true
});
}
const actionStore = actionAsyncStorage.getStore();
if (actionStore) {
if (actionStore.isAppRoute) {
// TODO(root-params): add support for route handlers
throw Object.defineProperty(new Error(`Route ${workStore.route} used ${apiName} inside a Route Handler. Support for this API in Route Handlers is planned for a future version of Next.js.`), "__NEXT_ERROR_CODE", {
value: "E765",
enumerable: false,
configurable: true
});
}
if (actionStore.isAction && workUnitStore.phase === 'action') {
// Actions are not fundamentally tied to a route (even if they're always submitted from some page),
// so root params would be inconsistent if an action is called from multiple roots.
// Make sure we check if the phase is "action" - we should not error in the rerender
// after an action revalidates or updates cookies (which will still have `actionStore.isAction === true`)
throw Object.defineProperty(new Error(`${apiName} was used inside a Server Action. This is not supported. Functions from 'next/root-params' can only be called in the context of a route.`), "__NEXT_ERROR_CODE", {
value: "E766",
enumerable: false,
configurable: true
});
}
}
switch(workUnitStore.type){
case 'unstable-cache':
case 'cache':
{
throw Object.defineProperty(new Error(`Route ${workStore.route} used ${apiName} inside \`"use cache"\` or \`unstable_cache\`. Support for this API inside cache scopes is planned for a future version of Next.js.`), "__NEXT_ERROR_CODE", {
value: "E760",
enumerable: false,
configurable: true
});
}
case 'prerender':
case 'prerender-client':
case 'prerender-ppr':
case 'prerender-legacy':
{
return createPrerenderRootParamPromise(paramName, workStore, workUnitStore, apiName);
}
case 'private-cache':
case 'prerender-runtime':
case 'request':
{
break;
}
default:
{
workUnitStore;
}
}
return Promise.resolve(workUnitStore.rootParams[paramName]);
}
function createPrerenderRootParamPromise(paramName, workStore, prerenderStore, apiName) {
switch(prerenderStore.type){
case 'prerender-client':
{
throw Object.defineProperty(new InvariantError(`${apiName} must not be used within a client component. Next.js should be preventing ${apiName} from being included in client components statically, but did not in this case.`), "__NEXT_ERROR_CODE", {
value: "E693",
enumerable: false,
configurable: true
});
}
case 'prerender':
case 'prerender-legacy':
case 'prerender-ppr':
default:
}
const underlyingParams = prerenderStore.rootParams;
switch(prerenderStore.type){
case 'prerender':
{
// We are in a dynamicIO prerender.
// The param is a fallback, so it should be treated as dynamic.
if (prerenderStore.fallbackRouteParams && prerenderStore.fallbackRouteParams.has(paramName)) {
return makeHangingPromise(prerenderStore.renderSignal, workStore.route, apiName);
}
break;
}
case 'prerender-ppr':
{
// We aren't in a dynamicIO prerender, but the param is a fallback,
// so we need to make an erroring params object which will postpone/error if you access it
if (prerenderStore.fallbackRouteParams && prerenderStore.fallbackRouteParams.has(paramName)) {
return makeErroringRootParamPromise(paramName, workStore, prerenderStore, apiName);
}
break;
}
case 'prerender-legacy':
{
break;
}
default:
{
prerenderStore;
}
}
// If the param is not a fallback param, we just return the statically available value.
return Promise.resolve(underlyingParams[paramName]);
}
/** Deliberately async -- we want to create a rejected promise, not error synchronously. */ async function makeErroringRootParamPromise(paramName, workStore, prerenderStore, apiName) {
const expression = describeStringPropertyAccess(apiName, paramName);
// In most dynamic APIs, we also throw if `dynamic = "error"`.
// However, root params are only dynamic when we're generating a fallback shell,
// and even with `dynamic = "error"` we still support generating dynamic fallback shells.
// TODO: remove this comment when dynamicIO is the default since there will be no `dynamic = "error"`
switch(prerenderStore.type){
case 'prerender-ppr':
{
return postponeWithTracking(workStore.route, expression, prerenderStore.dynamicTracking);
}
case 'prerender-legacy':
{
return throwToInterruptStaticGeneration(expression, workStore, prerenderStore);
}
default:
{
prerenderStore;
}
}
}
//# sourceMappingURL=root-params.js.map |