Skip to content

Feat(cloudflare): configure prerender environment#15711

Merged
matthewp merged 19 commits intomainfrom
feat/cloudflare-conf-prerender-env
Mar 11, 2026
Merged

Feat(cloudflare): configure prerender environment#15711
matthewp merged 19 commits intomainfrom
feat/cloudflare-conf-prerender-env

Conversation

@OliverSpeir
Copy link
Copy Markdown
Contributor

@OliverSpeir OliverSpeir commented Mar 2, 2026

Changes

Adds a prerenderEnvironment config option to cloudflare adapter

Closes #15684

Adds prerenderEnvironment: 'workerd' | 'node' to the adapter options (defaults to 'workerd'). When set to 'node', we skip the custom workerd-based prerenderer and the @cloudflare/vite-plugin prerender worker config, falling back to Astro's built-in prerender environment instead.

Makes prerenderEnvironment: 'node' work during astro dev by adding a Vite plugin that intercepts requests for prerendered pages and renders them through the prerender environment which is, currently, node instead of workerd.

Testing

adds tests to start up dev server and verify it is using the correct environement

Docs

/cc @withastro/maintainers-docs for feedback!
withastro/docs#13389

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 2, 2026

🦋 Changeset detected

Latest commit: 6ed27df

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

@github-actions github-actions bot added pkg: integration Related to any renderer integration (scope) pkg: astro Related to the core `astro` package (scope) labels Mar 2, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 2, 2026

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing feat/cloudflare-conf-prerender-env (6ed27df) with main (1cdaf9f)

Open in CodSpeed

@ematipico
Copy link
Copy Markdown
Member

@OliverSpeir please restore our PR template and fill it accordingly

Copy link
Copy Markdown
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Blocking because the testing part wasn't filled. Since this is a sensitive change, I want to make sure testing is done properly

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure I understand why we're using this plugin. Can't we use the default Astro behaviour? It seems we're repeating code here, and we risk to diverge even more the dev behaviour from the "default" one

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Might have missed an obvious way to do this, how would you do it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we don't load the CF plugin, Astro automatically uses its own dev server.

// Cloudflare handles its own requests
// TODO: let this handle non-runnable environments that don't intercept requests
if (!isRunnableDevEnvironment(viteServer.environments[ASTRO_VITE_ENVIRONMENT_NAMES.ssr])) {
return;
}

So the question is, what is this plugin actually doing? I read the documentation provided, and it seems it's meant for astro dev, correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah the scenario is we want pre-rendered pages to go through astro default dev server and on demand pages to go through workerd so that it aligns with the new option which uses node for prerendered pages instead of workerd

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

another way would be if the cloudflare vite plugin accepted a shouldHandle function

https://github.com/cloudflare/workers-sdk/blob/596b8a0cb/packages/vite-plugin-cloudflare/src/plugins/dev.ts#L267

could just return next() and then astros default dev server would handle it and we don't need this plugin at all

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We want to forward prerender pages to the astro environment in development, and prerenderer in build.

Yes in dev, in build we are actually just changing the prerenderer by not setting it to use the workerd one

We want to forward ssr pages to the ssr environment in development, and prerenderer in build.

on demand pages stay ssr for both dev and build

Currently all dev server requests go to ssr environment, and the cloudflare vite-plugin implements its own ssr which intercepts all requests indiscriminately via adding a vite middleware - so this PR creates a middleware that runs before the vite-plugin's and checks if the request is for a prerendered route and if it is it sends it to the astro environment which already exists but isnt used for dev server typically (no particular reason to use this environment its just that it already exists and has a runnable dev environment) and if it is not a prerendered route we let it go through to the ssr environment which is workerd

Copy link
Copy Markdown
Contributor Author

@OliverSpeir OliverSpeir Mar 3, 2026

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As a stop gap you could use configResolved and null out the dev plugin for cloudflare. https://github.com/cloudflare/workers-sdk/blob/main/packages/vite-plugin-cloudflare/src/plugins/dev.ts#L61

Pseudo-code:

const cloudflareDev = plugins.find(plugin => plugin.name === 'vite-plugin-cloudflare:dev');

cloudflareDev.configureServer = null

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do agree with James here though, that seems like a fairly clean way to do it: cloudflare/workers-sdk#12747

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I vibe coded James' idea here: 65cfde8

Copy link
Copy Markdown
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Great changeset @OliverSpeir ! 🎉

We'll just need a corresponding docs PR here in the Cloudflare adapter options section for prerenderEnvironment: https://v6.docs.astro.build/en/guides/integrations-guide/cloudflare/#options (You can use the headings for sessionKVBindingsName as a model for the API reference, since this one will need the <Since /> component too.)

Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
@OliverSpeir OliverSpeir force-pushed the feat/cloudflare-conf-prerender-env branch from 2a5b9dd to b328158 Compare March 4, 2026 03:06
@matthewp matthewp added the pr preview Apply this label to a PR to generate a preview release label Mar 10, 2026
@github-actions github-actions bot removed the pr preview Apply this label to a PR to generate a preview release label Mar 10, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 10, 2026

npm i https://pkg.pr.new/astro@15711
npm i https://pkg.pr.new/@astrojs/cloudflare@15711

commit: 76f3a50

@matthewp matthewp added the pr preview Apply this label to a PR to generate a preview release label Mar 10, 2026
@github-actions github-actions bot removed the pr preview Apply this label to a PR to generate a preview release label Mar 10, 2026
Copy link
Copy Markdown
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Unblocking. Let's merge after manual testing :)

@matthewp matthewp added the pr preview Apply this label to a PR to generate a preview release label Mar 11, 2026
@github-actions github-actions bot removed the pr preview Apply this label to a PR to generate a preview release label Mar 11, 2026
@matthewp matthewp added the pr preview Apply this label to a PR to generate a preview release label Mar 11, 2026
@github-actions github-actions bot removed the pr preview Apply this label to a PR to generate a preview release label Mar 11, 2026
@matthewp
Copy link
Copy Markdown
Contributor

pnpm add https://pkg.pr.new/@astrojs/cloudflare@a6cac29

@matthewp
Copy link
Copy Markdown
Contributor

I'm hitting an issue in my testing, looking into it.

@matthewp
Copy link
Copy Markdown
Contributor

Ah, I didn't have the right version of Astro because there isn't a changeset. I'll add one.

@matthewp matthewp added the pr preview Apply this label to a PR to generate a preview release label Mar 11, 2026
@github-actions github-actions bot removed the pr preview Apply this label to a PR to generate a preview release label Mar 11, 2026
@matthewp
Copy link
Copy Markdown
Contributor

pnpm add https://pkg.pr.new/astro@71a927a
pnpm add https://pkg.pr.new/@astrojs/cloudflare@71a927a

@matthewp matthewp added the pr preview Apply this label to a PR to generate a preview release label Mar 11, 2026
@github-actions github-actions bot removed the pr preview Apply this label to a PR to generate a preview release label Mar 11, 2026
@matthewp
Copy link
Copy Markdown
Contributor

npm i https://pkg.pr.new/astro@76f3a50
npm i https://pkg.pr.new/@astrojs/cloudflare@76f3a50

Copy link
Copy Markdown
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Approving the changeset for docs (though a suggestion to consider!)

Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
@matthewp matthewp merged commit b2bd27b into main Mar 11, 2026
25 of 27 checks passed
@matthewp matthewp deleted the feat/cloudflare-conf-prerender-env branch March 11, 2026 16:48
@astrobot-houston astrobot-houston mentioned this pull request Mar 11, 2026
dadezzz pushed a commit to dadezzz/ice-notes that referenced this pull request Mar 15, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [astro](https://astro.build) ([source](https://github.com/withastro/astro/tree/HEAD/packages/astro)) | [`6.0.2` → `6.0.3`](https://renovatebot.com/diffs/npm/astro/6.0.2/6.0.3) | ![age](https://developer.mend.io/api/mc/badges/age/npm/astro/6.0.3?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/astro/6.0.2/6.0.3?slim=true) |

---

### Release Notes

<details>
<summary>withastro/astro (astro)</summary>

### [`v6.0.3`](https://github.com/withastro/astro/blob/HEAD/packages/astro/CHANGELOG.md#603)

[Compare Source](https://github.com/withastro/astro/compare/astro@6.0.2...astro@6.0.3)

##### Patch Changes

- [#&#8203;15711](withastro/astro#15711) [`b2bd27b`](withastro/astro@b2bd27b) Thanks [@&#8203;OliverSpeir](https://github.com/OliverSpeir)! - Improves Astro core's dev environment handling for prerendered routes by ensuring route/CSS updates and prerender middleware behavior work correctly across both SSR and prerender environments.

  This enables integrations that use Astro's prerender dev environment (such as Cloudflare with `prerenderEnvironment: 'node'`) to get consistent route matching and HMR behavior during development.

- [#&#8203;15852](withastro/astro#15852) [`1cdaf9f`](withastro/astro@1cdaf9f) Thanks [@&#8203;ematipico](https://github.com/ematipico)! - Fixes a regression where the the routes emitted by the `astro:build:done` hook didn't have the `distURL` array correctly populated.

- [#&#8203;15765](withastro/astro#15765) [`ca76ff1`](withastro/astro@ca76ff1) Thanks [@&#8203;matthewp](https://github.com/matthewp)! - Hardens server island POST endpoint validation to use own-property checks for improved consistency

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My41OS41IiwidXBkYXRlZEluVmVyIjoiNDMuNTkuNSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: Renovate Bot <renovate@zarantonello.dev>
Co-committed-by: Renovate Bot <renovate@zarantonello.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Astro v6 Cloudflare prerendering environment is too restrictive

4 participants