/** * 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" if (process.env.NETLIFY) { const PUBLIC_URLS = { production: "https://www.scandichotels.com", stage: "https://stage.scandichotels.com", test: "https://test2.scandichotels.com", } let PUBLIC_URL if (PUBLIC_URLS[process.env.CONTEXT]) { PUBLIC_URL = PUBLIC_URLS[process.env.CONTEXT] } else if (PUBLIC_URLS[process.env.BRANCH]) { PUBLIC_URL = PUBLIC_URLS[process.env.BRANCH] } else { PUBLIC_URL = process.env.DEPLOY_PRIME_URL } const NEXTAUTH_URL = `${PUBLIC_URL}/api/web/auth` let contents = fs.readFileSync("./.env", { encoding: "utf-8" }) console.log({ pre_contents: contents }) console.log({ process_env: process.env }) contents = contents.replace(/PUBLIC_URL=.*/, `PUBLIC_URL="${PUBLIC_URL}"`) contents = contents.replace( /NEXTAUTH_URL=.*/, `NEXTAUTH_URL="${NEXTAUTH_URL}"` ) console.log({ post_contents: contents }) fs.writeFileSync("./.env", contents, { encoding: "utf-8" }) }