Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Service Worker With Custom Caching Strategies Based On File Type
- // 1. Install -> Cache Once Then Return Cache
- // 2. Excluded Files -> Cache Once Then Return Cache
- // 3. Final Stage (HTML) -> Stale While Re-Validate With Fallback Page ( Offline Page )
- cacheName = "v'.VERSION.'";
- var offlineURL = "offline_'.DEFAULT_LANG.'.html";
- // These Files Will Be Cached By The Service Worker Install Event
- // These Files Are Only Cached With Install And Then Returned As Cache Always
- var appShellFiles = [
- "offline_'.DEFAULT_LANG.'.html",
- "'.FAVICO16.'",
- "'.FAVICO32.'",
- "'.BOOTSTRAP_CSS.'",
- "'.BOOTSTRAP_JS.'",
- "'.FONT_AWESOME.'",
- "'.POPPER.'",
- "'.CUSTOM_CSS.'",
- "'.JQUERY.'"
- ];
- // Files To Use In Cache First Then Network Strategy, With Caching On Network
- // These Files Will Only Be Cached Once From Network Then Returned As Cache Always
- var excludeExts = [
- ".css",
- ".js",
- ".webp",
- ".png",
- ".jpg"
- ];
- // Install Event
- self.addEventListener("install", event => {
- // PreCache App Files
- //console.log("[Service Worker][Install] PreCahing...");
- event.waitUntil(
- caches.open(cacheName)
- .then(cache => cache.addAll(appShellFiles))
- .then(self.skipWaiting())
- );
- });
- // Fetch Event
- self.addEventListener("fetch", event => {
- const requestedURL = event.request.url;
- // Allow Local Files Only
- if (event.request.url.startsWith(self.location.origin)) {
- var flag = false;
- // Return All App Files From Cache
- //console.log("[Service Worker][New Fetch] -> "+requestedURL);
- appShellFiles.forEach(function(appShellFile){
- if(requestedURL.includes(appShellFile)){
- flag = true;
- //console.log("[Service Worker][AppFiles] Return From Cache -> "+requestedURL);
- event.respondWith(
- caches.match(event.request).then(cachedResponse => {
- if (cachedResponse) {
- return cachedResponse;
- }
- })
- );
- }
- });
- if(!flag){
- //console.log("[Service Worker] Determine Strategy -> "+requestedURL);
- var flagb = false;
- // Excluded File Extentions
- excludeExts.forEach(function(excludeExt){
- if(requestedURL.includes(excludeExt)){
- //console.log("[Service Worker][Resources] Cache First Then Network -> "+requestedURL);
- flagb = true;
- event.respondWith(
- caches.open(cacheName).then(function(cache) {
- return cache.match(event.request).then(function (response) {
- return response || fetch(event.request).then(function(response) {
- cache.put(event.request, response.clone());
- return response;
- }).catch(function() {
- //console.log("[Service Worker] Resource Not Found -> "+requestedURL);
- })
- });
- })
- );
- }
- });
- if(!flagb){
- // Final Stage ( Stale While Re-Validate )
- var fetched = fetch(event.request);
- var cached = caches.match(event.request);
- var fetchedCopy = fetched.then(resp => resp.clone());
- //console.log("[Service Worker][Final Stage] Stale While Re-Validate -> "+requestedURL);
- event.respondWith(
- Promise.race([fetched.catch(_ => cached), cached])
- .then(resp => resp || fetched)
- .catch(function(){
- return caches.open(cacheName).then(function(cache) {
- //console.log("[Service Worker][Final Stage] Fallback -> "+requestedURL);
- return cache.match(offlineURL);// Fallback Page
- });
- })
- );
- // Update The Cache
- event.waitUntil(
- Promise.all([fetchedCopy, caches.open(cacheName)])
- .then(([response, cache]) => cache.put(event.request, response))
- .catch(_ => {/* eat any errors */})
- );
- }
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement