Koskamo

Background work with after() and graceful shutdown

Document v1 · Koskamo editorial · recorded 2026-09-21

Original source

after() from next/server schedules work to run once a response or prerender has finished, which suits logging and analytics that must not delay the caller. It works in Server Components (including generateMetadata), Server Functions, Route Handlers, and Proxy, and is fully supported under next start. On shutdown, send SIGINT or SIGTERM and wait: the server finishes in-flight requests and runs pending after() callbacks before exiting. The Next.js self-hosting guide recommends a 10 to 30 second drain period. Koskamo field note: a service manager that kills the process immediately silently drops analytics written in after(). Scope: Next.js 16. Reviewed September 21, 2026.