11/// <reference lib="webworker" />
22
3- const CACHE_NAME = "FFCS-CodeChefVIT_v1" ;
3+ const CACHE_NAME = "FFCS-CodeChefVIT_v1.1.1" ; // Increment this on each deploy
4+
45const CORE_ASSETS = [
56 "/" ,
67 "/offline" ,
@@ -39,29 +40,37 @@ const limitCacheSize = (name, maxItems) => {
3940} ;
4041
4142self . addEventListener ( "install" , ( event ) => {
43+ console . log ( "Service Worker: Installing..." ) ;
4244 event . waitUntil (
43- caches . open ( CACHE_NAME ) . then ( ( cache ) => {
44- return cache . addAll ( CORE_ASSETS ) ;
45- } )
45+ caches . open ( CACHE_NAME ) . then ( ( cache ) => cache . addAll ( CORE_ASSETS ) )
4646 ) ;
4747 self . skipWaiting ( ) ;
4848} ) ;
4949
5050self . addEventListener ( "activate" , ( event ) => {
51+ console . log ( "Service Worker: Activating..." ) ;
5152 event . waitUntil (
52- caches
53- . keys ( )
54- . then ( ( keys ) =>
55- Promise . all (
56- keys
57- . filter ( ( key ) => key !== CACHE_NAME )
58- . map ( ( key ) => caches . delete ( key ) )
59- )
53+ caches . keys ( ) . then ( ( keys ) =>
54+ Promise . all (
55+ keys . map ( ( key ) => {
56+ if ( key !== CACHE_NAME ) {
57+ console . log ( "Service Worker: Removing old cache" , key ) ;
58+ return caches . delete ( key ) ;
59+ }
60+ } )
6061 )
62+ )
6163 ) ;
6264 self . clients . claim ( ) ;
6365} ) ;
6466
67+ self . addEventListener ( "message" , ( event ) => {
68+ if ( event . data && event . data . type === "SKIP_WAITING" ) {
69+ console . log ( "Service Worker: SKIP_WAITING message received" ) ;
70+ self . skipWaiting ( ) ;
71+ }
72+ } ) ;
73+
6574self . addEventListener ( "fetch" , ( event ) => {
6675 const { request } = event ;
6776
@@ -74,28 +83,36 @@ self.addEventListener("fetch", (event) => {
7483 return ;
7584 }
7685
86+ if ( request . headers . get ( "accept" ) ?. includes ( "text/html" ) ) {
87+ event . respondWith (
88+ fetch ( request )
89+ . then ( ( res ) => {
90+ const resClone = res . clone ( ) ;
91+ caches . open ( CACHE_NAME ) . then ( ( cache ) => {
92+ cache . put ( request , resClone ) ;
93+ } ) ;
94+ return res ;
95+ } )
96+ . catch ( ( ) => caches . match ( request ) . then ( ( res ) => res || caches . match ( "/offline" ) ) )
97+ ) ;
98+ return ;
99+ }
100+
77101 event . respondWith (
78- caches . match ( request ) . then ( ( cacheRes ) => {
102+ caches . match ( request ) . then ( ( res ) => {
79103 return (
80- cacheRes ||
104+ res ||
81105 fetch ( request )
82106 . then ( ( fetchRes ) => {
83107 return caches . open ( CACHE_NAME ) . then ( ( cache ) => {
84- if (
85- request . url . startsWith ( self . location . origin ) &&
86- ! request . url . includes ( "/api" )
87- ) {
88- cache . put ( request . url , fetchRes . clone ( ) ) ;
89- limitCacheSize ( CACHE_NAME , 50 ) ;
108+ if ( request . url . startsWith ( self . location . origin ) ) {
109+ cache . put ( request , fetchRes . clone ( ) ) ;
110+ limitCacheSize ( CACHE_NAME , 100 ) ;
90111 }
91112 return fetchRes ;
92113 } ) ;
93114 } )
94- . catch ( ( ) => {
95- if ( request . headers . get ( "accept" ) ?. includes ( "text/html" ) ) {
96- return caches . match ( "/offline" ) ;
97- }
98- } )
115+ . catch ( ( ) => { } )
99116 ) ;
100117 } )
101118 ) ;
0 commit comments