after from next/server takes a callback and runs it once the response (or prerender) is done. The point is side effects the user shouldn't wait for: an audit log, analytics, a webhook to another system, warming a cache.
What I measured on Next.js 16.4.5 under next start, with a callback that slept one second:
- Route handler:
curlfinished in 7 ms; the callback started at +1 ms and ended at +1002 ms. - Server Action: the action's response took 45 ms; the callback ended at +1007 ms.
- Server Component page: 9 ms response, callback done at +1005 ms.
- Streamed response: a handler streaming three chunks 500 ms apart closed its stream at +1504 ms;
afterran at +1505 ms. "After the response" means after the last byte. - Proxy: a callback scheduled there ran as well.
It also ran when the handler threw (500) and when a page called redirect() (307), as the docs say. It isn't a request-time API, so it doesn't make a route dynamic. With Cache Components on, a GET handler that only returned a constant was prerendered, and its after callback ran during next build.
Request data: in route handlers and actions, headers() and cookies() work inside the callback (it read curl/8.7.1). In Server Components they throw there; read them during render and close over the values.
Lifetime is the host's job. next start keeps running, and on SIGTERM it waited for a pending callback before exiting. Serverless platforms must keep the invocation alive: Next.js looks up a waitUntil through globalThis[Symbol.for('@next/request-context')], and per the docs the callback is bounded by the route's maxDuration (not run here).