sw-appcache-behavior
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -508,11 +508,84 @@ /* | ||
var log = () => { | ||
// Evaluate goog.DEBUG at runtime rather than once at export time to allow | ||
// developers to enable logging "on-the-fly" by setting `goog.DEBUG = true` | ||
// in the JavaScript console. | ||
return (self && self.goog && self.goog.DEBUG) ? | ||
console.debug.bind(console) : | ||
function() {}; | ||
self.goog = self.goog || {}; | ||
self.goog.LOG_LEVEL = self.goog.LOG_LEVEL || { | ||
none: -1, | ||
verbose: 0, | ||
debug: 1, | ||
warn: 2, | ||
error: 3, | ||
}; | ||
/** | ||
* A class that will only log given the current log level | ||
* defined by the developer. | ||
* | ||
* Define custom log level by setting `self.goog.logLevel`. | ||
* | ||
* @example | ||
* | ||
* self.goog.logLevel = self.goog.LOG_LEVEL.verbose; | ||
* | ||
* @private | ||
*/ | ||
class LogHelper { | ||
/** | ||
* LogHelper constructor. | ||
*/ | ||
constructor() { | ||
this._allowedLogLevel = location.hostname === 'localhost' ? | ||
self.goog.LOG_LEVEL.debug : self.goog.LOG_LEVEL.none; | ||
} | ||
/** | ||
* The most verbose log level. | ||
*/ | ||
log(...args) { | ||
this._printMessage(self.goog.LOG_LEVEL.verbose, console.log, args); | ||
} | ||
/** | ||
* Useful for logs that are more exceptional that log() | ||
* but not severe. | ||
*/ | ||
debug(...args) { | ||
this._printMessage(self.goog.LOG_LEVEL.debug, console.debug, args); | ||
} | ||
/** | ||
* Warning messages. | ||
*/ | ||
warn(...args) { | ||
this._printMessage(self.goog.LOG_LEVEL.warn, console.warn, args); | ||
} | ||
/** | ||
* Error logs. | ||
*/ | ||
error(...args) { | ||
this._printMessage(self.goog.LOG_LEVEL.error, console.error, args); | ||
} | ||
/** | ||
* Method to print to the console. | ||
* @param {number} logLevel | ||
* @param {function} logMethod | ||
* @param {Object} logArguments | ||
*/ | ||
_printMessage(logLevel, logMethod, logArguments) { | ||
let currentLogLevel = this._allowedLogLevel; | ||
if (self && self.goog && self.goog.logLevel) { | ||
currentLogLevel = self.goog.logLevel; | ||
} | ||
if (currentLogLevel === self.goog.LOG_LEVEL.none || | ||
logLevel < currentLogLevel) { | ||
return; | ||
} | ||
logMethod(...logArguments); | ||
} | ||
} | ||
var logHelper = new LogHelper(); | ||
/* | ||
@@ -582,3 +655,4 @@ Copyright 2016 Google Inc. All Rights Reserved. | ||
.catch((error) => { | ||
log('Error while using clients.get(event.clientId).url: ' + error); | ||
logHelper.error( | ||
'Error while using clients.get(event.clientId).url: ' + error); | ||
// Firefox currently sets the referer to 'about:client' for initial | ||
@@ -623,3 +697,3 @@ // navigations, but that's not useful for our purposes. | ||
function fetchWithFallback(request, fallbackUrl, cacheName) { | ||
log('Trying fetch for', request.url); | ||
logHelper.log('Trying fetch for', request.url); | ||
return fetch(request).then((response) => { | ||
@@ -633,3 +707,3 @@ // Succesful but error-like responses are treated as failures. | ||
}).catch(() => { | ||
log('fetch() failed. Falling back to cache of', fallbackUrl); | ||
logHelper.warn('fetch() failed. Falling back to cache of', fallbackUrl); | ||
return caches.open(cacheName).then( | ||
@@ -670,8 +744,8 @@ (cache) => cache.match(fallbackUrl)); | ||
versions = versions || []; | ||
log('versions is', versions); | ||
logHelper.log('versions is', versions); | ||
return versions.reduce((result, current) => { | ||
log('current is', current); | ||
logHelper.log('current is', current); | ||
// If we already have a result, just keep returning it. | ||
if (result) { | ||
log('result is', result); | ||
logHelper.log('result is', result); | ||
return result; | ||
@@ -683,3 +757,3 @@ } | ||
if (current.hash === manifestHash) { | ||
log('manifestHash match', current); | ||
logHelper.log('manifestHash match', current); | ||
return current.parsed; | ||
@@ -723,3 +797,3 @@ } | ||
function appCacheLogic(event, manifest, hash, clientUrl) { | ||
log('manifest is', manifest, 'version is', hash); | ||
logHelper.log('manifest is', manifest, 'version is', hash); | ||
const requestUrl = event.request.url; | ||
@@ -731,3 +805,3 @@ | ||
if (manifest.cache.includes(requestUrl) || requestUrl === clientUrl) { | ||
log('CACHE includes URL; using cache.match()'); | ||
logHelper.log('CACHE includes URL; using cache.match()'); | ||
// If so, return the cached response. | ||
@@ -744,3 +818,3 @@ return caches.open(hash).then((cache) => cache.match(requestUrl)); | ||
if (fallbackKey) { | ||
log('fallbackKey in parsedManifest matches', fallbackKey); | ||
logHelper.log('fallbackKey in parsedManifest matches', fallbackKey); | ||
return fetchWithFallback(event.request, manifest.fallback[fallbackKey], | ||
@@ -753,3 +827,3 @@ hash); | ||
manifest.network.includes('*')) { | ||
log('Match or * in NETWORK; using fetch()'); | ||
logHelper.log('Match or * in NETWORK; using fetch()'); | ||
return fetch(event.request); | ||
@@ -759,3 +833,3 @@ } | ||
// If nothing matches, then return an error response. | ||
log('Nothing matches; using Response.error()'); | ||
logHelper.log('Nothing matches; using Response.error()'); | ||
return Response.error(); | ||
@@ -821,3 +895,3 @@ } | ||
.then((manifests) => { | ||
log('All manifests:', manifests); | ||
logHelper.log('All manifests:', manifests); | ||
// Use .map() to create an array of the longest matching prefix | ||
@@ -833,3 +907,3 @@ // for each manifest. If no prefixes match for a given manifest, | ||
}); | ||
log('longestForEach:', longestForEach); | ||
logHelper.log('longestForEach:', longestForEach); | ||
@@ -846,3 +920,3 @@ // Next, find which of the longest matching prefixes from each | ||
}, {prefix: '', index: 0}); | ||
log('longest:', longest); | ||
logHelper.log('longest:', longest); | ||
@@ -852,9 +926,9 @@ // Now that we know the longest overall prefix, we'll use that | ||
const fallbackKey = longest.prefix; | ||
log('fallbackKey:', fallbackKey); | ||
logHelper.log('fallbackKey:', fallbackKey); | ||
if (fallbackKey) { | ||
const winningManifest = manifests[longest.index]; | ||
log('winningManifest:', winningManifest); | ||
logHelper.log('winningManifest:', winningManifest); | ||
const winningManifestVersion = | ||
winningManifest[winningManifest.length - 1]; | ||
log('winningManifestVersion:', winningManifestVersion); | ||
logHelper.log('winningManifestVersion:', winningManifestVersion); | ||
const hash = winningManifestVersion.hash; | ||
@@ -867,3 +941,3 @@ const parsedManifest = winningManifestVersion.parsed; | ||
// If nothing matches, then just fetch(). | ||
log('Nothing at all matches. Using fetch()'); | ||
logHelper.log('Nothing at all matches. Using fetch()'); | ||
return fetch(event.request); | ||
@@ -883,3 +957,3 @@ }); | ||
const requestUrl = new URL(event.request.url); | ||
log('Starting appCacheBehaviorForUrl for ' + requestUrl); | ||
logHelper.log('Starting appCacheBehaviorForUrl for ' + requestUrl); | ||
@@ -889,3 +963,3 @@ // If this is a request that, as per the AppCache spec, should be handled | ||
if (event.request.headers.get('X-Use-Fetch') === 'true') { | ||
log('Using fetch() because X-Use-Fetch: true'); | ||
logHelper.log('Using fetch() because X-Use-Fetch: true'); | ||
return fetch(event.request); | ||
@@ -897,3 +971,4 @@ } | ||
requestUrl.protocol !== location.protocol) { | ||
log('Using fetch() because AppCache does not apply to this request.'); | ||
logHelper.log( | ||
'Using fetch() because AppCache does not apply to this request.'); | ||
return fetch(event.request); | ||
@@ -903,6 +978,6 @@ } | ||
return getClientUrlForEvent(event).then((clientUrl) => { | ||
log('clientUrl is', clientUrl); | ||
logHelper.log('clientUrl is', clientUrl); | ||
return idbHelpers[constants.STORES.PATH_TO_MANIFEST].get(clientUrl) | ||
.then((manifestUrl) => { | ||
log('manifestUrl is', manifestUrl); | ||
logHelper.log('manifestUrl is', manifestUrl); | ||
@@ -913,3 +988,3 @@ if (manifestUrl) { | ||
log('No matching manifest for client found.'); | ||
logHelper.log('No matching manifest for client found.'); | ||
return noManifestBehavior(event); | ||
@@ -991,3 +1066,3 @@ }); | ||
}).then((idsToDelete) => { | ||
log('deleting cache ids', idsToDelete); | ||
logHelper.log('deleting cache ids', idsToDelete); | ||
return Promise.all(idsToDelete.map((cacheId) => caches.delete(cacheId))); | ||
@@ -1067,3 +1142,3 @@ }); | ||
function fetchBehavior(event) { | ||
log('client id is', event.clientId); | ||
logHelper.log('client id is', event.clientId); | ||
return appCacheBehaviorForEvent(event).then((response) => { | ||
@@ -1070,0 +1145,0 @@ // If this is a navigation, clean up unused caches that correspond to old |
@@ -375,3 +375,3 @@ /* | ||
* Licensed under the MIT license: | ||
* http://www.opensource.org/licenses/MIT | ||
* https://opensource.org/licenses/MIT | ||
* | ||
@@ -744,2 +744,29 @@ * Based on | ||
/** | ||
* # sw-appcache-behavior | ||
* | ||
* A service worker implementation of the behavior defined in a page's App | ||
* Cache manifest. | ||
* | ||
* In your web page you need to add the client-runtime.js file: | ||
* | ||
* ``` | ||
* <script src="../build/client-runtime.js" | ||
* data-service-worker="service-worker.js"></script> | ||
* ``` | ||
* | ||
* Then in your servier worker you must import the appcache-behavior-import.js | ||
* file: | ||
* | ||
* ``` | ||
* importScripts('../build/appcache-behavior-import.js'); | ||
* | ||
* self.addEventListener('fetch', (event) => { | ||
* event.respondWith(goog.appCacheBehavior.fetch(event)); | ||
* }); | ||
* ``` | ||
* | ||
* @module sw-appcache-behavior | ||
*/ | ||
const swScript = document.currentScript.dataset.serviceWorker; | ||
@@ -746,0 +773,0 @@ const manifestAttribute = document.documentElement.getAttribute('manifest'); |
{ | ||
"name": "sw-appcache-behavior", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "A service worker implementation of the behavior defined in a page's App Cache manifest.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -21,3 +21,3 @@ /* | ||
import IDBHelper from '../../../lib/idb-helper.js'; | ||
import log from '../../../lib/log.js'; | ||
import logHelper from '../../../lib/log-helper.js'; | ||
import constants from './lib/constants.js'; | ||
@@ -47,3 +47,4 @@ | ||
.catch((error) => { | ||
log('Error while using clients.get(event.clientId).url: ' + error); | ||
logHelper.error( | ||
'Error while using clients.get(event.clientId).url: ' + error); | ||
// Firefox currently sets the referer to 'about:client' for initial | ||
@@ -88,3 +89,3 @@ // navigations, but that's not useful for our purposes. | ||
function fetchWithFallback(request, fallbackUrl, cacheName) { | ||
log('Trying fetch for', request.url); | ||
logHelper.log('Trying fetch for', request.url); | ||
return fetch(request).then((response) => { | ||
@@ -98,3 +99,3 @@ // Succesful but error-like responses are treated as failures. | ||
}).catch(() => { | ||
log('fetch() failed. Falling back to cache of', fallbackUrl); | ||
logHelper.warn('fetch() failed. Falling back to cache of', fallbackUrl); | ||
return caches.open(cacheName).then( | ||
@@ -135,8 +136,8 @@ (cache) => cache.match(fallbackUrl)); | ||
versions = versions || []; | ||
log('versions is', versions); | ||
logHelper.log('versions is', versions); | ||
return versions.reduce((result, current) => { | ||
log('current is', current); | ||
logHelper.log('current is', current); | ||
// If we already have a result, just keep returning it. | ||
if (result) { | ||
log('result is', result); | ||
logHelper.log('result is', result); | ||
return result; | ||
@@ -148,3 +149,3 @@ } | ||
if (current.hash === manifestHash) { | ||
log('manifestHash match', current); | ||
logHelper.log('manifestHash match', current); | ||
return current.parsed; | ||
@@ -188,3 +189,3 @@ } | ||
function appCacheLogic(event, manifest, hash, clientUrl) { | ||
log('manifest is', manifest, 'version is', hash); | ||
logHelper.log('manifest is', manifest, 'version is', hash); | ||
const requestUrl = event.request.url; | ||
@@ -196,3 +197,3 @@ | ||
if (manifest.cache.includes(requestUrl) || requestUrl === clientUrl) { | ||
log('CACHE includes URL; using cache.match()'); | ||
logHelper.log('CACHE includes URL; using cache.match()'); | ||
// If so, return the cached response. | ||
@@ -209,3 +210,3 @@ return caches.open(hash).then((cache) => cache.match(requestUrl)); | ||
if (fallbackKey) { | ||
log('fallbackKey in parsedManifest matches', fallbackKey); | ||
logHelper.log('fallbackKey in parsedManifest matches', fallbackKey); | ||
return fetchWithFallback(event.request, manifest.fallback[fallbackKey], | ||
@@ -218,3 +219,3 @@ hash); | ||
manifest.network.includes('*')) { | ||
log('Match or * in NETWORK; using fetch()'); | ||
logHelper.log('Match or * in NETWORK; using fetch()'); | ||
return fetch(event.request); | ||
@@ -224,3 +225,3 @@ } | ||
// If nothing matches, then return an error response. | ||
log('Nothing matches; using Response.error()'); | ||
logHelper.log('Nothing matches; using Response.error()'); | ||
return Response.error(); | ||
@@ -286,3 +287,3 @@ } | ||
.then((manifests) => { | ||
log('All manifests:', manifests); | ||
logHelper.log('All manifests:', manifests); | ||
// Use .map() to create an array of the longest matching prefix | ||
@@ -298,3 +299,3 @@ // for each manifest. If no prefixes match for a given manifest, | ||
}); | ||
log('longestForEach:', longestForEach); | ||
logHelper.log('longestForEach:', longestForEach); | ||
@@ -311,3 +312,3 @@ // Next, find which of the longest matching prefixes from each | ||
}, {prefix: '', index: 0}); | ||
log('longest:', longest); | ||
logHelper.log('longest:', longest); | ||
@@ -317,9 +318,9 @@ // Now that we know the longest overall prefix, we'll use that | ||
const fallbackKey = longest.prefix; | ||
log('fallbackKey:', fallbackKey); | ||
logHelper.log('fallbackKey:', fallbackKey); | ||
if (fallbackKey) { | ||
const winningManifest = manifests[longest.index]; | ||
log('winningManifest:', winningManifest); | ||
logHelper.log('winningManifest:', winningManifest); | ||
const winningManifestVersion = | ||
winningManifest[winningManifest.length - 1]; | ||
log('winningManifestVersion:', winningManifestVersion); | ||
logHelper.log('winningManifestVersion:', winningManifestVersion); | ||
const hash = winningManifestVersion.hash; | ||
@@ -332,3 +333,3 @@ const parsedManifest = winningManifestVersion.parsed; | ||
// If nothing matches, then just fetch(). | ||
log('Nothing at all matches. Using fetch()'); | ||
logHelper.log('Nothing at all matches. Using fetch()'); | ||
return fetch(event.request); | ||
@@ -348,3 +349,3 @@ }); | ||
const requestUrl = new URL(event.request.url); | ||
log('Starting appCacheBehaviorForUrl for ' + requestUrl); | ||
logHelper.log('Starting appCacheBehaviorForUrl for ' + requestUrl); | ||
@@ -354,3 +355,3 @@ // If this is a request that, as per the AppCache spec, should be handled | ||
if (event.request.headers.get('X-Use-Fetch') === 'true') { | ||
log('Using fetch() because X-Use-Fetch: true'); | ||
logHelper.log('Using fetch() because X-Use-Fetch: true'); | ||
return fetch(event.request); | ||
@@ -362,3 +363,4 @@ } | ||
requestUrl.protocol !== location.protocol) { | ||
log('Using fetch() because AppCache does not apply to this request.'); | ||
logHelper.log( | ||
'Using fetch() because AppCache does not apply to this request.'); | ||
return fetch(event.request); | ||
@@ -368,6 +370,6 @@ } | ||
return getClientUrlForEvent(event).then((clientUrl) => { | ||
log('clientUrl is', clientUrl); | ||
logHelper.log('clientUrl is', clientUrl); | ||
return idbHelpers[constants.STORES.PATH_TO_MANIFEST].get(clientUrl) | ||
.then((manifestUrl) => { | ||
log('manifestUrl is', manifestUrl); | ||
logHelper.log('manifestUrl is', manifestUrl); | ||
@@ -378,3 +380,3 @@ if (manifestUrl) { | ||
log('No matching manifest for client found.'); | ||
logHelper.log('No matching manifest for client found.'); | ||
return noManifestBehavior(event); | ||
@@ -456,3 +458,3 @@ }); | ||
}).then((idsToDelete) => { | ||
log('deleting cache ids', idsToDelete); | ||
logHelper.log('deleting cache ids', idsToDelete); | ||
return Promise.all(idsToDelete.map((cacheId) => caches.delete(cacheId))); | ||
@@ -532,3 +534,3 @@ }); | ||
function fetchBehavior(event) { | ||
log('client id is', event.clientId); | ||
logHelper.log('client id is', event.clientId); | ||
return appCacheBehaviorForEvent(event).then((response) => { | ||
@@ -535,0 +537,0 @@ // If this is a navigation, clean up unused caches that correspond to old |
@@ -18,2 +18,29 @@ /* | ||
/** | ||
* # sw-appcache-behavior | ||
* | ||
* A service worker implementation of the behavior defined in a page's App | ||
* Cache manifest. | ||
* | ||
* In your web page you need to add the client-runtime.js file: | ||
* | ||
* ``` | ||
* <script src="../build/client-runtime.js" | ||
* data-service-worker="service-worker.js"></script> | ||
* ``` | ||
* | ||
* Then in your servier worker you must import the appcache-behavior-import.js | ||
* file: | ||
* | ||
* ``` | ||
* importScripts('../build/appcache-behavior-import.js'); | ||
* | ||
* self.addEventListener('fetch', (event) => { | ||
* event.respondWith(goog.appCacheBehavior.fetch(event)); | ||
* }); | ||
* ``` | ||
* | ||
* @module sw-appcache-behavior | ||
*/ | ||
import constants from './lib/constants.js'; | ||
@@ -20,0 +47,0 @@ import idb from 'idb'; |
110567
2768