Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEXT-1318] Unable to use context.waitUntil in Route Handler? #50522

Open
s1973 opened this issue May 30, 2023 · 17 comments
Open

[NEXT-1318] Unable to use context.waitUntil in Route Handler? #50522

s1973 opened this issue May 30, 2023 · 17 comments
Labels
area: app App directory (appDir: true) linear: next Confirmed issue that is tracked by the Next.js team. Pages Router Related to Pages Router. Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@s1973
Copy link

s1973 commented May 30, 2023

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

@s1973 s1973 added the Documentation Related to Next.js' official documentation. label May 30, 2023
@oalexdoda
Copy link

Hey @s1973 have you managed to figure this out yet?

@balazsorban44 balazsorban44 added area: API routes Runtime Related to Node.js or Edge Runtime with Next.js. area: app App directory (appDir: true) linear: next Confirmed issue that is tracked by the Next.js team. and removed Documentation Related to Next.js' official documentation. labels Jun 26, 2023
@balazsorban44 balazsorban44 changed the title Unable to use context.waitUntil in Route Handler? [NEXT-1318] Unable to use context.waitUntil in Route Handler? Jun 26, 2023
@oalexdoda
Copy link

@balazsorban44 is this a Next.js bug? And if so, any estimate as to when context.waitUntil would become available?

@balazsorban44
Copy link
Member

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.

@oalexdoda
Copy link

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.

Got it. Thank you! Will keep an eye out since it's blocking us from migrating some pretty important routes to the app directory.

@oalexdoda
Copy link

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!

@raunakdoesdev
Copy link

+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.

@kyb3r

This comment has been minimized.

@anuraagvaidya
Copy link

+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.

Were you able to access the context object using page router? Mine is an empty object.

@anuraagvaidya
Copy link

anuraagvaidya commented Oct 12, 2023

I was able to get around this issue by moving my long-running functions to middleware.js

export async function middleware(req, context) {
    if(/*match your path*/)
    {
        context.waitUntil(callFunctionsThatReturnsPromise);
    }
}

@oalexdoda
Copy link

Great workaround @anuraagvaidya , but pretty inconvenient for large apps with lots of moving pieces & routes. Really hoping to see native context support at a route level

@matannahmani

This comment has been minimized.

@sam3d
Copy link
Contributor

sam3d commented Dec 4, 2023

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";
}

@maccman
Copy link

maccman commented Feb 11, 2024

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

@PSoltes
Copy link

PSoltes commented Mar 21, 2024

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

@kyb3r
Copy link

kyb3r commented Apr 15, 2024

using internal_runWithWaitUntil

But any eta when the api will be stabilised in a release?

@balazsorban44 balazsorban44 added Pages Router Related to Pages Router. and removed area: API routes labels Apr 17, 2024
@maccman
Copy link

maccman commented May 3, 2024

It's brewing! #65038

@Thinkscape
Copy link

Good news!

https://vercel.com/changelog/waituntil-is-now-available-for-vercel-functions
https://vercel.com/docs/functions/functions-api-reference#waituntil

CahidArda added a commit to upstash/ratelimit-js that referenced this issue May 26, 2024
until recently, there was no way of using waitUntil in vercel edge. vercel/next.js#50522

But now it is possible. Updating the nextjs and vercel-edge examples utilizing the waitUntil.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: app App directory (appDir: true) linear: next Confirmed issue that is tracked by the Next.js team. Pages Router Related to Pages Router. Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests