跳到主要內容

connection

connection() 函式讓你能指示渲染應等待使用者請求進入後再繼續。

當組件未使用 動態 API,但你希望它在運行時動態渲染,而不是在建置時靜態渲染時,此函式非常有用。這通常發生在你存取外部資訊,且有意要變更渲染結果時,例如 Math.random()new Date()

app/page.tsx
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 的未來發展方向對齊。
  • 只有在需要動態渲染且未使用常見的動態 API 時,此函式才是必要的。

版本歷史

版本變更
v15.0.0connection 已穩定。
v15.0.0-RCconnection 已導入。