Skip to main content

connection

connection() 函数允许你指示渲染应等待传入的用户请求后再继续。

¥The connection() function allows you to indicate rendering should wait for an incoming user request before continuing.

当组件不使用 动态 API,但你希望它在运行时动态渲染而不是在构建时静态渲染时,它很有用。这通常发生在你访问有意更改渲染结果的外部信息时,例如 Math.random()new Date()

¥It's useful when a component doesn’t use Dynamic APIs, but you want it to be dynamically rendered at runtime and not statically rendered at build time. This usually occurs when you access external information that you intentionally want to change the result of a render, such as Math.random() or new Date().

import { connection } from 'next/server'

export default async function Page() {
await connection()
// Everything below will be excluded from prerendering
const rand = Math.random()
return <span>{rand}</span>
}
import { connection } from 'next/server'

export default async function Page() {
await connection()
// Everything below will be excluded from prerendering
const rand = Math.random()
return <span>{rand}</span>
}

参考

¥Reference

类型

¥Type

function connection(): Promise<void>

参数

¥Parameters

  • 该函数不接受任何参数。

    ¥The function does not accept any parameters.

返回

¥Returns

  • 该函数返回 void Promise。它不是为了被消耗而设的。

    ¥The function returns a void Promise. It is not meant to be consumed.

很高兴知道

¥Good to know

  • connection 取代 unstable_noStore,以更好地与 Next.js 的未来保持一致。

    ¥connection replaces unstable_noStore to better align with the future of Next.js.

  • 仅在需要动态渲染且未使用常见动态 API 时,才需要该函数。

    ¥The function is only necessary when dynamic rendering is required and common Dynamic APIs are not used.

版本历史

¥Version History

版本变化
v15.0.0-RCconnection 已引入。