/** * We define SCANDIC_ENV_URL for stable environments on Netlify: * production, stage and test. Avoid using SCANDIC_ENV_URL locally. * For deployments to those branches we have SCANDIC_ENV_URL defined. * Otherwise we fallback to DEPLOY_PRIME_URL from Netlify built-in variables. * Locally we set DEPLOY_PRIME_URL * * We set NEXTAUTH_URL here because build time environment variables are not * available runtime in Netlify functions. This leads to use being unable to * login on preview deployments. * * This approach updates the .env file in the root of the repo and tells * Netlify to include this file in the bundled function output * (see netlify.toml). This way Next.js will read .env runtime and update the * environment accordingly making the value available at runtime. */ import fs from "node:fs" // This is set for stable environments, e.g. test, stage, production const SCANDIC_ENV_URL = process.env.SCANDIC_ENV_URL // This is set for stable environments, e.g. test, stage, production let NEXTAUTH_URL = process.env.NEXTAUTH_URL // This is used for preview deployments const DEPLOY_PRIME_URL = process.env.DEPLOY_PRIME_URL const URL = SCANDIC_ENV_URL || DEPLOY_PRIME_URL if (!NEXTAUTH_URL) { NEXTAUTH_URL = URL + "/api/web/auth" } let contents = fs.readFileSync("./.env", { encoding: "utf-8" }) console.log({ pre_contents: contents }) console.log({ SCANDIC_ENV_URL, NEXTAUTH_URL, DEPLOY_PRIME_URL, URL, }) contents = contents.replace( // 'URL="REPLACEME-ON-BUILD-ON-NETLIFY"', /URL="[^"]+"/, `URL="${URL}"` ) contents = contents.replace( // 'NEXTAUTH_URL="REPLACEME-ON-BUILD-ON-NETLIFY"', /NEXTAUTH_URL="[^"]+"/, `NEXTAUTH_URL="${NEXTAUTH_URL}"` ) console.log({ post_contents: contents }) fs.writeFileSync("./.env", contents, { encoding: "utf-8" })