Skip to content

Conversation

rmcsharry
Copy link

@rmcsharry rmcsharry commented Sep 20, 2025

What is this PR?

This PR fixes the below bug (tested on my own fork as previously our worker would sometimes crash, now they don't)

Bug

The BroadcastChannel API is used directly in both previews.ts and webcontainer.preview.$id.tsx.

This will cause a ReferenceError in Cloudflare Workers, since they do not support BroadcastChannel.

Fix Desc

To fix this, ensure that BroadcastChannel is only used in the browser.

This can be done by checking if typeof BroadcastChannel !== "undefined" before creating or using it, and by guarding any code that uses it so it does not run in the server/worker environment.

Changes

Broadcast Guard

app/lib/stores/previews.ts:23-96 Only create broadcast/storage channels when BroadcastChannel exists, falling back to safer no-ops and logging when the API is missing so worker/server contexts stop throwing.

app/lib/stores/previews.ts:158-248 Switched every broadcast path to optional posting so preview refresh/state sync quietly skip when channels are unavailable.

app/routes/webcontainer.preview.$id.tsx:48-82 Wrapped the client-side channel setup in a runtime availability check so the Remix loader can execute in Cloudflare Workers without hitting the constructor.

@Stijnus Stijnus self-requested a review September 24, 2025 14:48
@Stijnus Stijnus self-assigned this Sep 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants