79bebe7bd3
Implements `unstable_after`, which lets the user schedule work to be executed after the response is finished. ### Implementation notes - `unstable_after()` is a dynamic function (bypassable only with `export dynamic = "force-static"`) - Usable in: server components (including `generateMetadata`), actions, route handlers, middleware - It is meant to run its callbacks even if a response didn't complete successfully (thrown error) or called `notFound()`/`redirect()` - Currently gated behind a `experimental.after` feature flag, because it touches many runtime bits (including a React monkeypatch...) - The state for `unstable_after()` in a given request lives in `requestAsyncStorage` (added via `RequestAsyncStorageWrapper`) - the implementation is based around two functions that we inject via `renderOpts`: - `waitUntil(promise)` - keep a function invocation alive until a promise settles. it is provided as a platform primitive in serverless contexts, and a noop in `next start` - for serverless (nodejs), Next.js will attempt to get `waitUntil` from `globalThis[Symbol.for('@next/request-context')].get().waitUntil`. This should be considered unstable for now. See `packages/next/src/server/after/wait-until-builtin.ts` for details. - `onClose(callback)` **[NEW]** - run something when a response is done. basically `res.on('close', callback)`, but also implemented for Web APIs - unfortunately, for Web, this requires some potentially expensive tricks - see `packages/next/src/server/web/web-on-close.ts` |
||
---|---|---|
.. | ||
middleware/redirect | ||
pages-dir |