Skip to content

Conversation

@petebacondarwin
Copy link
Contributor

@petebacondarwin petebacondarwin commented Feb 7, 2026

Fixes #1440.

This PR adds Pages-specific environment variables to wrangler pages dev for improved dev/prod parity. These variables are normally injected by Cloudflare Pages CI during builds/deployments.

Environment Variables Added

Variable Value
CF_PAGES "1" - Indicates running in Pages environment
CF_PAGES_BRANCH Current git branch or "local" if not in a git repo
CF_PAGES_COMMIT_SHA Current git commit SHA or placeholder if not in a git repo
CF_PAGES_URL Simulated commit preview URL (e.g., https://<sha>.<project-name>.pages.dev)

Project Name Resolution

The project name used in CF_PAGES_URL is determined using the same priority as wrangler pages deploy:

  1. name field from wrangler.toml
  2. project_name from config cache (from previous deployments)
  3. Current directory name (fallback)

Benefits

  • Enables frameworks like SvelteKit to auto-detect the Pages environment during local development (via adapter-auto)
  • Improves dev/prod parity for Pages projects by using the correct URL format
  • Variables can be overridden by user-defined vars in wrangler.toml, .dev.vars, or via CLI flags

Implementation

The CF_PAGES variables are passed as defaultVars which get merged before config vars. This ensures users can override any of these values in their configuration if needed.


A picture of a cute animal (not mandatory, but encouraged)

🦊

@changeset-bot
Copy link

changeset-bot bot commented Feb 7, 2026

🦋 Changeset detected

Latest commit: e8e6928

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

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

@claude
Copy link
Contributor

claude bot commented Feb 7, 2026

Claude finished @petebacondarwin's task —— View job


Changeset Review

All changesets look good

The changeset .changeset/cf-pages-env-vars.md follows all guidelines:

  1. Version Type: minor is correct - this adds new functionality (environment variables) to wrangler pages dev
  2. Changelog Quality: Clear, descriptive explanation with bullet points detailing each variable and its behavior. Includes information about override behavior.
  3. Markdown Headers: No h1/h2/h3 headers used
  4. Analytics: Not applicable - no analytics changes
  5. Dependabot: Not applicable - this is not a dependency update
  6. Experimental Features: Not applicable - this adds standard environment variables to an existing stable feature

The changeset provides excellent detail about what variables are added, their default values, and how they can be overridden. The description is clear and will be helpful in the changelog.


Cuteness Report: This is Claude's Spinner. They're a tiny orange dot doing their very best to rotate. Barely visible but full of determination. Would be 14/10 if they were an actual animal, but alas, just a loading indicator. Still deserves recognition for their hard work.


@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 7, 2026

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@12473

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@12473

miniflare

npm i https://pkg.pr.new/miniflare@12473

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@12473

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@12473

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@12473

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@12473

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@12473

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@12473

wrangler

npm i https://pkg.pr.new/wrangler@12473

commit: e8e6928

@petebacondarwin petebacondarwin marked this pull request as ready for review February 7, 2026 14:42
@petebacondarwin petebacondarwin requested review from a team as code owners February 7, 2026 14:42
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 5 additional findings.

Open in Devin Review

@petebacondarwin petebacondarwin force-pushed the fix/1440-env-var-mocking-pages branch from e139f06 to b77b98f Compare February 8, 2026 14:10
* Default vars that can be overridden by config vars.
* Useful for injecting environment-specific defaults like CF_PAGES variables.
*/
defaultVars?: Record<string, string>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry quick review and I don't have time to dig too much into the details right now - also this is probably something to add the PR desc.

Why would StartDevWorkerInput received defaults?
Shoudn't the values be resolved by the caller instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Really good question. The problem is that if we merged them at the caller (say into the vars) they would then take priority over the other possible sources such as the config file and the .env file. These values are fallbacks for when they are not specified in any other case.

@petebacondarwin petebacondarwin force-pushed the fix/1440-env-var-mocking-pages branch from b77b98f to b3a3849 Compare February 9, 2026 08:52
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 1 new potential issue.

View 7 additional findings in Devin Review.

Open in Devin Review

@petebacondarwin petebacondarwin force-pushed the fix/1440-env-var-mocking-pages branch from 4d46c12 to 8b40db4 Compare February 9, 2026 12:13
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 1 new potential issue.

View 8 additional findings in Devin Review.

Open in Devin Review

@petebacondarwin petebacondarwin force-pushed the fix/1440-env-var-mocking-pages branch 2 times, most recently from 47cc491 to 8f5b47d Compare February 9, 2026 17:21
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 1 new potential issue.

View 7 additional findings in Devin Review.

Open in Devin Review

@petebacondarwin petebacondarwin force-pushed the fix/1440-env-var-mocking-pages branch from 73a8278 to fad1071 Compare February 10, 2026 15:44
@petebacondarwin petebacondarwin requested a review from vicb February 10, 2026 15:57
@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Feb 10, 2026
@petebacondarwin petebacondarwin force-pushed the fix/1440-env-var-mocking-pages branch from d479f67 to 0030379 Compare February 10, 2026 18:47
Injects Pages-specific environment variables (CF_PAGES, CF_PAGES_BRANCH,
CF_PAGES_COMMIT_SHA, CF_PAGES_URL) during local development with
wrangler pages dev.

This enables frameworks like SvelteKit to auto-detect the Pages environment
during local development, improving dev/prod parity.

The variables can be overridden by user-defined vars in wrangler.toml,
.dev.vars, or via CLI flags.

Fixes #1440
Update maskVars to accept defaultVars parameter so that CF_PAGES
environment variables are not incorrectly masked as '(hidden)'.
These are non-secret defaults that should be visible to users.
CF_PAGES_URL should be the deployment URL (e.g., https://<sha>.<project>.pages.dev),
not the local dev server URL.

- Update getPagesEnvironmentVariables to generate commit preview URL format
- Determine project name from config.name, configCache, or directory name
- Update changeset and e2e test expectations
@petebacondarwin petebacondarwin force-pushed the fix/1440-env-var-mocking-pages branch from 0030379 to e8e6928 Compare February 11, 2026 09:17
@petebacondarwin petebacondarwin merged commit b900c5a into main Feb 11, 2026
35 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Feb 11, 2026
@petebacondarwin petebacondarwin deleted the fix/1440-env-var-mocking-pages branch February 11, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

🚀 Feature Request: Env var mocking for Pages

3 participants