import { timestamp , files , shell } from "@sapper/service-worker" ; const ASSETS = `cache${ timestamp }` ; const toCache = ( shell as string [ ] ) . concat ( files as string [ ] ) ; const cached = new Set ( toCache ) ; self . addEventListener ( "install" , < EventType extends ExtendableEvent > ( event ) => { event . waitUntil ( caches . open ( ASSETS ) . then ( ( cache ) => cache . addAll ( toCache ) ) . then ( ( ) => { ( self as any as ServiceWorkerGlobalScope ) . skipWaiting ( ) ; } ) ) ; } ) ; self . addEventListener ( "activate" , < EventType extends ExtendableEvent > ( event ) => { event . waitUntil ( caches . keys ( ) . then ( async ( keys ) => { for ( const key of keys ) { if ( key !== ASSETS ) await caches . delete ( key ) ; } ( self as any as { clients : Clients ; } ) . clients . claim ( ) ; } ) ) ; } ) ; self . addEventListener ( "fetch" , < EventType extends FetchEvent > ( event ) => { if ( event . request . method !== "GET" || event . request . headers . has ( "range" ) ) return ; const url = new URL ( event . request . url ) ; if ( ! url . protocol . startsWith ( "http" ) ) return ; if ( url . hostname === self . location . hostname && url . port !== self . location . port ) return ; if ( url . host === self . location . host && cached . has ( url . pathname ) ) { event . respondWith ( caches . match ( event . request ) as Promise < Response > ) ; return ; } if ( event . request . cache === "only-if-cached" ) return ; event . respondWith ( caches . open ( `offline${ timestamp }` ) . then ( async ( cache ) => { try { const response = await fetch ( event . request ) ; cache . put ( event . request , response . clone ( ) ) ; return response ; } catch ( err ) { const response = await cache . match ( event . request ) ; if ( response ) return response ; throw err ; } } ) ) ; } ) ;