npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-dynamic-runtime-config

v0.3.0

Published

Dynamic Runtime config for Next.js applications

Downloads

70

Readme

Next.JS Dynamic Runtime Config

Provide hooks and configuration to allow for env vars and other forms of configuration to be exposed to the browser without requiring recompilation.

Rationale

With the deprecation of built-in runtime configuration, Next.js and Vercel have clearly signaled a desire to do per-environment builds for maximum performance optimization.

Teams building and deploying Next.js not into Vercel but instead into custom docker containers still need the ability to control client side configuration via runtime Docker env vars. Examples of runtime configurations include per-environment public keys (e.g. for google analytics, sentry) and dynamic api routing urls.

Getting Started

To install use your favorite package manager, ideally npm:

npm i next-dynamic-runtime-config

Setup

It is recommended you create two files in your src/ or lib/ directories of your next app.

The first file is where you set up the shared configuration. I recommend naming the file lib/config-register.ts:

import registerConfig from "next-dynamic-runtime-config/register";

const getRuntimeConfig = registerConfig({
  foo: process.env.NEXT_PUBLIC_FOO,
  bar: process.env.BAR,
  baz: process.env.NODE_ENV === "production" ? "prod" : "dev",
});

export default getRuntimeConfig;

The second file will be your most commonly access client file that will export your hook. I recommend naming the file lib/config-client.ts:

"use client";

import runtimeConfig from "./config";
import createProvider from "next-dynamic-runtime-config/provider";

const { RuntimeConfigContext, RuntimeConfigProvider, useRuntimeConfig } = createProvider(runtimeConfig);

export { RuntimeConfigContext, RuntimeConfigProvider, useRuntimeConfig };
export default useRuntimeConfig;

Next in your app/providers.tsx add the RuntimeConfigProvider:

import { RuntimeConfigProvider } from "@/lib/config";

export default function Providers({ children }) {
  return (
    <RuntimeConfigProvider>
      <FooProvider>{children}</FooProvider>
    </RuntimeConfigProvider>
  );
}

Finally, in your root layout you must initialize the "init" script in order to provide configuration to the client. As the init component provides a beforeInteractive script and it it MUST be placed in the root layout in order to ensure it is available on ALL client pages. Add the following to app/layout.tsx:

// ...snip...
import RuntimeConfigInit from "next-dynamic-runtime-config/init";
import getRuntimeConfig from "@/lib/config";

// ...snip...

export default function RootLayout({ children }: RootLayoutProps) {
  return (
    <html>
      {/* snip */}
      <body>
        <Providers>{children}</Providers>
        <RuntimeConfigInit config={getRuntimeConfig()} />
      </body>
    </html>
  );
}

Congratulations! You're now able to use the hook to access runtime configuration in your components, for example:

"use client";

import { useRuntimeConfig } from "@/src/config";

export default function ClientPage() {
  const { config, server } = useRuntimeConfig();
  return <div>Config Item foo: {config.foo}</div>;
}

However for Server components you can either access the same values you used to build your runtime config OR just import the registered config directly:

"use server";

import getRuntimeConfig from "@/lib/config-register";

export default async function ServerPage() {
  const config = getRuntimeConfig();
  return <div>Config Item foo: {config.foo}</div>;
}

Support

This project exists to serve my needs in projects I work on. I will do my best to support any issues or PRs, however this project may be abandoned at any time. It is simple enough and licensed appropriately that you should feel free to copy-paste the contents into your apps or fork this project if you are concerned about long-term support.