Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: Reduce notification polling while hidden (#5764)

Oleg Isonen2551dcf93481
diff --git a/apps/builder/app/shared/notifications/subscription.tsx b/apps/builder/app/shared/notifications/subscription.tsx
index 3cc70e729c..805e135538 100644
--- a/apps/builder/app/shared/notifications/subscription.tsx
+++ b/apps/builder/app/shared/notifications/subscription.tsx
@@ -53,7 +53,6 @@ export const startSubscription = () => {
nativeClient.polly.poll.query({
topics,
}) as Promise<SubscriptionResponse>,
- pauseOnHidden: false,
onError: (error) => {
console.warn("[subscription] poll failed", error);
},
diff --git a/apps/builder/app/shared/polly/polling-client.test.ts b/apps/builder/app/shared/polly/polling-client.test.ts
index c5eb50e36c..dcbfac907c 100644
--- a/apps/builder/app/shared/polly/polling-client.test.ts
+++ b/apps/builder/app/shared/polly/polling-client.test.ts
@@ -116,7 +116,7 @@ describe("createPollingClient", () => {
client.stop();
});
- test("uses default 30s interval when not specified", async () => {
+ test("uses default 60s interval when not specified", async () => {
const fetcher = vi.fn(async () => "tick");
const client = createPollingClient({ fetcher, onData: vi.fn() });
@@ -124,7 +124,7 @@ describe("createPollingClient", () => {
await flush();
expect(fetcher).toHaveBeenCalledTimes(1);
- await vi.advanceTimersByTimeAsync(29_000);
+ await vi.advanceTimersByTimeAsync(59_000);
expect(fetcher).toHaveBeenCalledTimes(1);
await vi.advanceTimersByTimeAsync(1_000);
@@ -577,6 +577,38 @@ describe("createPollingClient", () => {
});
});
+ test("does not fetch on start while tab is hidden", async () => {
+ Object.defineProperty(document, "visibilityState", {
+ value: "hidden",
+ writable: true,
+ configurable: true,
+ });
+
+ const fetcher = vi.fn(async () => "data");
+ const client = createPollingClient({
+ fetcher,
+ onData: vi.fn(),
+ interval: 1000,
+ });
+
+ client.start();
+ await flush();
+
+ expect(fetcher).not.toHaveBeenCalled();
+
+ Object.defineProperty(document, "visibilityState", {
+ value: "visible",
+ writable: true,
+ configurable: true,
+ });
+ document.dispatchEvent(new Event("visibilitychange"));
+ await flush();
+
+ expect(fetcher).toHaveBeenCalledOnce();
+
+ client.stop();
+ });
+
test("resumes and fetches immediately when tab becomes visible", async () => {
const fetcher = vi.fn(async () => "data");
const client = createPollingClient({
diff --git a/apps/builder/app/shared/polly/polling-client.ts b/apps/builder/app/shared/polly/polling-client.ts
index 053d98645d..ae02fb0d98 100644
--- a/apps/builder/app/shared/polly/polling-client.ts
+++ b/apps/builder/app/shared/polly/polling-client.ts
@@ -4,7 +4,7 @@
* Repeatedly calls an async `fetcher` at a configurable interval and
* delivers results via an `onData` callback. Handles:
*
- * - Configurable interval (default 30 s)
+ * - Configurable interval (default 60 s)
* - Incremental backoff with decorrelated jitter on errors
* - Page Visibility API — pauses when the tab is hidden, resumes
* with an immediate fetch when visible
@@ -23,7 +23,7 @@ export type PollingClientOptions<T> = {
/**
* Polling interval in milliseconds (used while healthy).
- * @default 30_000
+ * @default 60_000
*/
interval?: number;
@@ -85,7 +85,7 @@ export type PollingClient = {
isActive: () => boolean;
};
-const DEFAULT_INTERVAL = 30_000;
+const DEFAULT_INTERVAL = 60_000;
const hasDocument = () => typeof document !== "undefined";
@@ -190,10 +190,13 @@ export const createPollingClient = <T>(
return;
}
active = true;
- poll();
if (pauseOnHidden && hasDocument()) {
document.addEventListener("visibilitychange", handleVisibilityChange);
+ if (document.visibilityState === "hidden") {
+ return;
+ }
}
+ poll();
};
const stop = () => {
diff --git a/apps/builder/app/shared/polly/polling-manager.ts b/apps/builder/app/shared/polly/polling-manager.ts
index ee7429ba93..44000e30c7 100644
--- a/apps/builder/app/shared/polly/polling-manager.ts
+++ b/apps/builder/app/shared/polly/polling-manager.ts
@@ -35,7 +35,7 @@ export type PollingManagerOptions = {
fetcher: (topics: TopicName[]) => Promise<SubscriptionResponse>;
/**
* Polling interval in milliseconds.
- * @default 30_000
+ * @default 60_000
*/
interval?: number;
/**