chore(debug): envs in api functions

This commit is contained in:
Michael Zetterberg
2024-05-17 15:16:51 +02:00
parent 53ffe519cd
commit 62a8374144
6 changed files with 66 additions and 38 deletions

3
.env
View File

@@ -1 +1,4 @@
# See update-dotenv.mjs
ENVTEST="value from .env.api"
URL="REPLACEME-ON-BUILD-ON-NETLIFY"
NEXTAUTH_URL="REPLACEME-ON-BUILD-ON-NETLIFY"

View File

@@ -6,19 +6,19 @@ import type { NextRequest } from "next/server"
export async function GET(request: NextRequest) {
const e = process.env
const envVar = "ENVTEST"
const urlVar = "URL"
const nextAuthUrlVar = "NEXTAUTH_URL"
const values = {
env: env.ENVTEST,
static: process.env.ENVTEST,
dynamic: e[envVar],
env_url: env.URL,
static_url: process.env.URL,
dynamic_url: e[urlVar],
env_nextauth: env.NEXTAUTH_URL,
static_nextauth: process.env.NEXTAUTH_URL,
dynamic_nextauth: e[nextAuthUrlVar],
}
console.log({
env: env.ENVTEST,
static: process.env.ENVTEST,
dynamic: e[envVar],
})
console.log(values)
return NextResponse.json(values)
}

2
env/server.ts vendored
View File

@@ -34,6 +34,7 @@ export const env = createEnv({
.default("false"),
NEXTAUTH_REDIRECT_PROXY_URL: z.string().optional(),
NEXTAUTH_SECRET: z.string(),
NEXTAUTH_URL: z.string().optional(),
NODE_ENV: z.enum(["development", "test", "production"]),
PRINT_QUERY: z
.string()
@@ -70,6 +71,7 @@ export const env = createEnv({
NEXTAUTH_DEBUG: process.env.NEXTAUTH_DEBUG,
NEXTAUTH_REDIRECT_PROXY_URL: process.env.NEXTAUTH_REDIRECT_PROXY_URL,
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
NODE_ENV: process.env.NODE_ENV,
PRINT_QUERY: process.env.PRINT_QUERY,
REVALIDATE_SECRET: process.env.REVALIDATE_SECRET,

View File

@@ -1,40 +1,14 @@
import createJiti from "jiti"
import { login } from "./constants/routes/handleAuth.js"
import {
benefits,
myPages,
overview,
profile,
profileEdit,
stays,
} from "./constants/routes/myPages.js"
import { myPages, overview } from "./constants/routes/myPages.js"
const jiti = createJiti(new URL(import.meta.url).pathname)
jiti("./env/server")
jiti("./env/client")
// 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
const ENV_URL = process.env.SCANDIC_ENV_URL || process.env.DEPLOY_PRIME_URL
console.log({ config_SCANDIC_ENV_URL: process.env.SCANDIC_ENV_URL })
console.log({ config_DEPLOY_PRIME_URL: process.env.DEPLOY_PRIME_URL })
console.log({ ENV_URL })
// We set NEXTAUTH_URL here because next.config.js is included in Netlify
// functions when bundling. Otherwise we are unable to login on preview
// deployments. Netlify`s Next.js Runtime has built-in support for Next-Auth,
// but Next-Auth v5 is ESM and therefore not yet handle correctly by Netlify.
// This workaround should not be needed once Netlify fixes their code.
/** @type {import('next').NextConfig} */
const nextConfig = {
env: {
URL: ENV_URL,
NEXTAUTH_URL: `${ENV_URL}/api/web/auth`,
},
poweredByHeader: false,
eslint: { ignoreDuringBuilds: true },
images: {

View File

@@ -4,7 +4,7 @@
"private": true,
"type": "module",
"scripts": {
"prebuild": "npm run lint",
"prebuild": "npm run update-dotenv && npm run lint",
"build": "next build",
"predev": "rm -rf .next",
"dev": "PORT=3000 NEXT_PUBLIC_PORT=3000 next dev",
@@ -19,7 +19,8 @@
"test:e2e": "start-server-and-test test:setup http://127.0.0.1:3000/en/test \"cypress open --e2e\"",
"test:e2e:headless": "start-server-and-test test:setup http://127.0.0.1:3000/en/test \"cypress run --e2e\"",
"test:setup": "npm run build && npm run start",
"preinstall": "export $(cat .env.local | grep -v '^#' | xargs)"
"preinstall": "export $(cat .env.local | grep -v '^#' | xargs)",
"update-dotenv": "node update-dotenv.mjs"
},
"dependencies": {
"@contentstack/live-preview-utils": "^1.4.0",

48
update-dotenv.mjs Normal file
View File

@@ -0,0 +1,48 @@
/**
* 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" })
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}"`
)
fs.writeFileSync("./.env", contents, { encoding: "utf-8" })