after()

Schedules work to run after the response has finished streaming.

Since Next 15 (stable) Spec ↗

Syntax

import { after } from 'next/server'; after(() => { ... })

Parameters

NameTypeRequiredDescription
callback () => void | Promise<void> Yes Work to execute after the response is sent (logging, analytics).

Returns

void — Defers the callback off the response critical path.

Examples

import { after } from 'next/server'

export default async function Page() {
  const data = await getData()
  after(async () => {
    await logAnalytics('page_view', { ts: Date.now() })
  })
  return <View data={data} />
}
import { after } from 'next/server'

export async function POST(req: Request) {
  const body = await req.json()
  const result = await process(body)
  after(() => auditLog('processed', result.id))
  return Response.json(result)
}

Notes

Runs the callback after the response finishes, so secondary work (logging, metrics, cache warming) does not delay the user. The callback cannot affect the response. Works in Server Components, Server Actions, Route Handlers, and middleware. Stable in Next 15.

See also