Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/yjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,21 @@ export function getOrCreateYDoc(roomId: string, token: string) {
console.log(`[Yjs] Room ${roomId} mapped. Synced:`, isSynced as boolean);
});
provider.on('status', (event: unknown) => {
console.log(`[Yjs] Room ${roomId} status:`, (event as { status: 'connected' | 'disconnected' | 'connecting' }).status);
const status = (event as {
status: 'connected' | 'disconnected' | 'connecting'
}).status;

console.log(`[Yjs] Room ${roomId} status:`, status);

// Attempt recovery after reconnect
if (status === "connected") {
console.log("[Yjs] Reconnected successfully");

// Force preview refresh after reconnect
window.dispatchEvent(
Comment thread
YASHcode-IIITV marked this conversation as resolved.
new CustomEvent("yjs-reconnected")
Comment thread
YASHcode-IIITV marked this conversation as resolved.
);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
});

return { doc, provider };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Input } from "@/components/ui/input";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
import { Label } from "@/components/ui/label";
import type { TemplateCategory } from "@/lib/templates/types";
import { getTemplateSummaries } from "@/lib/templates/actions";

import type { TemplateKey } from "@/lib/template";
Comment thread
YASHcode-IIITV marked this conversation as resolved.
import {
ChevronRight,
Expand Down
24 changes: 23 additions & 1 deletion modules/webcontainers/components/webcontainer-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ const WebContainerPreview = ({
};

/** Register a server-ready listener, unsubscribing any prior one to prevent accumulation. */
const bindServerReady = (inst: WebContainer, handler: (port: number, url: string) => void) => {
const bindServerReady = (
inst: WebContainer,
handler: (port: number, url: string) => void,
) => {
serverReadyCleanupRef.current?.();
serverReadyCleanupRef.current = inst.on("server-ready", handler);
};
Expand Down Expand Up @@ -370,12 +373,18 @@ const WebContainerPreview = ({
writeTerminal(
`🌐 Server ready at ${url} (port ${port})\r\n`,
);
console.log("SERVER READY EVENT", port, url);

const isCommonFrontendPort = [
3000, 5173, 8080, 4200, 8000,
].includes(port);
Comment thread
YASHcode-IIITV marked this conversation as resolved.
setPreviewUrl((prevUrl) => {
if (prevUrl !== url) {
setRefreshKey((k) => k + 1);
}

if (prevUrl && !isCommonFrontendPort) return prevUrl;

return url;
});

Expand Down Expand Up @@ -776,6 +785,19 @@ const WebContainerPreview = ({
};
}, []);

useEffect(() => {
const handler = () => {
console.log("FORCING PREVIEW REFRESH AFTER YJS CONNECT");

setRefreshKey((k) => k + 1);
};

window.addEventListener("yjs-reconnected", handler);

return () => {
Comment thread
YASHcode-IIITV marked this conversation as resolved.
window.removeEventListener("yjs-reconnected", handler);
};
}, []);


if (error || setupError) {
Expand Down
Loading