Browse Source

Editor: Improve service worker caching and activation.

Mr.doob 1 month ago
parent
commit
c36ae33e09
1 changed files with 22 additions and 2 deletions
  1. 22 2
      editor/sw.js

+ 22 - 2
editor/sw.js

@@ -235,7 +235,7 @@ self.addEventListener( 'install', async function () {
 
 
 	const cache = await caches.open( cacheName );
 	const cache = await caches.open( cacheName );
 
 
-	assets.forEach( async function ( asset ) {
+	await Promise.all( assets.map( async function ( asset ) {
 
 
 		try {
 		try {
 
 
@@ -247,7 +247,27 @@ self.addEventListener( 'install', async function () {
 
 
 		}
 		}
 
 
-	} );
+	} ) );
+
+	self.skipWaiting();
+
+} );
+
+self.addEventListener( 'activate', async function ( event ) {
+
+	event.waitUntil(
+		caches.keys().then( function ( names ) {
+
+			return Promise.all(
+				names.filter( name => name !== cacheName ).map( name => caches.delete( name ) )
+			);
+
+		} ).then( function () {
+
+			self.clients.claim();
+
+		} )
+	);
 
 
 } );
 } );
 
 

粤ICP备19079148号