Skip to content

does not find env vars when deployed to cloudflare using nextjs connector #4877

@NickCrews

Description

@NickCrews

Preliminary Checks

Reproduction

zipped repo attached below

Publishable key

pk_test_Zm9uZC1tYW50aXMtNTIuY2xlcmsuYWNjb3VudHMuZGV2JA

Description

I think this is basically a nextjs version of this issue fro the remix connector.

Steps to reproduce:

  1. unzip this repo (which I scaffolded from npm create cloudflare@latest -- my-next-app --framework=next per cloudflare's guide, and then followed clerk's nextjs instructions)
  2. add the needed secret key, which I obfuscated.
  3. run npm run preview which builds the nextjs app and then does some cloudflare magic to make the middleware functions run on cloudflare workers [if I understand correctly...]
  4. open the localhost link.

Expected behavior:

Clerk will find the env vars I set in .env, and the login widget etc will work.

Actual behavior:

I get a 500 error because the secret key is not found. Clerk only looks within process.env for the needed env vars. But I am deploying to cloudflare workers, in which process.env is empty. To get this to work correctly, I instead of putting the env vars in .env, I need to put them in .dev.vars, which is cloudflare's version of this.

I think the solution is the same as in the linked issue above, where clerk looks through context.cloudflare.env instead. To really make this robust, if possible, really I think this logic for fetching env vars should get hoisted to some common util package where all the different framework connectors can use it, and so it will be solved for all of them.

Environment

System:
    OS: macOS 14.6.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 10.16 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 23.5.0 - /opt/homebrew/bin/node
    npm: 10.9.2 - /opt/homebrew/bin/npm
    pnpm: 9.15.1 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 131.0.6778.265
    Safari: 17.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions