-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🦜 Cache remote assets using a ServiceWorker in the browser (#555)
This introduces a ServiceWorker to intercept and cache network requests to remote assets in the browser. It works for Clerk's js bundle, its tailwind css script, fonts and as well as javascript dynamically loaded using d3-require like Clerk's Vega and Plotly viewers. To use it, you need to open Clerk in the browser when online to populate the cache. Viewers that are dynamically loaded (e.g. Vega or Plotly) need to be used once while offline to be cached. We're considering loading them on worker init in a follow up. We currently apply it to the following urls: * https://fonts.bunny.net * https://cdn.skypack.dev * https://cdn.tailwindcss.com * https://storage.clerk.garden * https://cdn.jsdelivr.net * https://vega.github.io Co-authored-by: Martin Kavalar <martin@nextjournal.com>
- Loading branch information
1 parent
4e627e8
commit f35635f
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const cacheName = 'clerk-browser-cache-v2'; | ||
|
||
const hosts = [ | ||
'https://fonts.bunny.net', | ||
'https://cdn.skypack.dev', | ||
'https://cdn.tailwindcss.com', | ||
'https://storage.clerk.garden', | ||
'https://cdn.jsdelivr.net', | ||
'https://vega.github.io' | ||
]; | ||
|
||
self.addEventListener('install', function(event) { | ||
//console.log('install', event); | ||
self.skipWaiting(); | ||
}); | ||
|
||
self.addEventListener('activate', function(event) { | ||
//console.log('activate', event); | ||
|
||
// Remove unwanted caches | ||
event.waitUntil( | ||
caches.keys().then(function(cacheNames) { | ||
return Promise.all( | ||
cacheNames.map(function(cache) { | ||
if (cache !== cacheName) { | ||
console.log("Service Worker: Clearing old cache"); | ||
return caches.delete(cache); | ||
} | ||
})); | ||
})); | ||
|
||
return self.clients.claim() | ||
}); | ||
|
||
self.addEventListener('fetch', function(event) { | ||
//console.log(event); | ||
|
||
event.respondWith( | ||
caches.match(event.request).then(function(response) { | ||
return response || fetch(event.request).then(function(response) { | ||
|
||
hosts.map(function(host) { | ||
if (event.request.url.indexOf(host) === 0) { | ||
var clonedResponse = response.clone(); | ||
caches.open(cacheName).then(function(cache) { | ||
cache.put(event.request, clonedResponse); | ||
}); | ||
} | ||
}); | ||
return response; | ||
}); | ||
}) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters