Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Option to Disable Fallback Mechanism in Next.js envsafe #146

Open
snaka opened this issue Jun 3, 2023 · 0 comments · May be fixed by #144
Open

Add Option to Disable Fallback Mechanism in Next.js envsafe #146

snaka opened this issue Jun 3, 2023 · 0 comments · May be fixed by #144

Comments

@snaka
Copy link

snaka commented Jun 3, 2023

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


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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant