Question presented to candidate:
"After a user submits an order we write an audit log and send an analytics event, and both slow the response down. What does after() in Next.js do, and is it the right tool here?"
What a strong answer should cover:
after(callback)fromnext/serverschedules work to run after the response (or prerender) has finished, so logging and analytics do not delay the user.- Where it works: Server Components (including
generateMetadata), Server Actions, Route Handlers and Proxy; it is not a request-time API and does not make a route dynamic. - Request data: in Route Handlers and Server Actions you can call
cookies()/headers()inside the callback; in Server Components you must read them before and close over the values. - It runs even when the response fails, calls
notFound()orredirect(); in a static page it runs at build time or on revalidation. - Limits: the callback lives within the route's max duration; on serverless it relies on a
waitUntilprimitive; it is not a durable job queue, so critical work needs a real queue.
Clarifying questions expected:
- "Must the audit log never be lost?" β if yes, write it in the request or enqueue it durably;
afteris best effort. - "Where is this deployed?" β a Node server, Vercel and adapters support it differently; static export does not.
Code / implementation expected: Yes β a Route Handler or Server Action that returns immediately and logs inside after().