Skip to content

Instantly share code, notes, and snippets.

View sampolahtinen's full-sized avatar

Sampo Lahtinen sampolahtinen

  • Berlin, Germany
View GitHub Profile
@sampolahtinen
sampolahtinen / useServiceWorkerUpdateChecker.tsx
Last active January 28, 2022 09:45
React hook to initiate a service worker update checker
import { useEffect, useRef } from 'react';
const SERVICE_WORKER_UPDATE_CHECK_INTERVAL = 5 * 60 * 1000; // every 5 minutes
/**
* A hook to enable a periodic update checker for new service workers.
*
* For example, when new build is deployed, a new service worker is generated.
* However, when user navigates to the web page old Service Worker is still in control,
* which will return an old build from its cache.