|
320 | 320 | <script> |
321 | 321 | (() => { |
322 | 322 | const isIOS = window.__WEBGAL_DEVICE_INFO__?.isIOS ?? false; |
323 | | - if ('serviceWorker' in navigator && !isIOS) { |
324 | | - navigator.serviceWorker |
325 | | - .register('./webgal-serviceworker.js') |
326 | | - .then((reg) => { |
327 | | - console.log('Registration succeeded. Scope is ' + reg.scope); |
328 | | - }) |
329 | | - .catch((error) => { |
330 | | - console.log('Registration failed with ' + error); |
331 | | - }); |
| 323 | + const localHostnames = ['localhost', '127.0.0.1', '::1']; |
| 324 | + const isLocalPreview = localHostnames.includes(window.location.hostname) || window.location.protocol === 'file:'; |
| 325 | + const isElectron = Boolean(window.electronFuncs) || /Electron/i.test(navigator.userAgent || ''); |
| 326 | + const shouldBypassServiceWorker = isIOS || isLocalPreview || isElectron; |
| 327 | + const webgalServiceWorkerUrl = new URL('./webgal-serviceworker.js', window.location.href).href; |
| 328 | + const bypassReloadKey = 'webgal-sw-bypass-reloaded'; |
| 329 | + const isWebGALRegistration = (registration) => { |
| 330 | + const workers = [registration.active, registration.installing, registration.waiting]; |
| 331 | + return workers.some((worker) => worker?.scriptURL === webgalServiceWorkerUrl); |
| 332 | + }; |
| 333 | + const markBypassReload = () => { |
| 334 | + try { |
| 335 | + if (sessionStorage.getItem(bypassReloadKey) === 'true') { |
| 336 | + return false; |
| 337 | + } |
| 338 | + sessionStorage.setItem(bypassReloadKey, 'true'); |
| 339 | + return true; |
| 340 | + } catch { |
| 341 | + return false; |
| 342 | + } |
| 343 | + }; |
| 344 | + const clearBypassReloadMark = () => { |
| 345 | + try { |
| 346 | + sessionStorage.removeItem(bypassReloadKey); |
| 347 | + } catch {} |
| 348 | + }; |
| 349 | + const clearWebGALServiceWorker = async () => { |
| 350 | + try { |
| 351 | + const registrations = await navigator.serviceWorker.getRegistrations(); |
| 352 | + const webgalRegistrations = registrations.filter(isWebGALRegistration); |
| 353 | + const hasWebGALController = navigator.serviceWorker.controller?.scriptURL === webgalServiceWorkerUrl; |
| 354 | + await Promise.all(webgalRegistrations.map((registration) => registration.unregister())); |
| 355 | + if ('caches' in window) { |
| 356 | + const cacheKeys = await caches.keys(); |
| 357 | + await Promise.all(cacheKeys.filter((key) => key.startsWith('webgal-')).map((key) => caches.delete(key))); |
| 358 | + } |
| 359 | + if ((webgalRegistrations.length > 0 || hasWebGALController) && navigator.serviceWorker.controller && markBypassReload()) { |
| 360 | + window.location.reload(); |
| 361 | + } |
| 362 | + } catch (error) { |
| 363 | + console.warn('Failed to clear WebGAL Service Worker cache', error); |
| 364 | + } |
| 365 | + }; |
| 366 | + if (!('serviceWorker' in navigator)) { |
| 367 | + return; |
| 368 | + } |
| 369 | + if (shouldBypassServiceWorker) { |
| 370 | + clearWebGALServiceWorker(); |
| 371 | + return; |
332 | 372 | } |
| 373 | + if (!window.isSecureContext) { |
| 374 | + return; |
| 375 | + } |
| 376 | + clearBypassReloadMark(); |
| 377 | + navigator.serviceWorker |
| 378 | + .register('./webgal-serviceworker.js') |
| 379 | + .then((reg) => { |
| 380 | + console.log('Registration succeeded. Scope is ' + reg.scope); |
| 381 | + }) |
| 382 | + .catch((error) => { |
| 383 | + console.log('Registration failed with ' + error); |
| 384 | + }); |
333 | 385 | })(); |
334 | 386 | </script> |
335 | 387 | <!-- 首屏加载 --> |
|
0 commit comments