From 141a85b496bc6ca5f5d278dd3314fd9ab411f13a Mon Sep 17 00:00:00 2001 From: Cody Olsen Date: Fri, 13 Dec 2024 20:09:44 +0100 Subject: [PATCH] fix: improve StudioTelemetryProvider perf --- .../core/studio/StudioTelemetryProvider.tsx | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/packages/sanity/src/core/studio/StudioTelemetryProvider.tsx b/packages/sanity/src/core/studio/StudioTelemetryProvider.tsx index 234316a6a2a..a8e49b33804 100644 --- a/packages/sanity/src/core/studio/StudioTelemetryProvider.tsx +++ b/packages/sanity/src/core/studio/StudioTelemetryProvider.tsx @@ -56,35 +56,35 @@ export const debugLoggingStore: CreateBatchedStoreOptions = { export function StudioTelemetryProvider(props: {children: ReactNode; config: Config}) { const client = useClient({apiVersion: 'v2023-12-18'}) - const projectId = client.config().projectId - - const storeOptions = useMemo((): CreateBatchedStoreOptions => { - if (DEBUG_TELEMETRY) { - return debugLoggingStore - } - return { - // submit any pending events every ms - flushInterval: 30000, - - // implements user consent resolving - resolveConsent: () => - client.request({uri: '/intake/telemetry-status', tag: 'telemetry-consent.studio'}), - - // implements sending events to backend - sendEvents: (batch) => - client.request({ - uri: '/intake/batch', - method: 'POST', - json: true, - body: {projectId, batch}, - }), - // opts into a different strategy for sending events when the browser close, reload or navigate away from the current page - sendBeacon: (batch) => - navigator.sendBeacon(client.getUrl('/intake/batch'), JSON.stringify({projectId, batch})), - } - }, [client, projectId]) - - const store = useMemo(() => createBatchedStore(sessionId, storeOptions), [storeOptions]) + const store = useMemo(() => { + const projectId = client.config().projectId + const storeOptions: CreateBatchedStoreOptions = DEBUG_TELEMETRY + ? debugLoggingStore + : { + // submit any pending events every ms + flushInterval: 30000, + + // implements user consent resolving + resolveConsent: () => + client.request({uri: '/intake/telemetry-status', tag: 'telemetry-consent.studio'}), + + // implements sending events to backend + sendEvents: (batch) => + client.request({ + uri: '/intake/batch', + method: 'POST', + json: true, + body: {projectId, batch}, + }), + // opts into a different strategy for sending events when the browser close, reload or navigate away from the current page + sendBeacon: (batch) => + navigator.sendBeacon( + client.getUrl('/intake/batch'), + JSON.stringify({projectId, batch}), + ), + } + return createBatchedStore(sessionId, storeOptions) + }, [client]) useEffect(() => { const workspaces = arrify(props.config)