connection() 函数允许你指示渲染应该等待传入的用户请求,然后再继续。
当组件不使用 Dynamic APIs,但你希望它在运行时动态渲染而不是在构建时静态渲染时,它会很有用。这通常发生在你访问你刻意希望改变渲染结果的外部信息时,例如 Math.random() 或 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>
}function connection(): Promise<void>void Promise。它不应被消费。connection 替代了 unstable_noStore,以更好地与 Next.js 的未来发展保持一致。| 版本 | 变更 |
|---|---|
v15.0.0 | connection stabilized. |
v15.0.0-RC | connection introduced. |