Unverified Commit bf1b6a65 authored by Delba de Oliveira's avatar Delba de Oliveira Committed by GitHub

15.1 docs: stable `after` (#73038)

**Do not merge until stable `after` lands in canary (#73605).** 

Closes: https://linear.app/vercel/issue/DOC-3818/[stable]-nextafter
Redirect: https://github.com/vercel/front/pull/38704

---------
Co-authored-by: 's avatarLee Robinson <me@leerob.io>
parent 21eb54a8
---
title: unstable_after
title: after
description: API Reference for the after function.
version: unstable
---
`unstable_after` allows you to schedule work to be executed after a response (or prerender) is finished. This is useful for tasks and other side effects that should not block the response, such as logging and analytics.
`after` allows you to schedule work to be executed after a response (or prerender) is finished. This is useful for tasks and other side effects that should not block the response, such as logging and analytics.
It can be used in [Server Components](/docs/app/building-your-application/rendering/server-components) (including [`generateMetadata`](https://nextjs.org/docs/app/api-reference/functions/generate-metadata)), [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations), [Route Handlers](/docs/app/building-your-application/routing/route-handlers), and [Middleware](/docs/app/building-your-application/routing/middleware).
The function accepts a callback that will be executed after the response (or prerender) is finished:
```tsx filename="app/layout.tsx switcher
import { unstable_after as after } from 'next/server'
import { after } from 'next/server'
// Custom logging function
import { log } from '@/app/utils'
......@@ -25,7 +24,7 @@ export default function Layout({ children }: { children: React.ReactNode }) {
```
```jsx filename="app/layout.js switcher
import { unstable_after as after } from 'next/server'
import { after } from 'next/server'
// Custom logging function
import { log } from '@/app/utils'
......@@ -38,7 +37,7 @@ export default function Layout({ children }) {
}
```
> **Good to know:** `unstable_after` is not a [Dynamic API](/docs/app/building-your-application/rendering/server-components#dynamic-apis) and calling it does not cause a route to become dynamic. If it's used within a static page, the callback will execute at build time, or whenever a page is revalidated.
> **Good to know:** `after` is not a [Dynamic API](/docs/app/building-your-application/rendering/server-components#dynamic-apis) and calling it does not cause a route to become dynamic. If it's used within a static page, the callback will execute at build time, or whenever a page is revalidated.
## Reference
......@@ -46,24 +45,24 @@ export default function Layout({ children }) {
- A callback function which will be executed after the response (or prerender) is finished.
### Serverless function duration
### Duration
`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`](/docs/app/api-reference/file-conventions/route-segment-config#maxduration) route segment config.
`after` will run for the platform's default or configured max duration of your route. If your platform supports it, you can configure the timeout limit using the [`maxDuration`](/docs/app/api-reference/file-conventions/route-segment-config#maxduration) route segment config.
## Good to know
- `unstable_after` will be executed even if the response didn't complete successfully. Including when an error is thrown or when `notFound` or `redirect` is called.
- You can use React `cache` to deduplicate functions called inside `unstable_after`.
- `unstable_after` can be nested inside other `unstable_after` calls, for example, you can create utility functions that wrap `unstable_after` calls to add additional functionality.
- `after` will be executed even if the response didn't complete successfully. Including when an error is thrown or when `notFound` or `redirect` is called.
- You can use React `cache` to deduplicate functions called inside `after`.
- `after` can be nested inside other `after` calls, for example, you can create utility functions that wrap `after` calls to add additional functionality.
## Alternatives
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`](https://vercel.com/docs/functions/functions-api-reference) or removing `await` from a promise, but with the following differences:
The use case for `after` is to process secondary tasks without blocking the primary response. It's similar to using the platform's [`waitUntil()`](https://vercel.com/docs/functions/functions-api-reference) or removing `await` from a promise, but with the following differences:
- **`waitUntil`**: accepts a promise and enqueues a task to be executed during the lifecycle of the request, whereas `unstable_after` accepts a callback that will be executed **after** the response is finished.
- **`waitUntil()`**: accepts a promise and enqueues a task to be executed during the lifecycle of the request, whereas `after` accepts a callback that will be executed **after** the response is finished.
- **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.
We recommend using `unstable_after` as it has been designed to consider other Next.js APIs and contexts.
We recommend using `after` as it has been designed to consider other Next.js APIs and contexts.
## Examples
......@@ -125,4 +124,5 @@ However, you cannot use these request APIs inside `after` in [Server Components]
| Version History | Description |
| --------------- | ---------------------------- |
| `v15.1.0` | `after` became stable. |
| `v15.0.0-rc` | `unstable_after` introduced. |
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment