Skip to content

Support Cloudflare Vite plugin v2 - #18119

Open
jamesopstad wants to merge 11 commits into
withastro:nextfrom
jamesopstad:support-cloudflare-vite-plugin-v2
Open

jamesopstad wants to merge 11 commits into
withastro:nextfrom
jamesopstad:support-cloudflare-vite-plugin-v2

Conversation

@jamesopstad

@jamesopstad jamesopstad commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Depends on #18112.

Until that PR lands, the focused diff is:
jamesopstad/astro@resolved-vite-output-dirs...support-cloudflare-vite-plugin-v2

Changes

Updates the adapter to use v2 of @cloudflare/vite-plugin and support deployment with cf.

Projects should replace Wrangler configuration with cloudflare.config.ts, importing configuration utilities from cf/config. cf should also be used in place of Wrangler for deployment.

The adapter’s configPath option has been removed. Configuration is always loaded from a cloudflare.config.ts file in the project root.

wrangler is no longer a peer dependency of @astrojs/cloudflare.

Testing

Tests updated as appropriate.

Docs

This will need a migration guide in the docs.

@changeset-bot

changeset-bot Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e8b4c3f

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

This PR includes changesets to release 9 packages
Name Type
astro Minor
@astrojs/cloudflare Major
@astrojs/markdoc Patch
@astrojs/mdx Patch
@astrojs/netlify Patch
@astrojs/node Patch
@astrojs/svelte Patch
@astrojs/vercel Patch
@astrojs/vue 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

@jamesopstad jamesopstad changed the title Support cloudflare vite plugin v2 Support Cloudflare Vite plugin v2 Sep 24, 2026
@github-actions github-actions Bot added the semver: minor Change triggers a `minor` release label Sep 24, 2026

@github-actions github-actions Bot left a comment

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.

This PR is blocked because it contains a minor changeset. A reviewer will merge this at the next release if approved.

@github-actions github-actions Bot added pkg: integration Related to any renderer integration (scope) pkg: astro Related to the core `astro` package (scope) docs pr labels Sep 24, 2026
@jamesopstad
jamesopstad force-pushed the support-cloudflare-vite-plugin-v2 branch from 3f1e57c to c6357ef Compare September 24, 2026 13:33
@jamesopstad
jamesopstad marked this pull request as ready for review September 24, 2026 13:47
@ematipico ematipico added the pr: astro-review Triggers a bot to do an automated review label Sep 24, 2026
@astro-factory astro-factory Bot removed the pr: astro-review Triggers a bot to do an automated review label Sep 24, 2026
'astro': patch
---

Defers Vite manifest cleanup until all `buildApp` hooks have completed, allowing platform plugins to consume the manifests.

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.

Unless this change fixes some user-facing problem, we don't need it. If so, it should be reworded. Here's a small guide https://contribute.docs.astro.build/docs-for-code-changes/changesets/#patch-updates

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.

Comment on lines +1 to +5
---
'astro': minor
---

Astro now treats Vite's resolved `outDir` values as the source of truth for build output. Generated assets, build hook `dir` values, and SSR manifest paths use the resolved client, server, and prerender directories. Custom prerenderer factories also receive these directories through a new context argument.

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 would rewrite this changeset from the user point of view.

Comment on lines +28 to +32
const flags = compatibilityFlags ?? [];
if (flags.some((flag) => ALS_CAPABLE_FLAGS.includes(flag))) {
return flags;
}
return [...flags, 'nodejs_als'];

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.

Suggested change
const flags = compatibilityFlags ?? [];
if (flags.some((flag) => ALS_CAPABLE_FLAGS.includes(flag))) {
return flags;
}
return [...flags, 'nodejs_als'];
if (!compatibilityFlags) {
return ['nodejs_als']
}
if (compatibilityFlags.some((flag) => ALS_CAPABLE_FLAGS.includes(flag))) {
return compatibilityFlags;
}
return compatibilityFlags.push('nodejs_als')

Since in the if we return the original array, there's no need for the spread

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.

This code hasn't changed. It's only in the diff because the filename has changed.

Comment on lines -494 to -500
if (cloudflareOptions.configPath) {
addWatchFile(new URL(cloudflareOptions.configPath, config.root));
}

addWatchFile(new URL('./wrangler.toml', config.root));
addWatchFile(new URL('./wrangler.json', config.root));
addWatchFile(new URL('./wrangler.jsonc', config.root));

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.

Don't we need to watch the new configuration file?

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.

Hmm, that depends on how Astro restarts interact with Vite restarts. We restart the Vite dev server when the config file or any of its dependencies change. Watching it here would be tricky because you don't know its dependencies.

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 would check to make sure restarting the dev server in Astro works that same as in vite dev, it might not.

@jamesopstad jamesopstad Sep 24, 2026 •

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.

I got an agent to investigate this. Here is its reply:

The Cloudflare plugin already watches cloudflare.config.ts and its dependencies, so watching only the top-level file here would be incomplete and would cause both Astro and Cloudflare to initiate restarts. However, Matthew’s concern uncovered an Astro lifecycle issue: Vite replaces its watcher during a native restart, and Astro’s externally registered restart/content listeners are not reattached after a plugin-initiated restart. We should fix that listener lifecycle and add an astro dev regression test rather than restore addWatchFile() here.

// Assign the Wrangler config's effective env (`vars` merged with
// `.dev.vars`/`.env` overrides) to process.env so astro:env can find
// these variables at build time.
loadWranglerEnv(config.root, cloudflareOptions.configPath, logger);

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.

This isn't needed anymore?

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.

"peerDependencies": {
"astro": "^7.2.0",
"wrangler": "^4.125.0"
"astro": "^7.4.0"

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.

Was this change intentional? We usually don't bump it unless we need a specific API from astro

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.

This PR relies on #18112, which makes changes to Astro core, so I assumed that would require the bump?

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.

Oh I see. Makes sense then. So I suggest #18112 to main

}) => {
const wranglerConfigPath = resolvePath(fileURLToPath(root), '.wrangler/deploy/config.json');
if (!existsSync(wranglerConfigPath)) {
const buildOutputConfigPath = new URL('./.cloudflare/output/v0/config.json', root);

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.

Is this v0 something that could change? and if so, is it under semver?

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.

It will change, but only in the beta period. We'll try to keep changes as non-breaking as possible. It will change to v1 after the beta.


export function pluginPrerender(_opts: StaticBuildOptions, internals: BuildInternals): VitePlugin {
export function pluginPrerender(
_opts: StaticBuildOptionsInput,

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.

Remove it if it's not needed anymore

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.

Sure. I presumed it must be there for a reason.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs pr pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope) semver: minor Change triggers a `minor` release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants