Skip to content

Conversation

dario-piotrowicz
Copy link
Contributor

Fixes: #801

Copy link

changeset-bot bot commented Jul 25, 2025

🦋 Changeset detected

Latest commit: 945f849

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@opennextjs/cloudflare Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Jul 25, 2025

Open in StackBlitz

npm i https://pkg.pr.new/@opennextjs/cloudflare@818

commit: 945f849

Comment on lines 30 to 35
const platformProxyEnvVars = await getEnvFromPlatformProxy({
configPath: args.configPath,
environment: args.env,
});

const envVars = { ...platformProxyEnvVars, ...process.env };
Copy link
Contributor

Choose a reason for hiding this comment

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

upload.ts should be updated as well.

May be we could add a includeProcessEnv: Boolean to getEnvFromPlatformProxy so that we could document the purpose on the JSDoc of that functon.

I'm wondering if it should { ...platformProxyEnvVars, ...process.env } or { ...process.env, ...platformProxyEnvVars} - I would tend to prefer the latest as proxy vars are more specific as they could change with the environment but I don't have a very strong opinion on that. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually I'm wondering if we should rather update

if (!envVars.CF_WORKER_NAME) {
logger.error("CF_WORKER_NAME should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_PREVIEW_DOMAIN) {
logger.error("CF_PREVIEW_DOMAIN should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_WORKERS_SCRIPTS_API_TOKEN) {
logger.error("CF_WORKERS_SCRIPTS_API_TOKEN should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_ACCOUNT_ID) {
logger.error("CF_ACCOUNT_ID should be set when skew protection is enabled");
process.exit(1);
}

to read from either process.env of envVars.

That would be a more targeted change.

What are your thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

upload.ts should be updated as well.

Updated 👍

May be we could add a includeProcessEnv: Boolean to getEnvFromPlatformProxy so that we could document the purpose on the JSDoc of that functon.

What about renaming the function getEnvironmentVariables and have it always include values from both platformProxy and process.env? would there be an issue with that?

I'm wondering if it should { ...platformProxyEnvVars, ...process.env } or { ...process.env, ...platformProxyEnvVars} - I would tend to prefer the latest as proxy vars are more specific as they could change with the environment but I don't have a very strong opinion on that. What do you think?

I purposely put ...process.env as last since I think system environment variables should have precedence over those in the config files, for example if I run something line:

$ CF_ACCOUNT_ID="123" npm run deploy

I would personally expect that "123" would override a potential CF_ACCOUNT_ID value present in the config file.

I do think that this is the common behavior for this sort of things. Having the system environment variables also adds more flexibility to users, that could for example very easily deploy their worker onto different accounts (silly example):

$ CF_ACCOUNT_ID="123" npm run deploy
$ CF_ACCOUNT_ID="456" npm run deploy 

What would the benefits in having the platformProxy have precedence over process.env be?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually I'm wondering if we should rather update

if (!envVars.CF_WORKER_NAME) {
logger.error("CF_WORKER_NAME should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_PREVIEW_DOMAIN) {
logger.error("CF_PREVIEW_DOMAIN should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_WORKERS_SCRIPTS_API_TOKEN) {
logger.error("CF_WORKERS_SCRIPTS_API_TOKEN should be set when skew protection is enabled");
process.exit(1);
}
if (!envVars.CF_ACCOUNT_ID) {
logger.error("CF_ACCOUNT_ID should be set when skew protection is enabled");
process.exit(1);
}

to read from either process.env of envVars.

That would be a more targeted change.

What are your thoughts?

I think that generally it would make more sense to get the environment variables from both getPlatformProxy and process.env, actually generally process.env makes more sense to me if we're talking about build time variables, since the variables in getPlatformProxy are, in my opinion, runtime variables.

But if you prefer I'll update the code to limit the change to the skew protection logic.

Copy link
Contributor

@vicb vicb Jul 28, 2025

Choose a reason for hiding this comment

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

What about renaming the function getEnvironmentVariables and have it always include values from both platformProxy and process.env? would there be an issue with that?

From the discussion, I think getEnvFromPlatformProxy and have it returns only the the env. This is the original use case and what we usually want - i.e. when we access the cache at build time, we want to use the config that will be used at runtime.

So I think what we want is to pass the getEnvFromPlatformProxy() and getDeployementMapping() (i.e. no change from what we do today).

Inside this function you could add env = {...envVars, ...process.env}; with a comment that process.env has priority for those variables (agreed that process.env should have priority)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, hopefully this is what you had in mind: 945f849

@dario-piotrowicz dario-piotrowicz requested a review from vicb July 28, 2025 10:27
@dario-piotrowicz dario-piotrowicz changed the title fix deploy command not taking into account environment variables fix deploy and upload commands not taking into account environment variables Jul 28, 2025
Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

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

@dario-piotrowicz
Copy link
Contributor Author

Thanks!

Could you please comment the change at https://opennext.js.org/cloudflare/howtos/skew#:~:text=%7D-,Environment%20variables,-The%20following%20environment

How would you like me to update the docs there? I think that they are correct already? would you like me directly specify that environment variables are taken from both the platform proxy and the system env variables?

@vicb
Copy link
Contributor

vicb commented Jul 28, 2025

Thanks!
Could you please comment the change at https://opennext.js.org/cloudflare/howtos/skew#:~:text=%7D-,Environment%20variables,-The%20following%20environment

How would you like me to update the docs there? I think that they are correct already? would you like me directly specify that environment variables are taken from both the platform proxy and the system env variables?

Yes.

The doc says "environment variables", we should clarify what this means wrt "process environment variables" and "wrangler environment variables".

@dario-piotrowicz dario-piotrowicz merged commit be6a492 into main Jul 28, 2025
7 of 8 checks passed
@dario-piotrowicz dario-piotrowicz deleted the dario/801/build-vars-from-process-env branch July 28, 2025 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Environment Variables discrepancy in Skew Protection

2 participants