Why is Vercel invoking my function (GET endpoint) during deployment

I have a Vercel function in my NextJS application located at app\api\products\route.ts which exposes a GET endpoint as shown in the blow code. Everytime I do a deployment, Vercel is invoking the GET endpoint which I dont think it’s supposed to.

Say I am doing some sort of database connection/initialization in that endpoint (which I am not supposed to but say I am doing it anyway), Vercel executing that function during build is going to cause some unexpected side effect and if someone is not aware of this behaviour is gonna have a hard time figure out what’s happening.

Is this a an expected behaviour and supposed to be this way?

import { NextResponse } from "next/server";

export async function GET(request: Request) {
  console.trace('Fetching products...')
  return NextResponse.json({});

}

Hi @bjdash, welcome to the Vercel Community!

This is a Next.js behaviour instead of a Vercel issue. You can recreate this by running npm run build locally.

Now, Next.js tries to pre-render as much as possible. So, when the compiler notices that your route handler isn’t using any dynamic APIs it tries to pre-render it during the build.

You can bypass this behavior for a static route by putting at the top of the file:

export const dynamic = "force-dynamic";

import { NextResponse } from "next/server";

export async function GET(request: Request) {
  console.trace("Fetching products...");
  return NextResponse.json({});
}

This behaviour is changed in the Next.js 15, as per the version history:

v15.0.0-RC The default caching for GET handlers was changed from static to dynamic

1 Like

Ahh my bad. I should have run a local build first. Thanks for clarifying that. Cheers.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.