/
githubmirror
/
material-ui
Обзор
Документация
Войти
/
githubmirror
/
material-ui
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
packages-internal/core-docs/src/DocsApp/serviceWorker.ts
67 строк
2 KB
Jan Potoms
[core-docs] Tighten service worker host check (#48581)
23 июл 2026, 13:52
Не верифицирован
23 июл 2026, 13:52
2aed272
Код
Авторство
О чём код?
let reloadInterval: ReturnType<typeof setInterval>; // Avoid infinite loop when "Update on reload" is set in the Chrome sw dev tools. function lazyReload() { clearInterval(reloadInterval); reloadInterval = setInterval(() => { if (document.hasFocus()) { window.location.reload(); } }, 100); } // Inspired by // https://developers.google.com/web/tools/workbox/guides/advanced-recipes#offer_a_page_reload_for_users function forcePageReload(registration: ServiceWorkerRegistration) { // console.log('already controlled?', Boolean(navigator.serviceWorker.controller)); if (!navigator.serviceWorker.controller) { // The window client isn't currently controlled so it's a new service // worker that will activate immediately. return; } // console.log('registration waiting?', Boolean(registration.waiting)); if (registration.waiting) { // SW is waiting to activate. Can occur if multiple clients open and // one of the clients is refreshed. registration.waiting.postMessage('skipWaiting'); return; } function listenInstalledStateChange() { registration.installing!.addEventListener('statechange', (event) => { // console.log('statechange', event.target.state); if ((event.target as ServiceWorker).state === 'installed' && registration.waiting) { // A new service worker is available, inform the user registration.waiting.postMessage('skipWaiting'); } else if ((event.target as ServiceWorker).state === 'activated') { // Force the control of the page by the activated service worker. lazyReload(); } }); } if (registration.installing) { listenInstalledStateChange(); return; } // We are currently controlled so a new SW may be found... // Add a listener in case a new SW is found, registration.addEventListener('updatefound', listenInstalledStateChange); } export async function registerServiceWorker(swPath: string): Promise<void> { const { host } = window.location; if ( 'serviceWorker' in navigator && process.env.NODE_ENV === 'production' && (host === 'mui.com' || host.endsWith('.mui.com')) ) { // register() automatically attempts to refresh the sw.js. const registration = await navigator.serviceWorker.register(swPath); // Force the page reload for users. forcePageReload(registration); } }