unstable_after(实验)
此 API 是实验性的,可能会发生变化。
¥This API is experimental and subject to change.
unstable_after()
允许你安排在响应完成后执行的工作。这对于不应阻止响应的任务和其他副作用很有用,例如日志记录和分析。
¥unstable_after()
allows you to schedule work to be executed after a response is finished. This is useful for tasks and other side effects that should not block the response, such as logging and analytics.
它可以用于 服务器组件(包括 generateMetadata
)、服务器操作、路由处理程序 和 中间件。
¥It can be used in Server Components (including generateMetadata
), Server Actions, Route Handlers, and Middleware.
要使用 unstable_after()
,你需要使用 next.config.js
文件中的 experimental.after
配置启用它:
¥To use unstable_after()
, you need to enable it using the experimental.after
config in the next.config.js
file:
const nextConfig = {
experimental: {
after: true,
},
}
module.exports = nextConfig
该函数接受将在响应完成后执行的回调:
¥The function accepts a callback that will be executed after the response is finished:
import { unstable_after as after } from 'next/server'
import { log } from '@/app/utils'
export default function Layout({ children }: { children: React.ReactNode }) {
after(() => {
// Execute after the layout is rendered and sent to the user
log()
})
return <>{children}</>
}
import { unstable_after as after } from 'next/server'
import { log } from '@/app/utils'
export default function Layout({ children }) {
after(() => {
// Execute after the layout is rendered and sent to the user
log()
})
return <>{children}</>
}
很高兴知道:
¥Good to know:
即使响应未成功完成,
unstable_after()
也会执行。包括抛出错误或调用notFound()
或redirect()
时。¥
unstable_after()
will be executed even if the response didn't complete successfully. Including when an error is thrown or whennotFound()
orredirect()
is called.
unstable_after()
是一个将选择路由进行动态渲染的 动态函数。可以使用export dynamic = "force-static"
段配置覆盖此行为。¥
unstable_after()
is a dynamic function that will opt a route into dynamic rendering. This behavior can be overridden with theexport dynamic = "force-static"
segment config.你可以使用 React
cache
对unstable_after()
内部调用的函数进行数据去重。¥You can use React
cache
to deduplicate functions called insideunstable_after()
.由于响应已发送,因此无法在
unstable_after()
内设置cookies()
。¥
cookies()
cannot be set insideunstable_after()
since the response has already been sent.
unstable_after()
可以嵌套在其他unstable_after()
调用中。¥
unstable_after()
can be nested inside otherunstable_after()
calls.
参数
¥Parameters
-
响应完成后将执行的回调函数。
¥A callback function which will be executed after the response is finished.
返回
¥Returns
-
unstable_after()
不返回值。¥
unstable_after()
does not return a value.
替代方案
¥Alternatives
unstable_after()
的用例是在不阻止主要响应的情况下处理次要任务。它类似于使用平台的 waitUntil()
或从 promise 中删除 await
,但有以下区别:
¥The use case for unstable_after()
is to process secondary tasks without blocking the primary response. It's similar to using the platform's waitUntil()
or removing await
from a promise, but with the following differences:
-
waitUntil()
:接受 promise 并将要在请求的生命周期内执行的任务排入队列,而unstable_after()
接受将在响应完成后执行的回调。¥**
waitUntil()
**: accepts a promise and enqueues a task to be executed during the lifecycle of the request, whereasunstable_after()
accepts a callback that will be executed after the response is finished. -
删除
await
:在响应期间开始执行,这会使用资源。它在无服务器环境中也不可靠,因为函数在发送响应后立即停止计算,可能会中断任务。¥Removing
await
: starts executing during the response, which uses resources. It's also not reliable in serverless environments as the function stops computation immediately after the response is sent, potentially interrupting the task.
我们建议使用 unstable_after()
,因为它在设计时考虑了其他 Next.js API 和上下文。
¥We recommend using unstable_after()
as it has been designed to consider other Next.js APIs and contexts.
无服务器函数持续时间
¥Serverless function duration
unstable_after()
将运行平台默认或配置的无服务器功能的最大持续时间。如果你的平台支持,你可以使用 maxDuration
路由段配置来配置超时限制。
¥unstable_after()
will run for the platform's default or configured max duration of a serverless function. If your platform supports it, you can configure the timeout limit using the maxDuration
route segment config.