expatfile / next-runtime-env

Next.js Runtime Environment Configuration - Populates your environment at runtime rather than build time.
https://www.npmjs.com/package/next-runtime-env
MIT License
454 stars 18 forks source link

Can't refresh some pages #154

Open Tockra opened 1 month ago

Tockra commented 1 month ago

Hello,

after I built my application in a docker container and run it there, I have problems while refreshing some pages. I say some, because some pages can be reloaded, some pages not. I've no idea why.

But the console message in my browser indicates that some variables in my auth provider are not set correctly. But I am very confused why. It is caused by the env loading:

My provider:

"use client";

import React, { PropsWithChildren } from "react";

import { AuthProvider } from "react-oidc-context";

import { env } from "next-runtime-env";

export function AuthProviderWrapper({ children }: PropsWithChildren) {
  const NEXT_PUBLIC_AUTH_URL = env("NEXT_PUBLIC_AUTH_URL") ?? "";
  const NEXT_PUBLIC_AUTH_AUTHORITY_PATH =
    env("NEXT_PUBLIC_AUTH_AUTHORITY_PATH") ?? "";
  const NEXT_PUBLIC_AUTH_CLIENT_ID = env("NEXT_PUBLIC_AUTH_CLIENT_ID") ?? "";
  const NEXT_PUBLIC_AUTH_REDIRECT_URI =
    env("NEXT_PUBLIC_AUTH_REDIRECT_URI") ?? "";

  const oidcConfig = {
    authority: NEXT_PUBLIC_AUTH_URL + NEXT_PUBLIC_AUTH_AUTHORITY_PATH,
    client_id: NEXT_PUBLIC_AUTH_CLIENT_ID,
    redirect_uri: NEXT_PUBLIC_AUTH_REDIRECT_URI,
    loadUserInfo: true,
  };

  return <AuthProvider {...oidcConfig}>{children}</AuthProvider>;
}

I added some logging which says that after reload the page in the browser, my env("NEXT_PUBLIC_AUTH_URL") ?? ""(and all other calls) are evaluated to "" which causes my application to crash. Only if I visit my main page (mydomain.de) everything is loaded fine again. While navigation through the pages there is no issue.

Only on refresh the page. Maybe this is related with next-intl (???).

Does anybody has a hint how to fix that without removing the usage of next-runtime-env ?

T

tpjnorton commented 1 month ago

This is happening too for me - when I have my app deployed to Vercel sometimes on reload none of my runtime env vars are actually set - this disappears with a hard refresh, and sparingly with a regular refresh.

Tockra commented 1 month ago

I'm not sure anymore but my problem was caused by some Wrapper in my layout.tsx which was on the wrong level. After some cleanup there it worked again.