envsafe
envsafe copied to clipboard
Add Option to Disable Fallback Mechanism in Next.js envsafe
Problem
I'd like to highlight an issue that arises when passing environment variables to envsafe
in Next.js, using the code from the Example as a reference.
In Next.js applications, we use the input
option to inline environment variable values during the build process and pass them to envsafe
.
NEXT_PUBLIC_NO_DEFAULT: str({
input: process.env.NEXT_PUBLIC_NO_DEFAULT,
}),
However, if there happens to be a typo in the specified environment variable name, envsafe is unable to identify the error.γFor instance, consider the following scenario where the PABLIC
segment is incorrect (while PUBLIC
is the correct one):
NEXT_PUBLIC_NO_DEFAULT: str({
input: process.env.NEXT_PABLIC_NO_DEFAULT,
}),
The reason behind this behavior is that envsafe
relies on a fallback mechanism where it references the environment variable using NEXT_PUBLIC_NO_DEFAULT
when the input
value is empty.
Unfortunately, this fallback mechanism is ineffective for Next.js client-side applications. It causes delays in issue detection and creates unnecessary obstacles in such scenarios.
Proposal
I propose introducing an option that allows users to disable the fallback mechanism at their discretion. By adding, for instance, the inputOnly
option, we give users the ability to control this behavior.
export const browserEnv = envsafe({
NEXT_PUBLIC_NO_DEFAULT: str({
input: process.env.NEXT_PUBLIC_NO_DEFAULT,
// Only individual items can be targeted
// inputOnly: true
}),
}, {
// By setting this flag to `true`, only the input value will be referenced, and the value in process.env will be ignored.
inputOnly: true
})
Utilizing this feature empowers Next.js application builders to detect typographical errors in environment variables during the build process, treating them as build errors rather than runtime issues.
Pull Request
- https://github.com/KATT/envsafe/pull/144
I believe that the majority of cases where input
is used are with Next.js.
Therefore, I thought it would be a good idea to make it the default behavior to disable fallback when using input
.
However, I am not certain that anyone would find it harmful, so I opted to add the flag so as not to break the existing behavior.