Next.js Discord

Discord Forum

Help with Cron Job header checking

Unanswered
Peruvian anchoveta posted this in #help-forum
Open in Discord
Peruvian anchovetaOP
Trying to get a cron job set up in Vercel. I followed their example, here is my code

module.exports = async function handler(request, response) {
  const authHeader = request.headers.get("authorization");
  if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
    return response.json(
      { success: false },
      {
        status: 401,
      }
    );
  }

  try {
    await checkForImageUpdates();
    response.status(200).json({ message: `Updated assets` });
  } catch (e) {
    console.error("Error updating assets", error);
    response.status(500).json({ error: "Error updating assets" });
  }
};


When running, the logs are erroring with TypeError: request.headers.get is not a function at handler. It was my understanding that they send in the CRON_SECRET env var if supplies (which I did add it in my env vars). Any dieas what I'm doing wrong?

7 Replies

Giant panda
It's likely a mismatch between your handler and runtime. For example it's irritating that you're using CJS syntax. Is this deployed as a single serverless function? Or rather part of a Next app? What's its path?
The error is unrelated to the secret, it's an error accessing the headers object which is probably undefined.
Peruvian anchovetaOP
It's part of a next app. /api/cron
i can see if switching to esm fixes it
Giant panda
/api/cron looks like outside of the app and pages router though?
Or did you forget to mention which one you use.
Technically it should be possible to have them outside if I am not mistaken but then they become separate serverless functions, never done it like that.