vercel/next.js · error · Error

`after()` will not work correctly, because `waitUntil` is no

Error message

`after()` will not work correctly, because `waitUntil` is not available in the current environment.

What it means

Thrown by errorWaitUntilNotAvailable() (via addThenable/addCallback) when after() is called but the current runtime does not provide a `waitUntil` function. after() relies on the platform's waitUntil to keep the process alive after the response is sent; without it deferred work would be killed when the request ends. This is an environment capability error.

Source

Thrown at packages/next/src/server/after/after-context.ts:193

      // this is very defensive, but we really don't want anything to blow up in an error handler
      try {
        this.onTaskError?.(error)
      } catch (handlerError) {
        console.error(
          new InvariantError(
            '`onTaskError` threw while handling an error thrown from an `after` task',
            {
              cause: handlerError,
            }
          )
        )
      }
    }
  }
}

function errorWaitUntilNotAvailable(): never {
  throw new Error(
    '`after()` will not work correctly, because `waitUntil` is not available in the current environment.'
  )
}

View on GitHub (pinned to 0ae8c72462)

Solutions

  1. Run the code under the standard Next.js server (next dev / next start) or a supported adapter that provides waitUntil.
  2. Avoid calling after() in custom-server or standalone scripts that bypass the request lifecycle.
  3. If you must defer work in an unsupported environment, use your own background queue instead of after().

Example fix

// before: custom server without waitUntil
after(() => syncDb()) // throws
// after: run under next start, or use a manual queue
process.nextTick(() => syncDb()) // only if you accept it may be killed
Defensive patterns

Strategy: validation

Validate before calling

// before calling after(), confirm the environment provides waitUntil
function hasWaitUntil(): boolean {
  // This is framework-internal; in user code, gate after() usage on running under next start/dev
  return process.env.NEXT_RUNTIME !== undefined
}

Try / catch

try {
  after(() => doWork())
} catch (err) {
  if (err.message.includes('waitUntil is not available')) {
    // schedule work via your own background queue instead
  }
}

Prevention

When it happens

Trigger: Calling after() in a runtime/adapter that did not supply waitUntil in RequestLifecycleOpts (e.g. a minimal custom server, an unsupported serverless adapter, or running a route handler outside the standard Next.js request lifecycle). Both the Promise and callback code paths check `if (!this.waitUntil)` and throw.

Common situations: Using a custom Node.js server or an older/unsupported platform adapter that doesn't implement waitUntil; invoking route logic in a test harness without the full Next.js server; deploying to an environment where the request lifecycle hooks aren't wired.

Related errors


AI-assisted analysis of vercel/next.js@0ae8c72462 (2026-08-06). Data as JSON: /api/errors/09add2c77e9a87f4. Report an issue: GitHub.