MCPcopy
hub / github.com/vercel/next.js / getRootParam

Function getRootParam

packages/next/src/server/request/root-params.ts:26–130  ·  view source on GitHub ↗
(paramName: string)

Source from the content-addressed store, hash-verified

24 * @internal
25 */
26export function getRootParam(paramName: string): Promise<ParamValue> {
27 const apiName = `\`import('next/root-params').${paramName}()\``
28
29 const workStore = workAsyncStorage.getStore()
30 if (!workStore) {
31 throw new InvariantError(`Missing workStore in ${apiName}`)
32 }
33
34 const workUnitStore = workUnitAsyncStorage.getStore()
35 if (!workUnitStore) {
36 throw new Error(
37 `Route ${workStore.route} used ${apiName} outside of a Server Component. This is not allowed.`
38 )
39 }
40
41 const actionStore = actionAsyncStorage.getStore()
42 if (actionStore) {
43 if (actionStore.isAppRoute) {
44 // TODO(root-params): add support for route handlers
45 throw new Error(
46 `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.`
47 )
48 }
49 if (actionStore.isAction && workUnitStore.phase === 'action') {
50 // Actions are not fundamentally tied to a route (even if they're always submitted from some page),
51 // so root params would be inconsistent if an action is called from multiple roots.
52 // Make sure we check if the phase is "action" - we should not error in the rerender
53 // after an action revalidates or updates cookies (which will still have `actionStore.isAction === true`)
54 throw new Error(
55 `${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.`
56 )
57 }
58 }
59
60 switch (workUnitStore.type) {
61 case 'unstable-cache': {
62 throw new Error(
63 `Route ${workStore.route} used ${apiName} inside \`unstable_cache\`. This is not supported. Use \`"use cache"\` instead.`
64 )
65 }
66 case 'cache': {
67 if (!workUnitStore.rootParams) {
68 throw new Error(
69 `Route ${workStore.route} used ${apiName} inside \`"use cache"\` nested within \`unstable_cache\`. Root params are not available in this context.`
70 )
71 }
72 workUnitStore.readRootParamNames.add(paramName)
73 return Promise.resolve(workUnitStore.rootParams[paramName])
74 }
75 case 'prerender':
76 case 'prerender-ppr':
77 case 'prerender-legacy': {
78 return createPrerenderRootParamPromise(
79 paramName,
80 workStore,
81 workUnitStore,
82 apiName
83 )

Callers

nothing calls this directly

Calls 7

accumulateRootVaryParamFunction · 0.90
assertRootParamInSamplesFunction · 0.85
resolveMethod · 0.65
requireFunction · 0.50
getStoreMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected