Skip to content

fix(cloudflare): ensure HMR works when prerenderEnvironment is set to 'node'#16162

Merged
matthewp merged 5 commits intowithastro:mainfrom
rururux:cf-node-prerender-hmr
Apr 1, 2026
Merged

fix(cloudflare): ensure HMR works when prerenderEnvironment is set to 'node'#16162
matthewp merged 5 commits intowithastro:mainfrom
rururux:cf-node-prerender-hmr

Conversation

@rururux
Copy link
Copy Markdown
Contributor

@rururux rururux commented Mar 31, 2026

fixes: #16141

Changes

When using @astrojs/cloudflare with the prerenderEnvironment option set to 'node', HMR would fail to trigger, requiring a manual browser reload.

This occurred because the plugin added by the createNodePrerenderPlugin function utilizes devPrerenderMiddlewareSymbol, which causes the execution environment for each file to be identified as prerender.
However, Astro's core HMR plugin was designed to only execute HMR when the environment is specifically named ssr.

hotUpdate: {
order: 'post',
handler({ modules, server, timestamp }) {
if (this.environment.name !== ASTRO_VITE_ENVIRONMENT_NAMES.ssr) return;

To resolve this, I have updated the logic to use isAstroServerEnvironment, ensuring that the HMR plugin functions correctly in both ssr and prerender environments.

Testing

As an e2e test, I added a test to verify that the page updates correctly after a file is modified.

Docs

N/A, bug fix

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 31, 2026

🦋 Changeset detected

Latest commit: 8442198

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 the pkg: astro Related to the core `astro` package (scope) label Mar 31, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 31, 2026

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing rururux:cf-node-prerender-hmr (8442198) with main (4eec0f1)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (1d1448c) during the generation of this report, so 4eec0f1 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Copy link
Copy Markdown
Contributor

@matthewp matthewp left a comment

Choose a reason for hiding this comment

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

I think the E2E failures are real now. Need to figure out what about this change causes them to fail.

@rururux
Copy link
Copy Markdown
Contributor Author

rururux commented Apr 1, 2026

I wonder if that's the case, it feels more like I've just had remarkably bad luck.
For example, PR #16165 only touches language-tools, yet it's failing on the exact same tests as this PR.

[Chrome Stable] › e2e/namespaced-component.test.js:19:2 › Hydrating namespaced components › Preact Component
https://github.com/withastro/astro/actions/runs/23810683277/job/69398822390

@matthewp
Copy link
Copy Markdown
Contributor

matthewp commented Apr 1, 2026

It might not be this branch, #16180 fixes another flaky test. Should try again after that's merged.

Copy link
Copy Markdown
Contributor

@matthewp matthewp left a comment

Choose a reason for hiding this comment

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

looks good

@matthewp matthewp merged commit a0a49e9 into withastro:main Apr 1, 2026
27 checks passed
@rururux
Copy link
Copy Markdown
Contributor Author

rururux commented Apr 1, 2026

Finally! Thank you! 😆

@astrobot-houston astrobot-houston mentioned this pull request Apr 1, 2026
@rururux rururux deleted the cf-node-prerender-hmr branch April 1, 2026 15:11
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)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HMR not working when Cloudflare adapter set to prerenderEnvironment: 'node'

2 participants