Why aren't API routes with environment variables treated as dynamic by default?
Given a robots.ts file with this content for example. If we do not add
export const dynamic = "force-dynamic"
then process.env.APP_ENV will resolve to undefined since Next.js treats this as a static route since it pre-renders it at build time.
But the whole idea behind environment variables are that they are at runtime.
import type { MetadataRoute } from "next"
export default function robots(): MetadataRoute.Robots {
// Staging should not be indexed
if (process.env.APP_ENV !== "production") {
return { rules: [{ userAgent: "*", disallow: "/" }] }
}
return {
rules: [{ userAgent: "*", allow: "/" }],
}
}