GithubHelp home page GithubHelp logo

Comments (5)

HofmannZ avatar HofmannZ commented on June 17, 2024 2

This is due to how nextjs' build process works. They still set the public envs to a static string if they are declared in your .env and prefixed with NEXT_PUBLIC_.

There are two solutions:

  1. Remove the .env file.
  2. Remove the NEXT_PUBLIC_ prefix.

If you choose option 2, and you need to access the env var on both the server and the client, you can use the makeEnvPublic utility to automatically create the prefixed variable.

Let me know if that helps ✌️

from next-runtime-env.

HofmannZ avatar HofmannZ commented on June 17, 2024

I'm not able to reproduce your issue. To confirm I've added a route handler to the example.

You can try it out yourself:

  1. Clone the repo
  2. Checkout the development branch
  3. cd to the example with-app-router
  4. Run pnpm build
  5. Run NEXT_PUBLIC_FOO=foo-value BAR=bar-value BAZ=baz-value pnpm start
  6. Visit http://localhost:3000/api/bar-baz and see the values of the runtime env vars returned.

Can you check if your implementation differs from the example?

from next-runtime-env.

bagelbits avatar bagelbits commented on June 17, 2024

@HofmannZ I found where my implementation differs, but I don't know if this is working as intended.

Steps to replicate

  1. Add a .env.local file:
NEXT_PUBLIC_FOO="yes"
BAR="hello"
BAZ="world"
  1. Change examples/with-app-router/src/app/api/bar-baz to:
export async function GET() {
  return Response.json({
    bar: process.env.BAR,
    baz: process.env.BAZ,
    foo: process.env.NEXT_PUBLIC_FOO,
  });
}

export const dynamic = 'force-dynamic';
  1. pnpm build
  2. NEXT_PUBLIC_FOO=foo-value BAR=bar-value BAZ=baz-value pnpm start

Expected

{"bar":"bar-value","baz":"baz-value","foo":"foo-value"}

Actual

{"bar":"bar-value","baz":"baz-value","foo":"yes"}

from next-runtime-env.

bagelbits avatar bagelbits commented on June 17, 2024

@HofmannZ Amusingly, if you don't have the .env.local file, this works just fine. But it's inconsistent that .env.local overrides the NEXT_PUBLIC_* but not the normal ENV vars. Is that just the nature of how NEXT_PUBLIC_* works?

from next-runtime-env.

bagelbits avatar bagelbits commented on June 17, 2024

@HofmannZ That 100% worked! Thank you so much for the clarification!

from next-runtime-env.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.