fix(vite-plugin-cloudflare): add hot channel api for ssrLoadModule compatibility#12738
fix(vite-plugin-cloudflare): add hot channel api for ssrLoadModule compatibility#12738hyf0 wants to merge 1 commit intocloudflare:mainfrom
Conversation
…mpatibility CloudflareDevEnvironment's hot channel was missing the `api` property with `innerEmitter`/`outsideEmitter` that `ssrLoadModule()` requires. When another Vite plugin (e.g. a framework's SSG prerender step) calls `server.ssrLoadModule()` on a dev server where @cloudflare/vite-plugin is active, the SSRCompatModuleRunner's transport crashes: TypeError: Cannot read properties of undefined (reading 'outsideEmitter') The `HotChannel` interface defines `api` as optional, but `createServerModuleRunnerTransport` unconditionally accesses `channel.api.outsideEmitter`. Vite's built-in `createServerHotChannel()` (used by `RunnableDevEnvironment`) includes `api` — this aligns `CloudflareDevEnvironment`'s hot channel to do the same. Related: vitejs/vite#21726, vitejs/vite#21739 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
|
Codeowners approval required for this PR:
Show detailed file reviewers
|
There was a problem hiding this comment.
Pull request overview
This PR fixes a compatibility gap between @cloudflare/vite-plugin’s dev environment and Vite’s server.ssrLoadModule() by aligning CloudflareDevEnvironment’s hot channel with what Vite’s server-side module runner expects.
Changes:
- Add
hot.apito the Cloudflare hot channel (innerEmitter/outsideEmitter) to match Vite’s server hot channel shape. - Emit
outsideEmitter"send"events when sending payloads, and emitinnerEmitter"connection"when the channel starts listening. - Clean up emitter listeners on hot channel
close().
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Closing — adding |
Summary
CloudflareDevEnvironment's hot channel is missing theapiproperty (innerEmitter/outsideEmitter), which causesserver.ssrLoadModule()to crash when called by other Vite plugins on a dev server where@cloudflare/vite-pluginis active:Root cause
ssrLoadModule()creates anSSRCompatModuleRunnerwhose transport accessesenvironment.hot.api.outsideEmitter. Vite's built-increateServerHotChannel()(used byRunnableDevEnvironment) provides thisapiproperty, butCloudflareDevEnvironment'screateHotChannel()does not.The
HotChannelinterface definesapias optional (api?: Api), butcreateServerModuleRunnerTransportunconditionally accesses it.Fix
Add
api: { innerEmitter, outsideEmitter }tocreateHotChannel(), matching whatcreateServerHotChannel()provides. This makesCloudflareDevEnvironmentcompatible withssrLoadModule()without changing its existing WebSocket-based module execution.Use case
Framework plugins that perform SSG/prerendering call
server.ssrLoadModule()during build to execute exports from page files (e.g. collecting prerender paths for static site generation). When@cloudflare/vite-pluginis present in the user's config, this crashes because the SSR environment is aCloudflareDevEnvironmentwith an incomplete hot channel.Related
ssrLoadModulewith non-runnable ssr env vitejs/vite#21739 — Vite PR to improve the error message (confirmsssrLoadModulerequires a runnable environment, but the hot channelapigap is on the plugin side)