broccoli-serviceworker
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -12,9 +12,9 @@ var fs = require("fs"); | ||
} | ||
this.inTree = inTree; | ||
this.inTree = inTree; | ||
options = options || {}; | ||
this.addPolyfill = options.addPolyfill || true; | ||
this.addPolyfill = options.addPolyfill || true; | ||
this.debug = options.debug || true; | ||
this.dynamicCache = options.dynamicCache || []; | ||
this.excludePaths = options.excludePaths || ['tests/*']; | ||
this.fallback = options.fallback || []; | ||
this.fallback = options.fallback || []; | ||
this.includePaths = options.includePaths || []; | ||
@@ -39,4 +39,4 @@ this.polyFillLocation = options.polyFillLocation || 'serviceworker-cache-polyfill.js'; | ||
}); | ||
return readTree(serviceWorkerTree).then(function (srcDir) { | ||
return readTree(serviceWorkerTree).then(function (srcDir) { | ||
var cacheVersion = (new Date()).getTime(); | ||
@@ -53,3 +53,3 @@ var lines = []; | ||
lines.push("CURRENT_CACHES['dynamic'] = 'dynamic-cache-v' + CACHE_VERSION;"); | ||
} | ||
} | ||
if (dynamicCache.length) { | ||
@@ -67,3 +67,3 @@ lines.push("var DYNAMIC_URLS = ["); | ||
if (fallbackParts.length > 1) { | ||
var matchLine = "{match: new RegExp('"+escapeRegExp(fallbackParts[0])+"'), fallback:'"+fallbackParts[1]+"'}"; | ||
var matchLine = "{match: new RegExp('"+escapeRegExp(fallbackParts[0])+"'), fallback:'"+fallbackParts[1]+"'}"; | ||
lines.push(createArrayLine(matchLine, idx, array.length)); | ||
@@ -74,4 +74,4 @@ } | ||
} | ||
lines.push("self.addEventListener('install', function(event) {"); | ||
@@ -88,7 +88,7 @@ lines.push(" var urlsToPrefetch = ["); | ||
lines.push("];"); | ||
includePaths.forEach(function (file, idx, array) { | ||
lines.push("urlsToPrefetch.push('"+file+"');"); | ||
}); | ||
//ServiceWorker code derived from examples at https://github.com/GoogleChrome/samples/tree/gh-pages/service-worker | ||
@@ -136,3 +136,3 @@ addDebugLine("'Handling install event. Resources to pre-fetch:', urlsToPrefetch", debug, lines); | ||
addDebugLine("'Found dynamic cache response:', event.request.url", debug, lines); | ||
lines.push(" return;"); | ||
lines.push(" return;"); | ||
lines.push(" }"); | ||
@@ -142,3 +142,3 @@ } | ||
lines.push(" event.respondWith("); | ||
lines.push(" // caches.match() will look for a cache entry in all of the caches available to the service worker."); | ||
@@ -151,3 +151,3 @@ lines.push(" // It's an alternative to first opening a specific named cache and then matching on that."); | ||
lines.push(" if (response.status >= 400) {"); | ||
addDebugLine("'Got error status, checking for fallback. Response status was:', response.status", debug, lines); | ||
addDebugLine("'Got error status, checking for fallback. Response status was:', response.status", debug, lines); | ||
lines.push(" return fallbackResponse(event.request, response);"); | ||
@@ -171,3 +171,3 @@ lines.push(" }"); | ||
lines.push(" return fallbackResponse(event.request, response);"); | ||
} else { | ||
} else { | ||
lines.push(" console.error('Fetching failed:', error);"); | ||
@@ -179,3 +179,3 @@ lines.push(" throw error;"); | ||
lines.push(" );"); | ||
lines.push("});"); | ||
lines.push("});"); | ||
if (dynamicCache.length) { | ||
@@ -186,10 +186,10 @@ lines.push("function dynamicCacheResponse(event) {"); | ||
lines.push(" });"); | ||
lines.push(" if (matchingUrls.length) {"); | ||
addDebugLine("'Pulling dynamic url: '+event.request.url+' from network and adding to cache.'", debug, lines); | ||
lines.push(" if (matchingUrls.length) {"); | ||
addDebugLine("'Pulling dynamic url: '+event.request.url+' from network and adding to cache.'", debug, lines); | ||
lines.push(" event.respondWith("); | ||
lines.push(" caches.open('dynamic-cache-v"+cacheVersion+"').then(function(cache) {"); | ||
lines.push(" caches.open('dynamic-cache-v"+cacheVersion+"').then(function(cache) {"); | ||
lines.push(" return fetch(event.request).then(function(response) {"); | ||
addDebugLine("'Got response for dynamic url: '+event.request.url+' now adding to cache.', response", debug, lines); | ||
lines.push(" if (response.status >= 400) {"); | ||
addDebugLine("'Got response error for dynamic url, try to pull from cache: ',response.status", debug, lines); | ||
addDebugLine("'Got response error for dynamic url, try to pull from cache: ',response.status", debug, lines); | ||
lines.push(" caches.match(event.request).then(function(response) {"); | ||
@@ -199,11 +199,11 @@ lines.push(" return response;"); | ||
lines.push(" } else {"); | ||
lines.push(" cache.put(event.request, response.clone());"); | ||
lines.push(" cache.put(event.request, response.clone());"); | ||
lines.push(" return response;"); | ||
lines.push(" }"); | ||
lines.push(" }"); | ||
lines.push(" }).catch(function(error) {"); | ||
addDebugLine("'Got error for dynamic url, try to pull from cache: ',error", debug, lines); | ||
addDebugLine("'Got error for dynamic url, try to pull from cache: ',error", debug, lines); | ||
lines.push(" caches.match(event.request).then(function(response) {"); | ||
lines.push(" return response;"); | ||
lines.push(" });"); | ||
lines.push(" });"); | ||
lines.push(" });"); | ||
lines.push(" });"); | ||
lines.push(" })"); | ||
@@ -214,5 +214,5 @@ lines.push(" );"); | ||
lines.push(" return false;"); | ||
lines.push(" }"); | ||
lines.push(" }"); | ||
lines.push("}"); | ||
} | ||
} | ||
if (fallback.length) { | ||
@@ -222,6 +222,6 @@ lines.push("function fallbackResponse(request, response) {"); | ||
lines.push(" var matchingUrls =FALLBACK_URLS.filter(function(fallbackURL) {"); | ||
addDebugLine("'Checking for fallback match with:', fallbackURL", debug, lines); | ||
lines.push(" return (request.url.match(fallbackURL.match) !== null);"); | ||
addDebugLine("'Checking for fallback match with:', fallbackURL", debug, lines); | ||
lines.push(" return (request.url.match(fallbackURL.match) !== null);"); | ||
lines.push(" });"); | ||
lines.push(" if (matchingUrls.length) {"); | ||
lines.push(" if (matchingUrls.length) {"); | ||
addDebugLine("'Fetching from fallback url: '+ matchingUrls[0].fallback +'for url: '+event.request.url", debug, lines); | ||
@@ -231,3 +231,3 @@ lines.push(" return caches.match(matchingUrls[0].fallback);"); | ||
lines.push(" return response; "); | ||
lines.push(" } "); | ||
lines.push(" } "); | ||
lines.push("}"); | ||
@@ -237,3 +237,3 @@ } | ||
if (addPolyfill) { | ||
fs.createReadStream(swCachePolyFillFile).pipe(fs.createWriteStream(path.join(destDir, polyFillLocation))); | ||
fs.writeFileSync(path.join(destDir, polyFillLocation), fs.readFileSync(swCachePolyFillFile)); | ||
} | ||
@@ -240,0 +240,0 @@ }); |
{ | ||
"name": "broccoli-serviceworker", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A broccoli plugin automating ServiceWorker file creation for Broccoli and Ember.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/service-worker.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17429
2