GithubHelp home page GithubHelp logo

expatfile / next-runtime-env Goto Github PK

View Code? Open in Web Editor NEW
407.0 2.0 15.0 36.91 MB

Next.js Runtime Environment Configuration - Populates your environment at runtime rather than build time.

Home Page: https://www.npmjs.com/package/next-runtime-env

License: MIT License

JavaScript 6.06% TypeScript 91.45% Shell 2.49%

next-runtime-env's Introduction

GitHub branch checks state codecov Known Vulnerabilities

๐ŸŒ Next.js Runtime Environment Configuration

Effortlessly populate your environment at runtime, not just at build time, with next-runtime-env.

๐ŸŒŸ Highlights:

  • Isomorphic Design: Works seamlessly on both server and browser, and even in middleware.
  • Next.js 13 & 14 Ready: Fully compatible with the latest Next.js features.
  • .env Friendly: Use .env files during development, just like standard Next.js.

๐Ÿค” Why next-runtime-env?

In the modern software development landscape, the "Build once, deploy many" philosophy is key. This principle, essential for easy deployment and testability, is a cornerstone of continuous delivery and is embraced by the twelve-factor methodology. However, front-end development, particularly with Next.js, often lacks support for this - requiring separate builds for different environments. next-runtime-env is our solution to bridge this gap in Next.js.

๐Ÿ“ฆ Introducing next-runtime-env

next-runtime-env dynamically injects environment variables into your Next.js application at runtime. This approach adheres to the "build once, deploy many" principle, allowing the same build to be used across various environments without rebuilds.

๐Ÿค Compatibility Notes

๐Ÿ”– Version Guide

  • 1.x: Next.js 12/13 Page Router
  • 2.x: Next.js 13 App Router
  • 3.x: Next.js 14 with advanced caching

๐Ÿš€ Getting Started

In your app/layout.tsx, add:

// app/layout.tsx
import { PublicEnvScript } from 'next-runtime-env';

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <head>
        <PublicEnvScript />
      </head>
      <body>
        {children}
      </body>
    </html>
  );
}

The PublicEnvScript component automatically exposes all environment variables prefixed with NEXT_PUBLIC_ to the browser. For custom variable exposure, refer to EXPOSING_CUSTOM_ENV.md.

๐Ÿง‘โ€๐Ÿ’ป Usage

Access your environment variables easily:

// app/client-page.tsx
'use client';
import { env } from 'next-runtime-env';

export default function SomePage() {
  const NEXT_PUBLIC_FOO = env('NEXT_PUBLIC_FOO');
  return <main>NEXT_PUBLIC_FOO: {NEXT_PUBLIC_FOO}</main>;
}

๐Ÿ›  Utilities

Need to expose non-prefixed environment variables to the browser? Check out MAKING_ENV_PUBLIC.md.

๐Ÿ‘ท Maintenance

next-runtime-env is proudly maintained by Expatfile.tax, the leading US expat tax e-filing software.

๐Ÿ“š Acknowledgments

Kudos to the react-env project for the inspiration, and a shoutout to @andonirdgz for the innovative context provider idea!


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.