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({});
}
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: