after lets you schedule work to be executed after a response (or prerender) is finished. This is useful for tasks that should not block the response, such as logging, analytics, and other side effects.
It can be used in Server Components (including generateMetadata), Server Functions, Route Handlers, and Middleware.
app/layout.tsx
Parameters
A function to execute after the response or prerender is finished. Can be synchronous or asynchronous.
Duration
after runs for the platform’s default or configured max duration. You can configure the timeout with the maxDuration route segment config.
Good to know
afteris not a Request-time API. Calling it does not make a route dynamic. If used in a static page, the callback runs at build time or when the page is revalidated.afteris executed even if the response did not complete successfully — including when an error is thrown or whennotFound()orredirect()is called.aftercalls can be nested. Utility functions can wrapafterto add additional behavior.- Use React
cacheto deduplicate functions called insideafter.
Using request APIs inside after
Whether you can use cookies() and headers() inside after depends on where after is called:
In Route Handlers and Server Functions
You can callcookies() and headers() directly inside the after callback:
app/api/route.ts
In Server Components (pages and layouts)
Server Components cannot callcookies() or headers() inside the after callback. Read request data before after and pass the values in via closure:
app/page.tsx
With Cache Components
When using Cache Components, read request data in a dynamic (Suspense-wrapped) component and pass it intoafter:
app/page.tsx
Platform support
| Deployment option | Supported |
|---|---|
| Node.js server | Yes |
| Docker container | Yes |
| Static export | No |
| Adapters | Platform-specific |
after depends on a waitUntil primitive. When self-hosting, see the self-hosting guide for configuration.
Version history
| Version | Changes |
|---|---|
v15.1.0 | after became stable. |
v15.0.0-rc | unstable_after introduced. |
