Next.js Tutorial - Part 10 | Environment Variables and Runtime Configuration

Поделиться
HTML-код
  • Опубликовано: 31 янв 2025

Комментарии • 102

  • @BrunoAntunesPT
    @BrunoAntunesPT  4 года назад +9

    Attention: After Next 9.4 environment variables in Next.js become much easier than before. This video is accurate up to 9.3 only!!
    For new environment versions you can check: nextjs.org/docs/basic-features/environment-variables

    • @LeroyBaeyens
      @LeroyBaeyens 3 года назад +1

      Those new NEXT_PUBLIC_ variables are only build time variables, not runtime. So your video is still accurate about runtime variables I believe. And publicRuntime variables are only used when using serverSideProps or getInitialProps. At least, that's what I understand about them.

    • @BrunoAntunesPT
      @BrunoAntunesPT  3 года назад +1

      You are 100% correct, the NEXT_PUBLIC_MY_VAR, will only be set at build time and you can't "override" that one at build time :)
      Now in Next.js, your other environment variables (the ones that don't start by NEXT_PUBLIC_) you can set them at build time and "override" at runtime, without having to use the serverRuntimeConfig :)
      I started to code an example here on the youtube comment section, but youtube comments destroyed the code formatting completely, so I pushed into github: github.com/bmvantunes/nextjs-env-variables-youtube-comment-example/commit/0641758e198f0977d88f298959a779d56b8c3cf6
      Let me know if you can see those 2 files in the commit.
      I'm setting 2 environment variables at build time, and then "overriding" them at runtime, one is NEXT_PUBLIC_, the other isn't.
      By running "npm run build && npm start" and navigating to your browser on localhost:3000/youtube-comment
      The result:
      ```
      NEXT_PUBLIC_VAR1 = public-build-var1
      FROM SERVER: NEXT_PUBLIC_VAR1 = public-build-var1
      ---------------------
      VAR1 = -----
      FROM SERVER: VAR1 = private-runtime-bruno
      ```
      As you can see the NEXT_PUBLIC_ variables, we can't override at runtime, but all the others we can :)
      That means that we don't need to use serverRuntimeConfig anymore. Regarding publicRuntimeConfig we only need to use it if we don't wan't/can't pass those from the server like I did in this example. That being said, publicRuntimeConfig still has it's place, but we can overcome as I did on this example =)
      If you go the other page there using getStaticProps (localhost:3000/youtube-comment-getstaticprops) - the code is exactly the same, but replacing getServerSideProps by getStaticProps, the result will be:
      ```
      NEXT_PUBLIC_VAR1 = public-build-var1
      FROM SERVER: NEXT_PUBLIC_VAR1 = public-build-var1
      ---------------------
      VAR1 = -----
      FROM SERVER: VAR1 = private-build-bruno
      ```
      In this page, as expected the only values are the ones set at build time =)
      I know I wrote a bit too much in this comment, but I hope this example helps, if it doesn't, please let me know and we can go in more detail =D

    • @LeroyBaeyens
      @LeroyBaeyens 3 года назад

      Great example! Let me explain my use-case (which might be a bit out-of-scope, but it describes my issues).
      First let me explain the architecture.
      I use DTAP, so I have different environments. Let's limit it to test and production. Both of those environments have a graphql server (not build in nextjs) and a nextjs app.
      I build using docker and deploy to kubernetes (doesn't matter where I deploy actually).
      Now let me explain the NextJs app
      I have an ApolloProvider in `_app.js` which is configured to use an url from env variables. Because this graphql server is not part of the NextJS app, but is hosted elsewhere on another domain, I need to configure this in my application, during runtime. Because I don't want to build a different image per environment.
      I want to have server side rendering using that graphql server, and client side rendering using that same graphql server. Which means I can not use static pre-generation, because then the variables are unset in the browser. I always need server side rendering.
      I think I have to setup some graphql http-proxy, or use schema stitching with a graphql server also built in the nextjs app. Because then the graphql proxy or server can use server side variables to determine, to which graphql backend server I need to proxy to.

  • @justintime9720
    @justintime9720 4 года назад +2

    I am already using Next.js in production for already 2 years and it's great. I viewed part 1 to part 10 in 2 days and I can say that your content is awesome and it's a pleasure to watch even if I don't know typescript and I use MongoDB (with Mongoose)

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      Thank you my friend, it's great to hear! =)
      I hope you enjoy the next 6 videos I'm doing with Next.js in the series "Building a Car Trader App" =)

  • @RazerCrick
    @RazerCrick 4 года назад +1

    The whole series made me to like your video first, then watch. Great playlist Bruno. Blessings for you to get more success. Really helpfull👏

    • @BrunoAntunesPT
      @BrunoAntunesPT  3 года назад

      Thank you very much =D
      I really really appreciate your comments Amit!

  • @PrinceSodhi
    @PrinceSodhi 4 года назад +3

    Wow... I was waiting for this kind of tutorial for a long time. Thank you.

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      Thank you Prince 😊
      Glad it was helpful!

  • @JorgeErnestoAvila
    @JorgeErnestoAvila 4 года назад +1

    First, thanks for the index! I loved that i can just go ahead to what i needed. Very clear explanations and to the point. Keep em comin!

  • @jayow2k8
    @jayow2k8 4 года назад +2

    Love the content you're producing. Keep up the good work!

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      Thank you very much 😀😀
      keep in mind that after next 9.4 you can have environment variables in a easier way 😀 check the url I have in the pinned comment 😍😍😍

    • @jayow2k8
      @jayow2k8 4 года назад +1

      @@BrunoAntunesPT Moderately embarrassed I didn't check the doc's first, thanks for the quick heads up ❤️

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      No need to be embarrassed, we are not robots ❤️❤️😀

  • @rtnjo6936
    @rtnjo6936 4 года назад +2

    As always the best NextJS video, thank you!

  • @Blazephoenix
    @Blazephoenix 4 года назад +1

    You deserve way more subscribers. Amazing series!

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      Thank you soo much Tanmay 😍😍 I'm very happy you enjoyed the series 🙂😊

  • @senkasim
    @senkasim 4 года назад +2

    Thank you very much for your efforts 🙏 . I get superb information about Next.js with every new video ✨.

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      Thank you Kasim 😊 that's great to read my friend 😁😁

  • @adilsaeed31
    @adilsaeed31 4 года назад

    I am waiting for your next video. I am super excited about that.

  • @monicasilvestre1699
    @monicasilvestre1699 4 года назад +1

    Looking forward to the next series
    ❤️

  • @tamertamer9886
    @tamertamer9886 3 года назад +1

    I love your laugh and your explanation
    you are awesome

  • @abessesmahi4888
    @abessesmahi4888 4 года назад +1

    Thank you so much for this awesome series

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      Thank you for the support Abesse 😊 ☺️☺️

  • @sinaebr7337
    @sinaebr7337 4 года назад +1

    Thank you so much Bruno. awesome video.

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      Thank you Sina 😊😊

    • @sinaebr7337
      @sinaebr7337 4 года назад +1

      @@BrunoAntunesPT hey Bruno what's up ?
      are you ok?

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      I'm not too bad, but I can't say the same about my dell (computer) 😩 I think it will "die" sooner or later - I think I'm getting closer and closer to go back to apple after this 2 years using Windows 🤣🤣

  • @tslqs
    @tslqs 4 года назад +2

    Thank you so much, you are awesome :)

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      Thank you very much Oros 😀😀😀 you are awesome as well, never forget it ❤️❤️

  • @xnxbxs_zx
    @xnxbxs_zx 3 года назад +1

    Thank you 🙌

  • @sergiob3698
    @sergiob3698 4 года назад

    Bruno digging deeper in the type of env var you described, there are some ones which are available at build time and some ones at run time.
    I'm not used to have var at build time, thinking a use case for them could be the database connection or api endpoint to be consumed when generating static content (getStaticProps / getStaticPaths) Otherwise what could be something like step useful?
    That being said, we need to build for each env we have, I was used to have the same bin and deploy with different configuration across different envs.
    Sorry if the question is out of scope but could you please clarify it?

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      Really good question Sergio =)
      Basically we need to differentiate what is front-end code and back-end code.
      For example, imagine you have a react application, most of the times that application is served from a CDN - where you don't have any environment variables at all (Your react code is not running there, only being served as static files from there). The same kinda applies if you are serving a static react app from your server (react will not be running on your server).
      In the scenario I described before, environment variables at build time are your only option - If you don't want to use environment variables at build time then when your react application runs the first time, it needs to make an HTTP call to your server to get the environment variables, which is a bit of a performance hit for your users - that being said a lot of companies do this =)
      For code that will run on your server (or anywhere you control), you can have runtime environment variables.
      Next.js itself, has SSR (server side rendering), so you can use runtime environment variables in those scenarios. But in scenarios where you use SSG (static site generation) and you deploy to a CDN or github pages (for example), you have no access/control to runtime environment variables after deployment. You can look at this example (from the video I'm editing today) - bmvantunes.github.io/microphone-store
      That example, is an application that we built using static site generation (SSG), so I can't access any runtime environment variables in github pages. If I deploy that application to multiple environments I would have to have a build system per environment and build that app multiple times, one per environment (for example, because the databases will be different for each environment)
      Sorry for the very long answer, I hope it helps you =D
      PS - I agree that the industry should call different names to build time environment variables and runtime environment variables, the similar names might confuse people =D

    • @sergiob3698
      @sergiob3698 4 года назад +1

      ​@@BrunoAntunesPT Thank you Bruno, for such complete explanation. I think I have a lot of questions now , but the main question and general idea is much more clear!
      Glad to hear you are working on new videos!

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      Thank you Sergio 😀
      Yes, during the last 3 months without going outside as normal, not able to visit family and friends my brain has been a bit foggy and not that productive for RUclips videos 😔
      I'm sorry for that - hopefully my brain will be able to get back to speed in the next few weeks 😉😉

    • @sergiob3698
      @sergiob3698 4 года назад +1

      @@BrunoAntunesPT Yes, I'm not an expert but I think that keeping active trying to do what we love can help to overcome these difficult moments.
      Take care!

  • @ApurvKhare
    @ApurvKhare 4 года назад

    I have always used dotenv. Never knew something like crossenv existed. Still, I'd prefer dotenv. But thanks for teaching crossenv.

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      In the newer versions of next you don't need half of this complication. I need to re-do a video about env variables 😄

    • @ApurvKhare
      @ApurvKhare 4 года назад +1

      @@BrunoAntunesPT ya I read the docs from your comment. Thanks for updating in the comment

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      Yes I always put comments in the videos if something changes, even if it's very minor 😀in this case it's not minor 😅😅

  • @jitpomi
    @jitpomi 4 года назад +1

    I love this. It couldn't be explained better than this. Thanks Bruno... Just to mention: Since we are here customizing *next.config.js* I think we could use the opportunity to get rid of the anoying *relative path* imports for our custom files. that way we do not have to worry about correct path referencing... Just incase anyone is interested, here is a gist in which I customize the *webpack config* and *tsconfig* to allow for *absolute paths* imports:
    gist.github.com/jermsam/073c717f98d8a32c063daba96b9a3294

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      I think you'll love to know that you no longer need to do anything inside next.config.js in order for that to work (since next 9.4) 😍😍
      Just add a baseUrl in your tsconfig.json (or jsconfig.json if not using typescript). You can read more at: nextjs.org/blog/next-9-4#absolute-imports-and-aliases
      I love the work the next.js team is doing, absolutely amazing ♥️♥️♥️

  • @ruggeddog3103
    @ruggeddog3103 3 года назад +1

    but what about .env.local next js docs says it comes with it by defualt and thanks for the video as always good stuff!

    • @BrunoAntunesPT
      @BrunoAntunesPT  3 года назад

      Thank you =)
      Please read the pinned comment in this video =D You'll understand why I didn't talk about .env.local - Basically it was not launched yet at the time =D

    • @ruggeddog3103
      @ruggeddog3103 3 года назад +1

      @@BrunoAntunesPT oh ok 👍

    • @BrunoAntunesPT
      @BrunoAntunesPT  3 года назад

      @@ruggeddog3103 no problem my friend 😅😅

  • @Salman-dn3md
    @Salman-dn3md 2 года назад

    Could anyone please tell me how can I change .env file values after build without performing rebuild

  • @AhSoh7091
    @AhSoh7091 4 года назад

    May i confirm that, is it publicRuntimeConfig key value will not export in build file?

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +2

      After version 9.4 of next.js you don't need any of that, which is fantastic (I think I have a pinned comment in this video with the URL for the docs) =D

  • @adamtak3128
    @adamtak3128 4 года назад +1

    Can you please make a video on how to setup ApolloServer and ApolloClient?

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      Thank for the suggestion Adam. It will not be part of this series, but in the future we can look into it, for sure! 😁

    • @adamtak3128
      @adamtak3128 4 года назад

      @@BrunoAntunesPT Awesome thanks.

  • @bernardamofah4524
    @bernardamofah4524 4 года назад

    Thanks for another great tutorial.... i think the repository link in the description is pointing to part-9.

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      Thank you very much my friend :)
      I updated the URL to point to part 10 :D

  • @larbisahli2273
    @larbisahli2273 4 года назад

    What is the difference between next.config.js and .env.local in next?

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      In next.config.js you can configure "everything" related with next.js - including Webpack.
      The .env file is only to set environment variables 😊

    • @larbisahli2273
      @larbisahli2273 4 года назад

      @@BrunoAntunesPT Hello Bruno, base on the next docs i think using environment variables in .env.local is much better than next.config.js.
      for example in .env.local:
      DB_PASS=mypassword (good for secrets)
      This loads process.env.DB_PASS into the Node.js environment
      .
      NEXT_PUBLIC_ANALYTICS_ID=abcdefghijk
      here the process.env.NEXT_PUBLIC_ANALYTICS_ID will be inlined into JavaScript sent to the browser because of the NEXT_PUBLIC_ prefix.

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      This video was recorded during next 9.3 days 😊
      The feature you are talking about (next public) is quite new - next 9.4 - you can read more at nextjs.org/blog/next-9-4#new-environment-variables-support

    • @sergiob3698
      @sergiob3698 4 года назад

      @@BrunoAntunesPT Oh No, so a video you made 4 months ago is not the best 'practice' any more. what a mess.

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +3

      Next.js is moving at a very rapid pace at this stage, which is great news
      Sadly, at the same time it is a bit frustrating for people learning it now because it's always changing 😅

  • @anhvuuc8693
    @anhvuuc8693 4 года назад

    :c Hope u can make a video how to connect to mongode use mongoose in future. Still watching your series, but stuck on connect to db now.
    Don't understand this error: ReferenceError: Cannot access 'mongoose' before initialization.

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      I'm sorry to hear that my friend :(
      At the moment I don't have any video planned using MongoDB - even tho, I'm sure I'll do one video about it, sooner or later :)
      In the meantime, I found a really nice guide using MongoDB and Next.js in the MongoDB website. I read though it and looks really really good - it even teaches you how to create a MongoDB instance in Atlas (their cloud service with a free tier).
      This is the URL to the article: developer.mongodb.com/how-to/nextjs-building-modern-applications
      Let me know if the article helped you out =)

  • @mirceastaicu4131
    @mirceastaicu4131 3 года назад

    If you want to grab something from your environment, you can grab something from your environment?

    • @BrunoAntunesPT
      @BrunoAntunesPT  3 года назад +1

      If I understood correctly your question, yes. All your environment variables will be available via process.env.YourVarName 🙂 but not sure if that was your question 🙂

  • @abdulrafay1951
    @abdulrafay1951 4 года назад +1

    Bruh, is it possible that you create a tutorial on hosting such react next.js app on aws or similar platform.

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      I'm hosting everything I have in vercel and azure at the moment 😊
      In vercel it is super super easy 😍😍😍
      I can make a video for azure, but I'm not working enough with aws at this moment 😅

    • @abdulrafay1951
      @abdulrafay1951 4 года назад +1

      @@BrunoAntunesPT Whatever you are using, show us. I get very confused when it comes to hosting React Next.js apps. Also, how we can update our site code after it's uploaded to azure/aws if we want to change anything.

    • @abdulrafay1951
      @abdulrafay1951 4 года назад +1

      @@BrunoAntunesPT I will really appreciate 😍😅😅

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      It sounds good. Now I have my plate a bit full... but as soon as I find a bit of time, I'll prepare a video about deployment 😊😊😊

    • @abdulrafay1951
      @abdulrafay1951 4 года назад +1

      @@BrunoAntunesPT sure, anytime. Until then I'm going to finish car trader series. :D

  • @juancarlosvidela8602
    @juancarlosvidela8602 4 года назад

    How can I do this in development? gives me a defined

  • @daniel-nagy
    @daniel-nagy 4 года назад +1

    9:32 next config dot J aaass :D

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      😂😂😂 RUclips automatic captions with file names and technologies gets a bit lost 😂😂😂

  • @refaat1297
    @refaat1297 4 года назад +1

    You are great
    how can make log out and use privileges like when I have
    index page: app.com
    home page: app.com => but content will change after login
    How can do that?
    I hope you understand my words and this situation

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      Thank you Ahmed 😊
      Regarding logout you'll need to store some kind of state in your backend (for example in a database). With that you'll know which tokens are valid and which ones are not. With that you can also do what applications like Facebook do - having the possibility to "logout" specific devices that you own.
      Regarding the other question a simple if else for your logged in state does the trick. You check if you are logged in, if you are show A if not show B.
      I hope this is helpful ☺️☺️

    • @refaat1297
      @refaat1297 4 года назад +1

      @@BrunoAntunesPT
      Thank you for replying ❤️
      I will do it
      If I have a problem I will let you know 😆
      We are looking for next video
      Thank you for help 💚

    • @sergiob3698
      @sergiob3698 4 года назад

      @@BrunoAntunesPT Isn't it possible just to remove the cookie when the user click logout? to have a simpler implementation...

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      Imagine someone has your previous valid jwt token they can still enter the application as you 😊
      So deleting only from your browser is not enough - if we are talking about jwt tokens 😅

    • @sergiob3698
      @sergiob3698 4 года назад +1

      @@BrunoAntunesPT Sure, I understand the scenario you describe.

  • @chidibede2417
    @chidibede2417 4 года назад

    Just to add, the new version made working with environment variables easy...nextjs.org/docs/basic-features/environment-variables

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад

      Yes, after 9.4 it is much easier. This video was recorded before 9.4 was out 😊

    • @chidibede2417
      @chidibede2417 4 года назад +1

      @@BrunoAntunesPT Ohh to add, you really saved my life, passed an interview because of you. I really appreciate it.

    • @BrunoAntunesPT
      @BrunoAntunesPT  4 года назад +1

      That's amazing!!! Congratulations my friend 😀😀 I'm really happy I could play a little part on it. It is amazing to know this stories ❤️❤️❤️
      BTW, I added a pinned comment for new viewers of the video with the url to the new env variables documentation. I may create a video to replace this one 😃😃

    • @chidibede2417
      @chidibede2417 4 года назад +1

      @@BrunoAntunesPT Thank you !!!!!