Skip to content

fix(astro): use prerender loader environment for metadata#16618

Open
bittoby wants to merge 1 commit intowithastro:mainfrom
bittoby:fix/cloudflare-node-prerender-dev-styles
Open

fix(astro): use prerender loader environment for metadata#16618
bittoby wants to merge 1 commit intowithastro:mainfrom
bittoby:fix/cloudflare-node-prerender-dev-styles

Conversation

@bittoby
Copy link
Copy Markdown
Contributor

@bittoby bittoby commented May 6, 2026

Changes

  • Fixes ModuleLoader.getSSREnvironment() to return the ssrEnvironment parameter the loader was constructed with, instead of always looking up viteServer.environments.ssr.
  • With adapters that register a separate prerender Vite dev environment (notably @astrojs/cloudflare with prerenderEnvironment: 'node'), createViteLoader builds a dedicated loader per environment. Every other method already uses the constructor parameter - only getSSREnvironment() ignored it, so the prerender handler's loader returned the unrelated ssr environment and getComponentMetadata() walked the wrong module graph in dev. With Cloudflare's default workerd ssr, that env is also non-runnable, making the as RunnableDevEnvironment cast unsafe.
  • Removes the now-unused ASTRO_VITE_ENVIRONMENT_NAMES import.
  • Changeset: astro: patch (.changeset/fix-module-loader-prerender-env.md).
  • Closes @astrojs/cloudflare: prerenderEnvironment: 'node' swallows scoped <style> blocks into <template> in dev #16436. Follow-up to fix: propagate head metadata across ssr and prerender envs in dev #16292, which fixed the visible style-into-<template> symptom in astro:head-metadata but did not touch this separate correctness bug in the per-environment module loader.

Testing

  • tsc --noEmit on packages/astro passes cleanly.
  • Existing regression test packages/astro/test/head-propagation-prerender-env.test.js still passes (1/1) - confirms no regression on the astro:head-metadata path that fix: propagate head metadata across ssr and prerender envs in dev #16292 fixed.
  • No new test added: loader.getSSREnvironment() has a single caller (getComponentMetadata), and when only the ssr env exists ssrEnvironment === viteServer.environments.ssr, so the new behaviour is a strict superset of the old correct behaviour. Happy to extend the head-propagation-prerender-env fixture with a prerendered route to exercise the prerender handler's loader directly if maintainers prefer.

Docs

No docs change needed - this is a dev-server-internal correctness fix with no user-facing API or behaviour change. Users on adapters with a separate prerender env (e.g. @astrojs/cloudflare with prerenderEnvironment: 'node') will simply see correct head metadata for prerendered routes in dev.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 6, 2026

🦋 Changeset detected

Latest commit: c83c987

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 May 6, 2026
@bittoby
Copy link
Copy Markdown
Contributor Author

bittoby commented May 6, 2026

Hi @ematipico @matthewp, could you please take a look at my small PR? Welcome to any feedback

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 6, 2026

Merging this PR will not alter performance

✅ 18 untouched benchmarks


Comparing bittoby:fix/cloudflare-node-prerender-dev-styles (c83c987) with main (12a03f2)

Open in CodSpeed

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.

@astrojs/cloudflare: prerenderEnvironment: 'node' swallows scoped <style> blocks into <template> in dev

1 participant