Skip to content

Next JS API route Schedule Function doesn't run in Production #5603

@ui-jb

Description

@ui-jb

Describe the bug

I'm attempting to run a scheduled function using Netlify's Scheduled Next JS API Routes. While it appears that Netlify is calling the function on schedule, the code inside of it is not being ran.

I added a typescript file in the pages/api that runs a build hook every hour:

import fetch from "node-fetch";

const BUILD_HOOK = "*****";

export default async (req: NextApiRequest, res: NextApiResponse) => {
  console.log("ATTEMPTING DAILY REBUILD");
  try {
    const url = `${BUILD_HOOK}?trigger_title=daily_build&clear_cache=true`;
    await fetch(url, { method: "POST" });
    console.log("DAILY REBUILD SUCCEEDED");
    res.status(200).send({});
  } catch (err) {
    console.log("DAILY REBUILD ERROR:", err);
    res.status(500).send({});
  }
};

export const config = {
  type: "experimental-scheduled",
  schedule: "0 * * * *",
};

After deploying, Netlify properly identifies it as a scheduled function and tries to run it once an hour:
image

The problem is that nothing happens. It looks like Netlify is calling the build function in the logs above, but it seems like the script isn't running.

When I manually trigger the API route locally, the code properly executes.

Steps to reproduce

  1. Create a scheduled Next JS API route according to this article
  2. Push to production
  3. Verify that the code inside the function does not run

Configuration

No response

Environment

  System:
    OS: macOS 11.5.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 963.72 MB / 32.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
  npmGlobalPackages:
    netlify-cli: 12.7.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugcode to address defects in shipped code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions