next.js: [NEXT-1318] Unable to use context.waitUntil in Route Handler?

What is the improvement or update you wish to see?

When using pages router, we can use context on api routers:

import { NextResponse } from 'next/server';
import type { NextFetchEvent, NextRequest } from 'next/server';
 
export const config = {
  runtime: 'edge',
};
 
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
 
async function getAlbum() {
  const res = await fetch('https://jsonplaceholder.typicode.com/albums/1');
  await wait(10000);
  return res.json();
}
 
export default function MyEdgeFunction(
  request: NextRequest,
  context: NextFetchEvent,
) {
  context.waitUntil(getAlbum().then((json) => console.log({ json })));
 
  return NextResponse.json({
    name: `Hello, from ${request.url} I'm an Edge Function!`,
  });
}

while using app router, we got this from doc:

Currently, the only value of context is params, which is an object containing the dynamic route parameters for the current route.

so I am wondering how to use NextFetchEvent with route handler?

Is there any context that might help us understand?

I am trying to keep the function running after a response has been sent, as the vercel doc navigated, we can use waitUntil(), but it seems not approachable in App Router and Route Handler, can I get any thoughts? thanks

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/app/api-reference/file-conventions/route#context-optional

NEXT-1318

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 7
  • Comments: 15 (2 by maintainers)

Most upvoted comments

The Next.js team has created an undocumented (and highly experimental) API for this: internal_runWithWaitUntil.

⚠️ This is an internal API and will be removed soon. Please do not use.

  • The Edge Runtime supports it in Server Actions, Route Handlers, and in an SSR context
  • It delays cancellation of the worker for up to 30 seconds after the client disconnects (source)
  • I’ve been using it in production on Vercel for a few weeks now, and it seems to work exactly as advertised without issue
"use server";

import { internal_runWithWaitUntil as waitUntil } from "next/dist/server/web/internal-edge-wait-until";

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

export async function action() {
  console.log("Request received");

  waitUntil(async () => {
    await sleep(10000);
    console.log("Waited for 10 seconds");
  });

  return "Returned immediately";
}

+1 on this

Kind of strange that this was released without this feature. The types are literally wrong in saying there is this property that doesn’t exist (waitUntil). Had to move some routes to page router as a result of it.

@sam3d I’m wondering if there’s an update on when this is being planned to be released?

Yeah feels weird that there were 2(!!) major versions with app router released without this kinda important feature.

Hey @balazsorban44 , is this being worked on yet by any chance? If so can I have a rough ETA? Thank you & I really appreciate it!

Hi, it’s more like a missing feature. when Route Handlers have been introduced this was not added to the initial implementation requirements.

But after some internal discussions, we think we should add it so there’s feature parity with API Routes (edge).

It’s now tracked internally.