Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: NEXT_PUBLIC variable access #94

Merged
merged 1 commit into from
Sep 26, 2024
Merged

fix: NEXT_PUBLIC variable access #94

merged 1 commit into from
Sep 26, 2024

Conversation

ijxy
Copy link
Contributor

@ijxy ijxy commented Sep 26, 2024

Ensure that the NEXT_PUBLIC_ variable is inlined during builds.

https://nextjs.org/docs/app/building-your-application/configuring/environment-variables#bundling-environment-variables-for-the-browser

(The above link specifically talks about the browser, but the inlining of NEXT_PUBLIC variables also applies to middleware).

Ensure that the `NEXT_PUBLIC_` variable is inlined during builds.

https://nextjs.org/docs/app/building-your-application/configuring/environment-variables#bundling-environment-variables-for-the-browser

(The above link specifically talks about the browser, but the inlining of NEXT_PUBLIC variables also applies to middleware).
@ijxy
Copy link
Contributor Author

ijxy commented Sep 26, 2024

@PaulAsjes I tried out the new version and ran into an issue with the variable still being undefined.

const getEnv = (name: string) => process.env[name];

export function middleware(_request: NextRequest, _event: NextFetchEvent) {
  console.log({
    NEXT_PUBLIC_WORKOS_REDIRECT_URI: process.env.NEXT_PUBLIC_WORKOS_REDIRECT_URI,
    NEXT_PUBLIC_WORKOS_REDIRECT_URI_2: getEnv("NEXT_PUBLIC_WORKOS_REDIRECT_URI"),
  });
}

leads to

{
  NEXT_PUBLIC_WORKOS_REDIRECT_URI: 'https://***-hrs2cwz03-***.vercel.app/api/auth/callback',
  NEXT_PUBLIC_WORKOS_REDIRECT_URI_2: undefined
}

@@ -11,7 +11,7 @@ const WORKOS_COOKIE_DOMAIN = getEnvVariable('WORKOS_COOKIE_DOMAIN');
const WORKOS_COOKIE_MAX_AGE = getEnvVariable('WORKOS_COOKIE_MAX_AGE');
const WORKOS_COOKIE_NAME = getEnvVariable('WORKOS_COOKIE_NAME');
const WORKOS_COOKIE_PASSWORD = getEnvVariable('WORKOS_COOKIE_PASSWORD') ?? '';
const WORKOS_REDIRECT_URI = getEnvVariable('NEXT_PUBLIC_WORKOS_REDIRECT_URI') ?? '';
const WORKOS_REDIRECT_URI = process.env.NEXT_PUBLIC_WORKOS_REDIRECT_URI ?? '';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is baffling, I wonder why process.env[name] isn't working as expected. Since getEnvVariable doesn't add much anymore, would you mind replacing every instance in this file with a direct lookup of process.env? e.g.

Suggested change
const WORKOS_REDIRECT_URI = process.env.NEXT_PUBLIC_WORKOS_REDIRECT_URI ?? '';
const WORKOS_API_HOSTNAME = process.env..WORKOS_API_HOSTNAME;
const WORKOS_API_HTTPS = process.env.WORKOS_API_HTTPS;
const WORKOS_API_KEY = process.env.WORKOS_API_KEY ?? '';
const WORKOS_API_PORT = process.env.WORKOS_API_PORT;
const WORKOS_CLIENT_ID = process.env.WORKOS_CLIENT_ID ?? '';
const WORKOS_COOKIE_DOMAIN = process.env.WORKOS_COOKIE_DOMAIN;
const WORKOS_COOKIE_MAX_AGE = process.env.WORKOS_COOKIE_MAX_AGE;
const WORKOS_COOKIE_NAME = process.env.WORKOS_COOKIE_NAME;
const WORKOS_COOKIE_PASSWORD = process.env.WORKOS_COOKIE_PASSWORD ?? '';
const WORKOS_REDIRECT_URI = process.env.NEXT_PUBLIC_WORKOS_REDIRECT_URI ?? '';

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't see the nextjs docs you linked, makes sense now. Thanks for catching!

@PaulAsjes PaulAsjes merged commit d652c43 into workos:main Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants