fix: for more stable broadcast channels on CF workers (#2007)

This commit is contained in:
Richard McSharry | Code Monkey
2025-10-23 17:02:19 +02:00
committed by GitHub
parent 983b3025a5
commit 5f925566c4
2 changed files with 67 additions and 35 deletions

View File

@@ -46,17 +46,22 @@ export default function WebContainerPreview() {
}, [previewId, previewUrl]);
useEffect(() => {
// Initialize broadcast channel
broadcastChannelRef.current = new BroadcastChannel(PREVIEW_CHANNEL);
const supportsBroadcastChannel = typeof window !== 'undefined' && typeof window.BroadcastChannel === 'function';
// Listen for preview updates
broadcastChannelRef.current.onmessage = (event) => {
if (event.data.previewId === previewId) {
if (event.data.type === 'refresh-preview' || event.data.type === 'file-change') {
handleRefresh();
if (supportsBroadcastChannel) {
broadcastChannelRef.current = new window.BroadcastChannel(PREVIEW_CHANNEL);
// Listen for preview updates
broadcastChannelRef.current.onmessage = (event) => {
if (event.data.previewId === previewId) {
if (event.data.type === 'refresh-preview' || event.data.type === 'file-change') {
handleRefresh();
}
}
}
};
};
} else {
broadcastChannelRef.current = undefined;
}
// Construct the WebContainer preview URL
const url = `https://${previewId}.local-credentialless.webcontainer-api.io`;