fix: debounce synced event spam + fix rApp iframe 404s on subdomains

Synced event was firing 100+ times during initial Automerge sync negotiation.
Debounce with 300ms timer so it fires once after the burst settles.

folk-rapp iframes built URLs as /{space}/{moduleId} which on subdomain
URLs (jeff.rspace.online) got double-prefixed to /{space}/{space}/{moduleId}
causing 404s. Now detects subdomains and uses /{moduleId} directly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jeff Emmett 2026-03-03 15:10:51 -08:00
parent 15be495e91
commit 92f75141eb
2 changed files with 15 additions and 2 deletions

View File

@ -128,6 +128,7 @@ export class CommunitySync extends EventTarget {
#reconnectDelay = 1000;
#offlineStore: OfflineStore | null = null;
#saveDebounceTimer: ReturnType<typeof setTimeout> | null = null;
#syncedDebounceTimer: ReturnType<typeof setTimeout> | null = null;
#wsUrl: string | null = null;
constructor(communitySlug: string, offlineStore?: OfflineStore) {
@ -757,7 +758,14 @@ export class CommunitySync extends EventTarget {
}
}
this.dispatchEvent(new CustomEvent("synced", { detail: { shapes } }));
// Debounce the synced event — during initial sync negotiation, #applyDocToDOM()
// is called for every Automerge sync message (100+ round-trips). Debounce to
// fire once after the burst settles.
if (this.#syncedDebounceTimer) clearTimeout(this.#syncedDebounceTimer);
this.#syncedDebounceTimer = setTimeout(() => {
this.#syncedDebounceTimer = null;
this.dispatchEvent(new CustomEvent("synced", { detail: { shapes } }));
}, 300);
}
/**

View File

@ -543,7 +543,12 @@ export class FolkRApp extends FolkShape {
if (pathParts.length >= 1) this.#spaceSlug = pathParts[0];
}
const space = this.#spaceSlug || "demo";
const iframeUrl = `/${space}/${this.#moduleId}`;
// On subdomain URLs (jeff.rspace.online), the server prepends /{space}/ automatically,
// so the iframe should load /{moduleId} directly. Using /{space}/{moduleId} would
// double-prefix to /{space}/{space}/{moduleId} → 404.
const hostname = window.location.hostname;
const onSubdomain = hostname.split(".").length >= 3 && hostname.startsWith(space + ".");
const iframeUrl = onSubdomain ? `/${this.#moduleId}` : `/${space}/${this.#moduleId}`;
const iframe = document.createElement("iframe");
iframe.className = "rapp-iframe";