Free tools Windows power users keep installed
One-click scans. No signup required.
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Next.js after() schedules a callback to run after a response or prerender finishes. It is useful for secondary work such as logging and analytics, but two details are easy to get wrong: it does not make a route dynamic, and whether the callback can use cookies() or headers() depends on where after() is registered.
What does Next.js after() do?
Import after from next/server and pass it a callback. Next.js runs that callback after the response or prerender finishes, making it suitable for work that should not hold up the main response, such as logging or analytics. It is available in Server Components, Server Functions, Route Handlers, and Proxy. The callback runs within the route’s default or configured maximum duration; this is deferred work, not an unlimited execution window. Next.js API reference: after
The API reference says the callback still executes if the response does not complete successfully, including when an error is thrown or notFound or redirect is called. It also documents nested after() calls and using React cache to deduplicate functions invoked in a callback. Those behaviors do not amount to a promise of retries, exactly-once execution, or durable job storage.
Recommended Free Tools
Does Next.js after() make a route dynamic?
No. The API reference explicitly says after is not a Dynamic API and does not, by itself, make a route dynamic. For a statically rendered page, the callback runs at build time or when that page is revalidated—not once for every visitor. So a callback that records an event should not be treated as a per-request analytics hook merely because it is registered in a static page.
#1 Best Overall
Can cookies() or headers() be used inside after()?
It depends on the context where the callback is registered. In Route Handlers and Server Functions, the callback may call request APIs such as cookies() and headers() directly. In a Server Component—including a page, layout, or generateMetadata—those Dynamic APIs cannot be called from inside the after() callback. Next.js explains that request-data access must be known during rendering to support Partial Prerendering and Cache Components, while the callback runs after React rendering. Next.js API reference: after
Read the value before registering a Server Component callback
In a Server Component, read the required request value during rendering, then close over that value in the callback:
Rank #2
import { after } from 'next/server'
import { cookies } from 'next/headers'
export default async function Page() {
const cookieStore = await cookies()
const sessionId = cookieStore.get('session-id')?.value
after(() => {
if (sessionId) {
logVisit(sessionId)
}
})
return <main>Page content</main>
}
The important distinction is where the request API is called: before after() in the component, rather than from inside its callback.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Does after() run on static pages?
Yes, but its timing follows the page’s rendering mode. On a static page, the callback runs during the build or when the page is revalidated. On request-rendered output, it runs after the response. This distinction matters whenever the side effect is meant to describe an individual visitor or request: static rendering does not provide a fresh callback execution for each page view.
Rank #3
With Cache Components
When using Cache Components, components that read request data such as cookies or headers must be wrapped in <Suspense> so the rest of the page can be prerendered as a static shell. The documented pattern is to read the request value in the dynamic component and pass it into the after() closure. Next.js API reference: after
Does after() guarantee a background task will finish?
No such general guarantee is documented. The callback is limited by the route’s platform or configured maximum duration, and deferred execution depends on the runtime keeping the invocation alive. The API’s post-response timing should not be interpreted as a durable queue, retry mechanism, or guarantee that work will finish after the route’s execution window ends.
For work that must survive process termination or be retried reliably, use a durable job system appropriate to your application rather than relying on after() alone.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Does Next.js after() work on every deployment platform?
No. The current API reference lists Node.js server and Docker container as supported, static export as unsupported, and adapters as platform-specific. For serverless deployments, the platform must keep the invocation alive for deferred promises. Next.js describes this integration with waitUntil(promise); a serverless implementation needs an analogous waitUntil capability. Check the support and configuration for the specific platform and adapter you deploy to. Next.js API reference: after
Best Value
| Rendering or deployment context | What to expect |
|---|---|
| Static page | The callback runs at build time or on revalidation, not once per visitor. |
| Node.js server | Listed as supported in the current Next.js API reference. |
| Docker container | Listed as supported in the current Next.js API reference. |
| Static export | Listed as unsupported in the current Next.js API reference. |
| Serverless platform | Requires platform support that keeps the invocation alive for deferred work, such as an analogous waitUntil integration. |
| Adapter | Support is platform-specific; check the adapter’s behavior. |
Which Next.js versions support stable after()?
after() became stable in Next.js v15.1.0. It was introduced as unstable_after in v15.0.0-rc. Current examples import after from next/server; older examples may refer to the unstable name. The Next.js 15.1 announcement describes stabilization work that included improved self-hosted support, fixes for interactions with other features, platform extensibility through waitUntil, and runtime API support in Server Actions and Route Handlers. Next.js 15.1 announcement
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

