Netlify Functions - Env Var to indicate to function that it is in Netlify

On other platforms, like Vercel and Cloudflare Pages, an environment variable is defined so that an application knows which platform it is on. Respectively, these are VERCEL and CF_PAGES.

Netlify defines the NETLIFY environment variable at build time, but this is not available to Netlify Functions executing at run time. This might be useful in scenarios when a function might need to know which platform it is executing in.

There is one other use case for this: Auth.js (on which NextAuth.js is based) configures itself to trust forwarded headers from a reverse proxy by inferring the platform it is running on, based on the environment variables mentioned earlier. On Netlify however, a user will have to explicitly set AUTH_TRUST_HOST to true.

If Netlify can define an env var for functions to indicate that Netlify is indeed hosting them, Auth.js can be patched to automatically trust forwarded headers for Netlify, like equivalent platforms, so that Next.js applications that rely on NextAuth.js would just work.

Netlify has a global variable named Netlify that’s defined. Is should be possible to do something like: if (typeof Netlify !== 'undefined') {}

Interesting, thanks for that. Given that NextAuth.js is implemented with TypeScript, I cannot reference the Netlify global without the compiler complaining about a name that cannot be found, or making a explicit module declaration. Either way, it would somewhat complicate their code determining whether to trust host, by testing more than just env vars.

Given this, I would still look forward to the introduction of an env var proper, so that eventually, Netlify Next.js users depending on NextAuth.js can use it out of the box.

I’ve asked the devs if they’ll consider this.