From d3694127af3e7e6e063bed53d06d187089116b95 Mon Sep 17 00:00:00 2001 From: Sebastian Herrlinger Date: Thu, 29 Oct 2020 12:15:52 +0100 Subject: [PATCH] avoid creating multiple service worker instances (#266) --- src/components/ServiceWorker.tsx | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/components/ServiceWorker.tsx b/src/components/ServiceWorker.tsx index c6ed9685..578fc04c 100644 --- a/src/components/ServiceWorker.tsx +++ b/src/components/ServiceWorker.tsx @@ -6,14 +6,18 @@ export type ServiceWorkerProps = { prefix?: string; }; +let client; + export const ServiceWorker = ({ swPath = "/sw.js", prefix = "react-plugin-clerk" }: ServiceWorkerProps) => { useEffect(() => { - const client = createClient({ - swPath, - prefix, - }); - client.on("waiting", (updateNow) => updateNow()); - client.registerServiceWorker(); + if (!client) { + client = createClient({ + swPath, + prefix, + }); + client.on("waiting", (updateNow) => updateNow()); + client.registerServiceWorker(); + } }, []); return null; };