Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 5c61c78

Browse files
committed
add getEnvVar function
1 parent 4cb76bc commit 5c61c78

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

lib/functions.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,34 @@
22
export const fetcher = (...args) => fetch(...args).then((res) => res.json()) // eslint-disable-line no-undef
33

44
/**
5-
* On scroll, add or remove a "shrink" class.
5+
* Retrieve environment-specific var.
66
*
7-
* @param {object} el The header element.
7+
* @param {string} varName Environment variable.
8+
* @param {bool} isPublic Whether var is public.
9+
* @return {string} Env var value.
810
*/
9-
export function shrinkHeader(el) {
10-
/* eslint-disable */
11-
window.addEventListener('scroll', () => {
12-
if (window.scrollY > 30) {
13-
el.current.classList.add('shrink')
14-
} else {
15-
el.current.classList.remove('shrink')
16-
}
17-
})
18-
/* eslint-enable */
11+
export function getEnvVar(varName, isPublic = false) {
12+
const prefix = isPublic ? 'NEXT_PUBLIC_' : ''
13+
14+
// If var missing or currently in Vercel "dev" (local), use local settings.
15+
if (!process.env.VERCEL_ENV || 'development' === process.env.VERCEL_ENV) {
16+
return process.env[`${prefix}LOCAL_${varName}`]
17+
}
18+
19+
// Prod / main / default.
20+
if (
21+
'production' === process.env.VERCEL_ENV ||
22+
'preview' !== process.env.VERCEL_ENV
23+
) {
24+
return process.env[`${prefix}PROD_${varName}`]
25+
}
26+
27+
// Switch between staging and develop in Vercel "preview" env.
28+
switch (process.env.VERCEL_GITHUB_COMMIT_REF) {
29+
case 'staging':
30+
return process.env[`${prefix}STAGING_${varName}`]
31+
case 'develop':
32+
default:
33+
return process.env[`${prefix}DEV_${varName}`]
34+
}
1935
}

0 commit comments

Comments
 (0)