Skip to main content
Next.js after() in Production: Post-Response Work, the Static-Page Build-Time Trap, and When We Use a Queue Instead

Next.js after() in Production: Post-Response Work, the Static-Page Build-Time Trap, and When We Use a Queue Instead

November 23, 2026
Full-Stack Engineering
8 min read

Headline: after() from next/server runs a callback once the response or prerender has finished, so analytics, audit logs, and cache work stop adding latency to the request. after() is not a job queue: it lives inside the same request's lifetime, it is capped by the route's max duration, and on a statically rendered page it runs at build time instead of on every visit.

Most Route Handlers and Server Actions our team reviews end with a few awaited side effects right before the return: an analytics event, an audit-log insert, a cache-warming fetch. Each one adds a network round trip to the response the user is waiting for, and none of them changes what the user sees. after() is the Next.js API for moving that work out of the response path. Next.js 15.0.0-rc introduced it as unstable_after, and Next.js 15.1.0 made it stable. These are our notes on where after() fits, the two behaviors that surprised us, and the line where we stop using it.

Key takeaways

- What it does: after(callback) from next/server schedules the callback to run after the response (or prerender) is finished. It is stable since Next.js 15.1.0.

- Where it works: Server Components (including generateMetadata), Server Functions such as Server Actions, Route Handlers, and Proxy, the file convention that replaced middleware in Next.js 16.

- The static-page trap: after() is not a Request-time API, so it does not make a route dynamic. On a static page the callback runs at build time or on revalidation, not once per visit.

- Request data: cookies() and headers() work inside the after() callback in Route Handlers and Server Functions, and throw a runtime error inside the callback in Server Components.

- Not durable: an after() callback is bounded by the route's max duration and is never retried. Work we cannot afford to lose goes to a durable queue.

What problem does after() in Next.js actually solve?

after() takes side effects off the critical path without relying on a fire-and-forget promise that the platform might freeze. Before after(), we had two bad options for non-critical work in a Route Handler.

- Await the work inline: correct, but every user waits for the analytics provider's response time on every request.

- Fire and forget with void track(): fast, but a serverless runtime can suspend the instance as soon as the response is sent, so the promise may never finish. On a long-running next start server the promise usually completes, but nothing waits for it during shutdown.

after() registers the callback with the platform instead. On Vercel, Next.js hands the work to waitUntil(promise), the primitive that extends a serverless invocation until the promise settles. On a self-hosted next start server, pending after() callbacks run to completion before the process exits on SIGTERM.

In practice our Route Handlers now await only the work the response depends on, such as creating the order, and call after() with an async callback that sends the order_created analytics event. The handler returns the 201 response immediately, and the analytics call happens once the response is out.

Where can we call after(), and when exactly does the callback run?

The after() callback runs once the response is finished, and a streamed Server Component page counts as finished when the stream completes, not when the first byte goes out. The Next.js docs list four call sites: Server Components (including generateMetadata), Server Functions, Route Handlers, and Proxy.

- It runs on failure too: Next.js executes after() callbacks even when the response did not complete successfully, including when an error is thrown or when notFound() or redirect() is called. A callback that logs "order created" must check the outcome it captured instead of assuming success.

- It can be nested: an after() call inside another after() callback is allowed, which makes small wrapper utilities safe to write.

- It shares React.cache: in Server Components, a function wrapped in React's cache() returns the value already computed during render, so the callback does not refetch the user or the session.

Why does an after() analytics call only fire at build time?

An after() call on a statically rendered page runs at build time or when the page is revalidated, because after() is not a Request-time API and does not opt the route into dynamic rendering. A page-view counter that calls after() with recordView(slug) in a static blog page records one view per build, not one per visitor, and the build log is the only place the event shows up.

- Per-visit analytics belong in the browser, sent by the analytics script or navigator.sendBeacon(). The static HTML stays cacheable at the CDN.

