Nextjs background functions return 202 but never actually run

I have a very basic test background function in nextjs which returns the 20x status code but never runs the actual function (nothing in logs nothing posted to other services).

Actual background function:
https://regal-zabaione-e0776d.netlify.app/.netlify/functions/_api_hello-background

Source code:

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next";
import axios from "axios";

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

type Data = {
  name: string;
};

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<Data>
) {
  const { url, headers } = req;

  for (let i = 0; i <= 10; i++) {
    console.log(`sleeping ${i}`);
    axios.post("https://en1ta4a0x9c6i.x.pipedream.net", { i, url, headers });
    await sleep(i * 1000);
  }

  await res.status(200).json({ name: "John Doe" });
}

export const config = {
  type: "experimental-background",
};

Logs show the function is called but never actually logs anything (console.log statements never go to log):

And the requestbin receives nothing:

This all works when I’m running locally with netlify dev


I’m a pro customer (not free, so background functions should be enabled). This is happening in a couple repos, one on next12 and one on next13. It’s been happening for at least 10 days so far.

Hey there, @jsullivanlive :wave:

Welcome to the Netlify Forums and thanks for reaching out! We have shared this with our teams internally and will follow up with you here once we know more. Thanks for your patience!

To post an update (which has also been shared in the helpdesk), this appears to be a bug with the Next.js Runtime. For anyone willing to use Background Functions, you can use it by using standard Netlify Background Functions instead of Next.js API routes. The fix for the bug is still being investigated.

Is there a public tracker on GitHub for this issue with nextjs?

What is the last working version of nextjs on your platform? I tried several and they didn’t work.

No, it’s being tracked internally at the moment.

Any (recent) Next.js version should work fine. However, if you’re talking specifically about Background Functions not working, that’s not even a Next.js feature. That’s a Netlify-specific add-on, and it’s currently having some issues - as you’ve noticed. It should be fixed soon.

If you’re having some other issues with Next.js on Netlify, feel free to let us know.

Hi, do you know if there is any update on this issue yet? We faced this same issue when trying to deploy next.js api routes as background functions.

Are you using Next.js 13 as well? Could you share the site name?

There’s no update as such, the team hasn’t been able to prioritize the issue, but if you’re not using Next.js 13, this should not happen.

Ok thanks. It’s our dashboard site and we are using Next.js 12.2, I believe the docs mention that 12.2 or above should be supported.

Thank you for following up. We’ve added this note on the internal issue and would update with new info as we get it.

Hey all,

Could you confirm if you were accessing the URL as /api/<route-name> and not /.netlify/functions/? The former should work as a background/scheduled function, the latter would not. We’re working on improving this so it can be more obvious in the future.