@@ -5,10 +5,10 @@ import { Notification } from '../notification.js'
55import { sleep } from '../timers.js'
66import globals from '../internal/globals.js'
77import crypto from '../crypto.js'
8+ import hooks from '../hooks.js'
89import ipc from '../ipc.js'
910
1011export const workers = new Map ( )
11- export const channel = new ipc . IPCBroadcastChannel ( 'socket.runtime.serviceWorker' )
1212
1313globals . register ( 'ServiceWorkerContext.workers' , workers )
1414globals . register ( 'ServiceWorkerContext.info' , new Map ( ) )
@@ -24,7 +24,7 @@ sharedWorker.port.onmessage = (event) => {
2424 }
2525 }
2626 } else if ( event . data ?. showNotification && event . data . registration ?. id ) {
27- onShowNotification ( event , sharedWorker . port )
27+ onNotificationShow ( event , sharedWorker . port )
2828 } else if ( event . data ?. getNotifications && event . data . registration ?. id ) {
2929 onGetNotifications ( event , sharedWorker . port )
3030 }
@@ -37,12 +37,17 @@ export class ServiceWorkerInstance extends Worker {
3737 constructor ( filename , options ) {
3838 options = { ...options }
3939 const info = options . info ?? null
40-
4140 if ( info . serializedWorkerArgs ) {
42- options . args = JSON . parse ( decodeURIComponent ( info . serializedWorkerArgs ) )
43- options . args . index = globalThis . __args . index
41+ try {
42+ options . args = JSON . parse ( info . serializedWorkerArgs )
43+ } catch {
44+ try {
45+ options . args = JSON . parse ( decodeURIComponent ( info . serializedWorkerArgs ) )
46+ } catch {
47+ options . args = JSON . parse ( decodeURIComponent ( decodeURIComponent ( info . serializedWorkerArgs ) ) )
48+ }
49+ }
4450 }
45-
4651 super ( filename , {
4752 name : `ServiceWorker (${ options ?. info ?. pathname ?? filename } )` ,
4853 ...options ,
@@ -106,7 +111,7 @@ export class ServiceWorkerInstance extends Worker {
106111 log . scrollTop = log . scrollHeight
107112 }
108113 } else if ( event . data ?. showNotification && event . data . registration ?. id ) {
109- onShowNotification ( event , this )
114+ onNotificationShow ( event , this )
110115 } else if ( event . data ?. getNotifications && event . data . registration ?. id ) {
111116 onGetNotifications ( event , this )
112117 } else if ( event . data ?. notificationclose ?. id ) {
@@ -120,7 +125,6 @@ export class ServiceWorkerInfo {
120125 url = null
121126 hash = null
122127 scope = null
123- scheme = null
124128 scriptURL = null
125129 serializedWorkerArgs = null
126130
@@ -134,7 +138,7 @@ export class ServiceWorkerInfo {
134138
135139 const url = new URL ( this . scriptURL )
136140 this . url = url . toString ( )
137- this . hash = crypto . murmur3 ( this . scheme + url . pathname + ( this . scope || '' ) )
141+ this . hash = crypto . murmur3 ( url . pathname + ( this . scope || '' ) )
138142 }
139143
140144 get pathname ( ) {
@@ -205,7 +209,7 @@ export async function onFetch (event) {
205209 }
206210 } ) ( ) ,
207211 new Promise ( ( resolve ) => {
208- globalThis . addEventListener (
212+ globalThis . top . addEventListener (
209213 'serviceWorker.activate' ,
210214 async function ( event ) {
211215 // @ts -ignore
@@ -250,7 +254,7 @@ export async function onFetch (event) {
250254 worker . postMessage ( { fetch : { ...info , client, request } } )
251255}
252256
253- export function onShowNotification ( event , target ) {
257+ export function onNotificationShow ( event , target ) {
254258 for ( const worker of workers . values ( ) ) {
255259 if ( worker . info . id === event . data . registration . id ) {
256260 let notification = null
@@ -353,14 +357,22 @@ export function onGetNotifications (event, target) {
353357
354358export default null
355359
356- globalThis . addEventListener ( 'serviceWorker.register' , onRegister )
357- globalThis . addEventListener ( 'serviceWorker.unregister' , onUnregister )
358- globalThis . addEventListener ( 'serviceWorker.skipWaiting' , onSkipWaiting )
359- globalThis . addEventListener ( 'serviceWorker.activate' , onActivate )
360- globalThis . addEventListener ( 'serviceWorker.fetch' , onFetch )
360+ globalThis . top . addEventListener ( 'serviceWorker.register' , onRegister )
361+ globalThis . top . addEventListener ( 'serviceWorker.unregister' , onUnregister )
362+ globalThis . top . addEventListener ( 'serviceWorker.skipWaiting' , onSkipWaiting )
363+ globalThis . top . addEventListener ( 'serviceWorker.activate' , onActivate )
364+ globalThis . top . addEventListener ( 'serviceWorker.fetch' , onFetch )
365+
366+ hooks . onReady ( async ( ) => {
367+ // notify top frame that the service worker init module is ready
368+ globalThis . top . postMessage ( {
369+ __service_worker_frame_init : true
370+ } )
361371
362- channel . addEventListener ( 'message' , ( e ) => {
363- if ( e . data ?. type ?. startsWith ( 'serviceWorker.' ) ) {
364- globalThis . dispatchEvent ( new CustomEvent ( e . data . type , e . data ) )
372+ const result = await ipc . request ( 'serviceWorker.getRegistrations' )
373+ if ( Array . isArray ( result . data ) ) {
374+ for ( const info of result . data ) {
375+ await navigator . serviceWorker . register ( info . scriptURL , info )
376+ }
365377 }
366378} )
0 commit comments