- Per-request server events belong in a component that already reads request data. With Cache Components, that component sits inside a Suspense boundary, reads cookies() during render, and passes the value into after() through a closure.

Can we read cookies() and headers() inside an after() callback?

Reading cookies() and headers() inside an after() callback works in Route Handlers and Server Functions and throws a runtime error in Server Components. Next.js needs to know which part of the component tree reads request data to support Partial Prerendering (PPR, which serves a static shell and streams dynamic holes) and Cache Components, and an after() callback runs after React's rendering lifecycle has ended.

- Route Handler: allowed inside the callback.

- Server Function (Server Action): allowed inside the callback.

- Page, layout, or generateMetadata: throws a runtime error; read the values during render and pass plain values in by closure.

The rule we follow everywhere, because it works at every call site: read request data before calling after(), and close over plain strings and IDs rather than request objects.

What happens when an after() callback throws or runs too long?

A failing after() callback cannot change the response, because the response has already been sent. The error never becomes a 500 or an error page; at most it becomes a line in the server log. We wrap every callback in a small afterSafe(task, fn) helper that calls after() with a try/catch and sends failures, tagged with the task label, through the same structured logger as the rest of the app.

Duration is the second limit. after() runs for the platform's default or configured max duration of the route, and the maxDuration route segment config raises that limit where the platform supports it. On serverless platforms the limit covers the whole invocation, so a slow render leaves less time for post-response work.

Shutdown is the third limit when self-hosting. The Next.js self-hosting guide says next start finishes in-flight requests and pending after() callbacks after SIGINT or SIGTERM, and recommends a 10 to 30 second drain period. docker stop sends SIGKILL after 10 seconds by default, so we set stop_grace_period: 30s in Docker Compose; Kubernetes already defaults terminationGracePeriodSeconds to 30 seconds.

Two deployment targets need extra care. Static export (output: 'export') does not support after() at all, because no server exists to run the callback. A custom serverless adapter must supply its own waitUntil through globalThis[Symbol.for('@next/request-context')]; without it, nothing keeps the invocation alive after the response.

When should we use a queue instead of after()?

We send work to a durable queue whenever a user or the business would notice its loss, and we use after() for everything else. after() runs in-process: an out-of-memory crash, a deploy that replaces the instance, or a timeout drops the callback, and nothing retries it.

- Await inline: the response includes the work; if the process dies, the request fails and the client can retry. We use it for work the response depends on.

- after(): the response excludes the work; if the process dies, the work is lost silently. We use it for analytics, logs, cache warming, and best-effort audit events.

- waitUntil() from @vercel/functions: the response excludes the work, but the work starts immediately; if the process dies, the work is lost silently. We use it in Vercel Functions that are not Next.js routes.

- Durable queue (Vercel Queues, BullMQ, or a transactional outbox): the response excludes the work, and a failed job is retried. We use it for receipts, payments, entitlements, and third-party webhooks.

The difference between after() and waitUntil() is timing as much as portability. waitUntil(promise) receives a promise that is already running, so the work competes with the render for CPU and database connections. after(callback) does not start the callback until the response is finished. Inside a Next.js app we use after().

FAQ

Q: Is after() stable in Next.js?

A: Yes. after() became stable in Next.js 15.1.0 and is imported from next/server; Next.js 15.0.0-rc shipped it as unstable_after.

Q: Does calling after() make a page dynamic?

A: No. after() is not a Request-time API, so a static page stays static and its after() callback runs at build time or on revalidation.

Q: Does after() run when a Server Action throws or calls redirect()?

A: Yes. Next.js runs after() callbacks even when the response fails, including after notFound() and redirect(), so the callback should check the outcome it captured.

Q: Can we use after() with a static export?

A: No. output: 'export' produces files with no server, and the Next.js platform support table lists static export as unsupported for after().

Q: Should we send transactional emails from after()?

A: Only if a lost email is acceptable. after() has no retries and dies with the process, so receipts and payment notifications go through a durable queue in our projects.