Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ impl ReactServerComponentValidator {
],

invalid_client_lib_apis_mapping: FxHashMap::from_iter([
("next/server", vec!["after", "unstable_rootParams"]),
("next/server", vec!["after"]),
(
"next/cache",
vec![
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* This is a comment.
*/

import { unstable_rootParams } from 'next/server'
import { lang } from 'next/root-params'

export default function () {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';
/**
* This is a comment.
*/ import { unstable_rootParams } from 'next/server';
*/ import { lang } from 'next/root-params';
export default function() {
return null;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
x You're importing a component that needs "unstable_rootParams". That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/app/
| building-your-application/rendering/server-components
x You're importing a component that needs "next/root-params". That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/app/building-
| your-application/rendering/server-components
|
|
,-[input.js:9:1]
8 |
9 | import { unstable_rootParams } from 'next/server'
: ^^^^^^^^^^^^^^^^^^^
9 | import { lang } from 'next/root-params'
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`----
1 change: 0 additions & 1 deletion packages/next/server.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ export { URLPattern } from 'next/dist/compiled/@edge-runtime/primitives/url'
export { ImageResponse } from 'next/dist/server/web/spec-extension/image-response'
export type { ImageResponseOptions } from 'next/dist/compiled/@vercel/og/types'
export { after } from 'next/dist/server/after'
export { unstable_rootParams } from 'next/dist/server/request/root-params'
export { connection } from 'next/dist/server/request/connection'
3 changes: 0 additions & 3 deletions packages/next/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ const serverExports = {
.URLPattern,
after: require('next/dist/server/after').after,
connection: require('next/dist/server/request/connection').connection,
unstable_rootParams: require('next/dist/server/request/root-params')
.unstable_rootParams,
}

// https://nodejs.org/api/esm.html#commonjs-namespaces
Expand All @@ -30,4 +28,3 @@ exports.userAgent = serverExports.userAgent
exports.URLPattern = serverExports.URLPattern
exports.after = serverExports.after
exports.connection = serverExports.connection
exports.unstable_rootParams = serverExports.unstable_rootParams
13 changes: 2 additions & 11 deletions packages/next/src/build/webpack/plugins/next-types-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,7 @@ function isSubpath(parentLayoutPath: string, potentialChildLayoutPath: string) {
)
}

function createServerDefinitions(
rootParams: { param: string; optional: boolean }[]
) {
function createServerDefinitions() {
return `
declare module 'next/server' {

Expand All @@ -423,13 +421,6 @@ function createServerDefinitions(
export type { ImageResponseOptions } from 'next/dist/compiled/@vercel/og/types'
export { after } from 'next/dist/server/after'
export { connection } from 'next/dist/server/request/connection'
export function unstable_rootParams(): Promise<{ ${rootParams
.map(
({ param, optional }) =>
// ensure params with dashes are valid keys
`${param.includes('-') ? `'${param}'` : param}${optional ? '?' : ''}: string`
)
.join(', ')} }>
}
`
}
Expand Down Expand Up @@ -814,7 +805,7 @@ export class NextTypesPlugin {
compilation.emitAsset(
serverTypesPath,
new sources.RawSource(
createServerDefinitions(rootParams)
createServerDefinitions()
) as unknown as webpack.sources.RawSource
)
}
Expand Down
184 changes: 2 additions & 182 deletions packages/next/src/server/request/root-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,189 +14,9 @@ import {
type StaticPrerenderStore,
} from '../app-render/work-unit-async-storage.external'
import { makeHangingPromise } from '../dynamic-rendering-utils'
import type { OpaqueFallbackRouteParams } from './fallback-params'
import type { Params, ParamValue } from './params'
import {
describeStringPropertyAccess,
wellKnownProperties,
} from '../../shared/lib/utils/reflect-utils'
import type { ParamValue } from './params'
import { describeStringPropertyAccess } from '../../shared/lib/utils/reflect-utils'
import { actionAsyncStorage } from '../app-render/action-async-storage.external'
import { warnOnce } from '../../build/output/log'

interface CacheLifetime {}
const CachedParams = new WeakMap<CacheLifetime, Promise<Params>>()

/**
* @deprecated import specific root params from `next/root-params` instead.
*/
export async function unstable_rootParams(): Promise<Params> {
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 new InvariantError('Missing workStore in unstable_rootParams')
}

const workUnitStore = workUnitAsyncStorage.getStore()

if (!workUnitStore) {
throw new Error(
`Route ${workStore.route} used \`unstable_rootParams()\` in Pages Router. This API is only available within App Router.`
)
}

switch (workUnitStore.type) {
case 'cache':
case 'unstable-cache': {
throw 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.`
)
}
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 satisfies never
}
}

function createPrerenderRootParams(
underlyingParams: Params,
workStore: WorkStore,
prerenderStore: StaticPrerenderStore
): Promise<Params> {
switch (prerenderStore.type) {
case 'prerender-client': {
const exportName = '`unstable_rootParams`'
throw 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.`
)
}
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<Params>(
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 satisfies never
}

// We don't have any fallback params so we have an entirely static safe params object
return Promise.resolve(underlyingParams)
}

function makeErroringRootParams(
underlyingParams: Params,
fallbackParams: OpaqueFallbackRouteParams,
workStore: WorkStore,
prerenderStore: PrerenderStorePPR | PrerenderStoreLegacy
): Promise<Params> {
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 as any)[prop] = underlyingParams[prop]
}
}
})

return promise
}

/**
* Used for the compiler-generated `next/root-params` module.
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/server/web/exports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ export { userAgent, userAgentFromString } from '../spec-extension/user-agent'
export { URLPattern } from '../spec-extension/url-pattern'
export { after } from '../../after'
export { connection } from '../../request/connection'
export { unstable_rootParams } from '../../request/root-params'

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading