Socket
Socket
Sign inDemoInstall

workbox-strategies

Package Overview
Dependencies
Maintainers
4
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

workbox-strategies - npm Package Compare versions

Comparing version 3.6.2 to 4.0.0-alpha.0

LICENSE

1099

build/workbox-strategies.dev.js
this.workbox = this.workbox || {};
this.workbox.strategies = (function (logger_mjs,assert_mjs,cacheNames_mjs,cacheWrapper_mjs,fetchWrapper_mjs,getFriendlyURL_mjs) {
this.workbox.strategies = (function (logger_mjs,assert_mjs,cacheNames_mjs,cacheWrapper_mjs,fetchWrapper_mjs,getFriendlyURL_mjs,WorkboxError_mjs) {
'use strict';
try {
self.workbox.v['workbox:strategies:3.6.2'] = 1;
self.workbox.v['workbox:strategies:4.0.0-alpha.0'] = 1;
} catch (e) {} // eslint-disable-line
/*
Copyright 2018 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Copyright 2018 Google LLC
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

@@ -26,5 +19,7 @@

const urlObj = new URL(url, location);
if (urlObj.origin === location.origin) {
return urlObj.pathname;
}
return urlObj.href;

@@ -45,16 +40,8 @@ };

/*
Copyright 2018 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Copyright 2018 Google LLC
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**

@@ -68,4 +55,8 @@ * An implementation of a [cache-first]{@link https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#cache-falling-back-to-network}

*
* If the network request fails, and there is no cache match, this will throw
* a `WorkboxError` exception.
*
* @memberof workbox.strategies
*/
class CacheFirst {

@@ -90,3 +81,2 @@ /**

}
/**

@@ -98,26 +88,17 @@ * This method will perform a request strategy and follows an API that

* @param {Object} options
* @param {FetchEvent} options.event The fetch event to run this strategy
* against.
* @param {Request} options.request The request to run this strategy for.
* @param {Event} [options.event] The event that triggered the request.
* @return {Promise<Response>}
*/
handle({ event }) {
var _this = this;
return babelHelpers.asyncToGenerator(function* () {
{
assert_mjs.assert.isInstance(event, FetchEvent, {
moduleName: 'workbox-strategies',
className: 'CacheFirst',
funcName: 'handle',
paramName: 'event'
});
}
return _this.makeRequest({
event,
request: event.request
});
})();
async handle({
event,
request
}) {
return this.makeRequest({
event,
request: request || event.request
});
}
/**

@@ -138,72 +119,76 @@ * This method can be used to perform a make a standalone request outside the

*/
makeRequest({ event, request }) {
var _this2 = this;
return babelHelpers.asyncToGenerator(function* () {
const logs = [];
if (typeof request === 'string') {
request = new Request(request);
}
async makeRequest({
event,
request
}) {
const logs = [];
{
assert_mjs.assert.isInstance(request, Request, {
moduleName: 'workbox-strategies',
className: 'CacheFirst',
funcName: 'makeRequest',
paramName: 'request'
});
}
if (typeof request === 'string') {
request = new Request(request);
}
let response = yield cacheWrapper_mjs.cacheWrapper.match({
cacheName: _this2._cacheName,
request,
event,
matchOptions: _this2._matchOptions,
plugins: _this2._plugins
{
assert_mjs.assert.isInstance(request, Request, {
moduleName: 'workbox-strategies',
className: 'CacheFirst',
funcName: 'makeRequest',
paramName: 'request'
});
}
let error;
if (!response) {
{
logs.push(`No response found in the '${_this2._cacheName}' cache. ` + `Will respond with a network request.`);
}
try {
response = yield _this2._getFromNetwork(request, event);
} catch (err) {
error = err;
}
let response = await cacheWrapper_mjs.cacheWrapper.match({
cacheName: this._cacheName,
request,
event,
matchOptions: this._matchOptions,
plugins: this._plugins
});
let error;
{
if (response) {
logs.push(`Got response from network.`);
} else {
logs.push(`Unable to get a response from the network.`);
}
}
} else {
{
logs.push(`Found a cached response in the '${_this2._cacheName}' cache.`);
}
if (!response) {
{
logs.push(`No response found in the '${this._cacheName}' cache. ` + `Will respond with a network request.`);
}
try {
response = await this._getFromNetwork(request, event);
} catch (err) {
error = err;
}
{
logger_mjs.logger.groupCollapsed(messages.strategyStart('CacheFirst', request));
for (let log of logs) {
logger_mjs.logger.log(log);
if (response) {
logs.push(`Got response from network.`);
} else {
logs.push(`Unable to get a response from the network.`);
}
messages.printFinalResponse(response);
logger_mjs.logger.groupEnd();
}
} else {
{
logs.push(`Found a cached response in the '${this._cacheName}' cache.`);
}
}
if (error) {
// Don't swallow error as we'll want it to throw and enable catch
// handlers in router.
throw error;
{
logger_mjs.logger.groupCollapsed(messages.strategyStart('CacheFirst', request));
for (let log of logs) {
logger_mjs.logger.log(log);
}
return response;
})();
messages.printFinalResponse(response);
logger_mjs.logger.groupEnd();
}
if (!response) {
throw new WorkboxError_mjs.WorkboxError('no-response', {
url: request.url,
error
});
}
return response;
}
/**

@@ -218,53 +203,43 @@ * Handles the network and cache part of CacheFirst.

*/
_getFromNetwork(request, event) {
var _this3 = this;
return babelHelpers.asyncToGenerator(function* () {
const response = yield fetchWrapper_mjs.fetchWrapper.fetch({
request,
event,
fetchOptions: _this3._fetchOptions,
plugins: _this3._plugins
});
// Keep the service worker while we put the request to the cache
const responseClone = response.clone();
const cachePutPromise = cacheWrapper_mjs.cacheWrapper.put({
cacheName: _this3._cacheName,
request,
response: responseClone,
event,
plugins: _this3._plugins
});
async _getFromNetwork(request, event) {
const response = await fetchWrapper_mjs.fetchWrapper.fetch({
request,
event,
fetchOptions: this._fetchOptions,
plugins: this._plugins
}); // Keep the service worker while we put the request to the cache
if (event) {
try {
event.waitUntil(cachePutPromise);
} catch (error) {
{
logger_mjs.logger.warn(`Unable to ensure service worker stays alive when ` + `updating cache for '${getFriendlyURL_mjs.getFriendlyURL(event.request.url)}'.`);
}
const responseClone = response.clone();
const cachePutPromise = cacheWrapper_mjs.cacheWrapper.put({
cacheName: this._cacheName,
request,
response: responseClone,
event,
plugins: this._plugins
});
if (event) {
try {
event.waitUntil(cachePutPromise);
} catch (error) {
{
logger_mjs.logger.warn(`Unable to ensure service worker stays alive when ` + `updating cache for '${getFriendlyURL_mjs.getFriendlyURL(event.request.url)}'.`);
}
}
}
return response;
})();
return response;
}
}
/*
Copyright 2018 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Copyright 2018 Google LLC
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**

@@ -275,6 +250,10 @@ * An implementation of a

*
* This class is useful if you want to take advantage of any [Workbox plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}.
* This class is useful if you want to take advantage of any
* [Workbox plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}.
*
* If there is no cache match, this will throw a `WorkboxError` exception.
*
* @memberof workbox.strategies
*/
class CacheOnly {

@@ -295,3 +274,2 @@ /**

}
/**

@@ -303,26 +281,17 @@ * This method will perform a request strategy and follows an API that

* @param {Object} options
* @param {FetchEvent} options.event The fetch event to run this strategy
* against.
* @param {Request} options.request The request to run this strategy for.
* @param {Event} [options.event] The event that triggered the request.
* @return {Promise<Response>}
*/
handle({ event }) {
var _this = this;
return babelHelpers.asyncToGenerator(function* () {
{
assert_mjs.assert.isInstance(event, FetchEvent, {
moduleName: 'workbox-strategies',
className: 'CacheOnly',
funcName: 'handle',
paramName: 'event'
});
}
return _this.makeRequest({
event,
request: event.request
});
})();
async handle({
event,
request
}) {
return this.makeRequest({
event,
request: request || event.request
});
}
/**

@@ -343,58 +312,60 @@ * This method can be used to perform a make a standalone request outside the

*/
makeRequest({ event, request }) {
var _this2 = this;
return babelHelpers.asyncToGenerator(function* () {
if (typeof request === 'string') {
request = new Request(request);
}
{
assert_mjs.assert.isInstance(request, Request, {
moduleName: 'workbox-strategies',
className: 'CacheOnly',
funcName: 'makeRequest',
paramName: 'request'
});
}
async makeRequest({
event,
request
}) {
if (typeof request === 'string') {
request = new Request(request);
}
const response = yield cacheWrapper_mjs.cacheWrapper.match({
cacheName: _this2._cacheName,
request,
event,
matchOptions: _this2._matchOptions,
plugins: _this2._plugins
{
assert_mjs.assert.isInstance(request, Request, {
moduleName: 'workbox-strategies',
className: 'CacheOnly',
funcName: 'makeRequest',
paramName: 'request'
});
}
{
logger_mjs.logger.groupCollapsed(messages.strategyStart('CacheOnly', request));
if (response) {
logger_mjs.logger.log(`Found a cached response in the '${_this2._cacheName}'` + ` cache.`);
messages.printFinalResponse(response);
} else {
logger_mjs.logger.log(`No response found in the '${_this2._cacheName}' cache.`);
}
logger_mjs.logger.groupEnd();
const response = await cacheWrapper_mjs.cacheWrapper.match({
cacheName: this._cacheName,
request,
event,
matchOptions: this._matchOptions,
plugins: this._plugins
});
{
logger_mjs.logger.groupCollapsed(messages.strategyStart('CacheOnly', request));
if (response) {
logger_mjs.logger.log(`Found a cached response in the '${this._cacheName}'` + ` cache.`);
messages.printFinalResponse(response);
} else {
logger_mjs.logger.log(`No response found in the '${this._cacheName}' cache.`);
}
return response;
})();
logger_mjs.logger.groupEnd();
}
if (!response) {
throw new WorkboxError_mjs.WorkboxError('no-response', {
url: request.url
});
}
return response;
}
}
/*
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Copyright 2018 Google LLC
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
var cacheOkAndOpaquePlugin = {

@@ -411,6 +382,9 @@ /**

*/
cacheWillUpdate: ({ response }) => {
cacheWillUpdate: ({
response
}) => {
if (response.ok || response.status === 0) {
return response;
}
return null;

@@ -421,16 +395,8 @@ }

/*
Copyright 2018 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Copyright 2018 Google LLC
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**

@@ -446,4 +412,8 @@ * An implementation of a

*
* If the network request fails, and there is no cache match, this will throw
* a `WorkboxError` exception.
*
* @memberof workbox.strategies
*/
class NetworkFirst {

@@ -480,2 +450,3 @@ /**

this._networkTimeoutSeconds = options.networkTimeoutSeconds;
{

@@ -495,3 +466,2 @@ if (this._networkTimeoutSeconds) {

}
/**

@@ -503,26 +473,17 @@ * This method will perform a request strategy and follows an API that

* @param {Object} options
* @param {FetchEvent} options.event The fetch event to run this strategy
* against.
* @param {Request} options.request The request to run this strategy for.
* @param {Event} [options.event] The event that triggered the request.
* @return {Promise<Response>}
*/
handle({ event }) {
var _this = this;
return babelHelpers.asyncToGenerator(function* () {
{
assert_mjs.assert.isInstance(event, FetchEvent, {
moduleName: 'workbox-strategies',
className: 'NetworkFirst',
funcName: 'handle',
paramName: 'event'
});
}
return _this.makeRequest({
event,
request: event.request
});
})();
async handle({
event,
request
}) {
return this.makeRequest({
event,
request: request || event.request
});
}
/**

@@ -543,57 +504,78 @@ * This method can be used to perform a make a standalone request outside the

*/
makeRequest({ event, request }) {
var _this2 = this;
return babelHelpers.asyncToGenerator(function* () {
const logs = [];
if (typeof request === 'string') {
request = new Request(request);
}
async makeRequest({
event,
request
}) {
const logs = [];
{
assert_mjs.assert.isInstance(request, Request, {
moduleName: 'workbox-strategies',
className: 'NetworkFirst',
funcName: 'handle',
paramName: 'makeRequest'
});
}
if (typeof request === 'string') {
request = new Request(request);
}
const promises = [];
let timeoutId;
{
assert_mjs.assert.isInstance(request, Request, {
moduleName: 'workbox-strategies',
className: 'NetworkFirst',
funcName: 'handle',
paramName: 'makeRequest'
});
}
if (_this2._networkTimeoutSeconds) {
const { id, promise } = _this2._getTimeoutPromise({ request, event, logs });
timeoutId = id;
promises.push(promise);
}
const promises = [];
let timeoutId;
const networkPromise = _this2._getNetworkPromise({ timeoutId, request, event, logs });
promises.push(networkPromise);
if (this._networkTimeoutSeconds) {
const {
id,
promise
} = this._getTimeoutPromise({
request,
event,
logs
});
// Promise.race() will resolve as soon as the first promise resolves.
let response = yield Promise.race(promises);
// If Promise.race() resolved with null, it might be due to a network
// timeout + a cache miss. If that were to happen, we'd rather wait until
// the networkPromise resolves instead of returning null.
// Note that it's fine to await an already-resolved promise, so we don't
// have to check to see if it's still "in flight".
if (!response) {
response = yield networkPromise;
}
timeoutId = id;
promises.push(promise);
}
{
logger_mjs.logger.groupCollapsed(messages.strategyStart('NetworkFirst', request));
for (let log of logs) {
logger_mjs.logger.log(log);
}
messages.printFinalResponse(response);
logger_mjs.logger.groupEnd();
const networkPromise = this._getNetworkPromise({
timeoutId,
request,
event,
logs
});
promises.push(networkPromise); // Promise.race() will resolve as soon as the first promise resolves.
let response = await Promise.race(promises); // If Promise.race() resolved with null, it might be due to a network
// timeout + a cache miss. If that were to happen, we'd rather wait until
// the networkPromise resolves instead of returning null.
// Note that it's fine to await an already-resolved promise, so we don't
// have to check to see if it's still "in flight".
if (!response) {
response = await networkPromise;
}
{
logger_mjs.logger.groupCollapsed(messages.strategyStart('NetworkFirst', request));
for (let log of logs) {
logger_mjs.logger.log(log);
}
return response;
})();
messages.printFinalResponse(response);
logger_mjs.logger.groupEnd();
}
if (!response) {
throw new WorkboxError_mjs.WorkboxError('no-response', {
url: request.url
});
}
return response;
}
/**

@@ -608,24 +590,24 @@ * @param {Object} options

*/
_getTimeoutPromise({ request, logs, event }) {
var _this3 = this;
_getTimeoutPromise({
request,
logs,
event
}) {
let timeoutId;
const timeoutPromise = new Promise(resolve => {
const onNetworkTimeout = (() => {
var _ref = babelHelpers.asyncToGenerator(function* () {
{
logs.push(`Timing out the network response at ` + `${_this3._networkTimeoutSeconds} seconds.`);
}
const onNetworkTimeout = async () => {
{
logs.push(`Timing out the network response at ` + `${this._networkTimeoutSeconds} seconds.`);
}
resolve((yield _this3._respondFromCache({ request, event })));
});
resolve((await this._respondFromCache({
request,
event
})));
};
return function onNetworkTimeout() {
return _ref.apply(this, arguments);
};
})();
timeoutId = setTimeout(onNetworkTimeout, this._networkTimeoutSeconds * 1000);
});
return {

@@ -636,3 +618,2 @@ promise: timeoutPromise,

}
/**

@@ -648,68 +629,75 @@ * @param {Object} options

*/
_getNetworkPromise({ timeoutId, request, logs, event }) {
var _this4 = this;
return babelHelpers.asyncToGenerator(function* () {
let error;
let response;
try {
response = yield fetchWrapper_mjs.fetchWrapper.fetch({
request,
event,
fetchOptions: _this4._fetchOptions,
plugins: _this4._plugins
});
} catch (err) {
error = err;
}
if (timeoutId) {
clearTimeout(timeoutId);
async _getNetworkPromise({
timeoutId,
request,
logs,
event
}) {
let error;
let response;
try {
response = await fetchWrapper_mjs.fetchWrapper.fetch({
request,
event,
fetchOptions: this._fetchOptions,
plugins: this._plugins
});
} catch (err) {
error = err;
}
if (timeoutId) {
clearTimeout(timeoutId);
}
{
if (response) {
logs.push(`Got response from network.`);
} else {
logs.push(`Unable to get a response from the network. Will respond ` + `with a cached response.`);
}
}
if (error || !response) {
response = await this._respondFromCache({
request,
event
});
{
if (response) {
logs.push(`Got response from network.`);
logs.push(`Found a cached response in the '${this._cacheName}'` + ` cache.`);
} else {
logs.push(`Unable to get a response from the network. Will respond ` + `with a cached response.`);
logs.push(`No response found in the '${this._cacheName}' cache.`);
}
}
} else {
// Keep the service worker alive while we put the request in the cache
const responseClone = response.clone();
const cachePut = cacheWrapper_mjs.cacheWrapper.put({
cacheName: this._cacheName,
request,
response: responseClone,
event,
plugins: this._plugins
});
if (error || !response) {
response = yield _this4._respondFromCache({ request, event });
{
if (response) {
logs.push(`Found a cached response in the '${_this4._cacheName}'` + ` cache.`);
} else {
logs.push(`No response found in the '${_this4._cacheName}' cache.`);
if (event) {
try {
// The event has been responded to so we can keep the SW alive to
// respond to the request
event.waitUntil(cachePut);
} catch (err) {
{
logger_mjs.logger.warn(`Unable to ensure service worker stays alive when ` + `updating cache for '${getFriendlyURL_mjs.getFriendlyURL(event.request.url)}'.`);
}
}
} else {
// Keep the service worker alive while we put the request in the cache
const responseClone = response.clone();
const cachePut = cacheWrapper_mjs.cacheWrapper.put({
cacheName: _this4._cacheName,
request,
response: responseClone,
event,
plugins: _this4._plugins
});
if (event) {
try {
// The event has been responded to so we can keep the SW alive to
// respond to the request
event.waitUntil(cachePut);
} catch (err) {
{
logger_mjs.logger.warn(`Unable to ensure service worker stays alive when ` + `updating cache for '${getFriendlyURL_mjs.getFriendlyURL(event.request.url)}'.`);
}
}
}
}
}
return response;
})();
return response;
}
/**

@@ -725,3 +713,8 @@ * Used if the network timeouts or fails to make the request.

*/
_respondFromCache({ event, request }) {
_respondFromCache({
event,
request
}) {
return cacheWrapper_mjs.cacheWrapper.match({

@@ -735,19 +728,12 @@ cacheName: this._cacheName,

}
}
/*
Copyright 2018 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Copyright 2018 Google LLC
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**

@@ -758,6 +744,10 @@ * An implementation of a

*
* This class is useful if you want to take advantage of any [Workbox plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}.
* This class is useful if you want to take advantage of any
* [Workbox plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}.
*
* If the network request fails, this will throw a `WorkboxError` exception.
*
* @memberof workbox.strategies
*/
class NetworkOnly {

@@ -780,3 +770,2 @@ /**

}
/**

@@ -788,26 +777,17 @@ * This method will perform a request strategy and follows an API that

* @param {Object} options
* @param {FetchEvent} options.event The fetch event to run this strategy
* against.
* @param {Request} options.request The request to run this strategy for.
* @param {Event} [options.event] The event that triggered the request.
* @return {Promise<Response>}
*/
handle({ event }) {
var _this = this;
return babelHelpers.asyncToGenerator(function* () {
{
assert_mjs.assert.isInstance(event, FetchEvent, {
moduleName: 'workbox-strategies',
className: 'NetworkOnly',
funcName: 'handle',
paramName: 'event'
});
}
return _this.makeRequest({
event,
request: event.request
});
})();
async handle({
event,
request
}) {
return this.makeRequest({
event,
request: request || event.request
});
}
/**

@@ -828,69 +808,67 @@ * This method can be used to perform a make a standalone request outside the

*/
makeRequest({ event, request }) {
var _this2 = this;
return babelHelpers.asyncToGenerator(function* () {
if (typeof request === 'string') {
request = new Request(request);
}
{
assert_mjs.assert.isInstance(request, Request, {
moduleName: 'workbox-strategies',
className: 'NetworkOnly',
funcName: 'handle',
paramName: 'request'
});
}
async makeRequest({
event,
request
}) {
if (typeof request === 'string') {
request = new Request(request);
}
let error;
let response;
try {
response = yield fetchWrapper_mjs.fetchWrapper.fetch({
request,
event,
fetchOptions: _this2._fetchOptions,
plugins: _this2._plugins
});
} catch (err) {
error = err;
}
{
assert_mjs.assert.isInstance(request, Request, {
moduleName: 'workbox-strategies',
className: 'NetworkOnly',
funcName: 'handle',
paramName: 'request'
});
}
{
logger_mjs.logger.groupCollapsed(messages.strategyStart('NetworkOnly', request));
if (response) {
logger_mjs.logger.log(`Got response from network.`);
} else {
logger_mjs.logger.log(`Unable to get a response from the network.`);
}
messages.printFinalResponse(response);
logger_mjs.logger.groupEnd();
}
let error;
let response;
// If there was an error thrown, re-throw it to ensure the Routers
// catch handler is triggered.
if (error) {
throw error;
try {
response = await fetchWrapper_mjs.fetchWrapper.fetch({
request,
event,
fetchOptions: this._fetchOptions,
plugins: this._plugins
});
} catch (err) {
error = err;
}
{
logger_mjs.logger.groupCollapsed(messages.strategyStart('NetworkOnly', request));
if (response) {
logger_mjs.logger.log(`Got response from network.`);
} else {
logger_mjs.logger.log(`Unable to get a response from the network.`);
}
return response;
})();
messages.printFinalResponse(response);
logger_mjs.logger.groupEnd();
}
if (!response) {
throw new WorkboxError_mjs.WorkboxError('no-response', {
url: request.url,
error
});
}
return response;
}
}
/*
Copyright 2018 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Copyright 2018 Google LLC
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**

@@ -911,4 +889,8 @@ * An implementation of a

*
* If the network request fails, and there is no cache match, this will throw
* a `WorkboxError` exception.
*
* @memberof workbox.strategies
*/
class StaleWhileRevalidate {

@@ -942,3 +924,2 @@ /**

}
/**

@@ -950,26 +931,17 @@ * This method will perform a request strategy and follows an API that

* @param {Object} options
* @param {FetchEvent} options.event The fetch event to run this strategy
* against.
* @param {Request} options.request The request to run this strategy for.
* @param {Event} [options.event] The event that triggered the request.
* @return {Promise<Response>}
*/
handle({ event }) {
var _this = this;
return babelHelpers.asyncToGenerator(function* () {
{
assert_mjs.assert.isInstance(event, FetchEvent, {
moduleName: 'workbox-strategies',
className: 'StaleWhileRevalidate',
funcName: 'handle',
paramName: 'event'
});
}
return _this.makeRequest({
event,
request: event.request
});
})();
async handle({
event,
request
}) {
return this.makeRequest({
event,
request: request || event.request
});
}
/**

@@ -990,65 +962,83 @@ * This method can be used to perform a make a standalone request outside the

*/
makeRequest({ event, request }) {
var _this2 = this;
return babelHelpers.asyncToGenerator(function* () {
const logs = [];
if (typeof request === 'string') {
request = new Request(request);
}
async makeRequest({
event,
request
}) {
const logs = [];
{
assert_mjs.assert.isInstance(request, Request, {
moduleName: 'workbox-strategies',
className: 'StaleWhileRevalidate',
funcName: 'handle',
paramName: 'request'
});
}
if (typeof request === 'string') {
request = new Request(request);
}
const fetchAndCachePromise = _this2._getFromNetwork({ request, event });
let response = yield cacheWrapper_mjs.cacheWrapper.match({
cacheName: _this2._cacheName,
request,
event,
matchOptions: _this2._matchOptions,
plugins: _this2._plugins
{
assert_mjs.assert.isInstance(request, Request, {
moduleName: 'workbox-strategies',
className: 'StaleWhileRevalidate',
funcName: 'handle',
paramName: 'request'
});
}
if (response) {
{
logs.push(`Found a cached response in the '${_this2._cacheName}'` + ` cache. Will update with the network response in the background.`);
}
const fetchAndCachePromise = this._getFromNetwork({
request,
event
});
if (event) {
try {
event.waitUntil(fetchAndCachePromise);
} catch (error) {
{
logger_mjs.logger.warn(`Unable to ensure service worker stays alive when ` + `updating cache for '${getFriendlyURL_mjs.getFriendlyURL(event.request.url)}'.`);
}
let response = await cacheWrapper_mjs.cacheWrapper.match({
cacheName: this._cacheName,
request,
event,
matchOptions: this._matchOptions,
plugins: this._plugins
});
let error;
if (response) {
{
logs.push(`Found a cached response in the '${this._cacheName}'` + ` cache. Will update with the network response in the background.`);
}
if (event) {
try {
event.waitUntil(fetchAndCachePromise);
} catch (error) {
{
logger_mjs.logger.warn(`Unable to ensure service worker stays alive when ` + `updating cache for '${getFriendlyURL_mjs.getFriendlyURL(event.request.url)}'.`);
}
}
} else {
{
logs.push(`No response found in the '${_this2._cacheName}' cache. ` + `Will wait for the network response.`);
}
response = yield fetchAndCachePromise;
}
} else {
{
logger_mjs.logger.groupCollapsed(messages.strategyStart('StaleWhileRevalidate', request));
for (let log of logs) {
logger_mjs.logger.log(log);
}
messages.printFinalResponse(response);
logger_mjs.logger.groupEnd();
logs.push(`No response found in the '${this._cacheName}' cache. ` + `Will wait for the network response.`);
}
return response;
})();
try {
response = await fetchAndCachePromise;
} catch (err) {
error = err;
}
}
{
logger_mjs.logger.groupCollapsed(messages.strategyStart('StaleWhileRevalidate', request));
for (let log of logs) {
logger_mjs.logger.log(log);
}
messages.printFinalResponse(response);
logger_mjs.logger.groupEnd();
}
if (!response) {
throw new WorkboxError_mjs.WorkboxError('no-response', {
url: request.url,
error
});
}
return response;
}
/**

@@ -1062,50 +1052,43 @@ * @param {Object} options

*/
_getFromNetwork({ request, event }) {
var _this3 = this;
return babelHelpers.asyncToGenerator(function* () {
const response = yield fetchWrapper_mjs.fetchWrapper.fetch({
request,
event,
fetchOptions: _this3._fetchOptions,
plugins: _this3._plugins
});
const cachePutPromise = cacheWrapper_mjs.cacheWrapper.put({
cacheName: _this3._cacheName,
request,
response: response.clone(),
event,
plugins: _this3._plugins
});
async _getFromNetwork({
request,
event
}) {
const response = await fetchWrapper_mjs.fetchWrapper.fetch({
request,
event,
fetchOptions: this._fetchOptions,
plugins: this._plugins
});
const cachePutPromise = cacheWrapper_mjs.cacheWrapper.put({
cacheName: this._cacheName,
request,
response: response.clone(),
event,
plugins: this._plugins
});
if (event) {
try {
event.waitUntil(cachePutPromise);
} catch (error) {
{
logger_mjs.logger.warn(`Unable to ensure service worker stays alive when ` + `updating cache for '${getFriendlyURL_mjs.getFriendlyURL(event.request.url)}'.`);
}
if (event) {
try {
event.waitUntil(cachePutPromise);
} catch (error) {
{
logger_mjs.logger.warn(`Unable to ensure service worker stays alive when ` + `updating cache for '${getFriendlyURL_mjs.getFriendlyURL(event.request.url)}'.`);
}
}
}
return response;
})();
return response;
}
}
/*
Copyright 2017 Google Inc.
Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/

@@ -1122,16 +1105,8 @@

/*
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Copyright 2018 Google LLC
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
/**

@@ -1174,3 +1149,2 @@ * @function workbox.strategies.cacheFirst

};
const defaultExport = {};

@@ -1185,17 +1159,8 @@ Object.keys(mapping).forEach(keyName => {

/*
Copyright 2017 Google Inc.
Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
const finalExport = Object.assign(defaultExport, publicAPI);

@@ -1205,4 +1170,4 @@

}(workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private));
}(workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private));
//# sourceMappingURL=workbox-strategies.dev.js.map

@@ -1,3 +0,3 @@

this.workbox=this.workbox||{},this.workbox.strategies=function(e,t,s){"use strict";try{self.workbox.v["workbox:strategies:3.6.2"]=1}catch(e){}class r{constructor(t={}){this.e=e.cacheNames.getRuntimeName(t.cacheName),this.t=t.plugins||[],this.s=t.fetchOptions||null,this.r=t.matchOptions||null}handle({event:e}){var t=this;return babelHelpers.asyncToGenerator(function*(){return t.makeRequest({event:e,request:e.request})})()}makeRequest({event:e,request:s}){var r=this;return babelHelpers.asyncToGenerator(function*(){"string"==typeof s&&(s=new Request(s));let n,i=yield t.cacheWrapper.match({cacheName:r.e,request:s,event:e,matchOptions:r.r,plugins:r.t});if(!i)try{i=yield r.n(s,e)}catch(e){n=e}if(n)throw n;return i})()}n(e,r){var n=this;return babelHelpers.asyncToGenerator(function*(){const i=yield s.fetchWrapper.fetch({request:e,event:r,fetchOptions:n.s,plugins:n.t}),u=i.clone(),l=t.cacheWrapper.put({cacheName:n.e,request:e,response:u,event:r,plugins:n.t});if(r)try{r.waitUntil(l)}catch(e){}return i})()}}class n{constructor(t={}){this.e=e.cacheNames.getRuntimeName(t.cacheName),this.t=t.plugins||[],this.r=t.matchOptions||null}handle({event:e}){var t=this;return babelHelpers.asyncToGenerator(function*(){return t.makeRequest({event:e,request:e.request})})()}makeRequest({event:e,request:s}){var r=this;return babelHelpers.asyncToGenerator(function*(){return"string"==typeof s&&(s=new Request(s)),yield t.cacheWrapper.match({cacheName:r.e,request:s,event:e,matchOptions:r.r,plugins:r.t})})()}}var i={cacheWillUpdate:({response:e})=>e.ok||0===e.status?e:null};class u{constructor(t={}){if(this.e=e.cacheNames.getRuntimeName(t.cacheName),t.plugins){let e=t.plugins.some(e=>!!e.cacheWillUpdate);this.t=e?t.plugins:[i,...t.plugins]}else this.t=[i];this.i=t.networkTimeoutSeconds,this.s=t.fetchOptions||null,this.r=t.matchOptions||null}handle({event:e}){var t=this;return babelHelpers.asyncToGenerator(function*(){return t.makeRequest({event:e,request:e.request})})()}makeRequest({event:e,request:t}){var s=this;return babelHelpers.asyncToGenerator(function*(){const r=[];"string"==typeof t&&(t=new Request(t));const n=[];let i;if(s.i){const{id:u,promise:l}=s.u({request:t,event:e,logs:r});i=u,n.push(l)}const u=s.l({timeoutId:i,request:t,event:e,logs:r});n.push(u);let l=yield Promise.race(n);return l||(l=yield u),l})()}u({request:e,logs:t,event:s}){var r=this;let n;var i;return{promise:new Promise(t=>{const u=(i=babelHelpers.asyncToGenerator(function*(){t(yield r.c({request:e,event:s}))}),function(){return i.apply(this,arguments)});n=setTimeout(u,1e3*this.i)}),id:n}}l({timeoutId:e,request:r,logs:n,event:i}){var u=this;return babelHelpers.asyncToGenerator(function*(){let n,l;try{l=yield s.fetchWrapper.fetch({request:r,event:i,fetchOptions:u.s,plugins:u.t})}catch(e){n=e}if(e&&clearTimeout(e),n||!l)l=yield u.c({request:r,event:i});else{const e=l.clone(),s=t.cacheWrapper.put({cacheName:u.e,request:r,response:e,event:i,plugins:u.t});if(i)try{i.waitUntil(s)}catch(e){}}return l})()}c({event:e,request:s}){return t.cacheWrapper.match({cacheName:this.e,request:s,event:e,matchOptions:this.r,plugins:this.t})}}class l{constructor(t={}){this.e=e.cacheNames.getRuntimeName(t.cacheName),this.t=t.plugins||[],this.s=t.fetchOptions||null}handle({event:e}){var t=this;return babelHelpers.asyncToGenerator(function*(){return t.makeRequest({event:e,request:e.request})})()}makeRequest({event:e,request:t}){var r=this;return babelHelpers.asyncToGenerator(function*(){let n,i;"string"==typeof t&&(t=new Request(t));try{i=yield s.fetchWrapper.fetch({request:t,event:e,fetchOptions:r.s,plugins:r.t})}catch(e){n=e}if(n)throw n;return i})()}}class c{constructor(t={}){if(this.e=e.cacheNames.getRuntimeName(t.cacheName),this.t=t.plugins||[],t.plugins){let e=t.plugins.some(e=>!!e.cacheWillUpdate);this.t=e?t.plugins:[i,...t.plugins]}else this.t=[i];this.s=t.fetchOptions||null,this.r=t.matchOptions||null}handle({event:e}){var t=this;return babelHelpers.asyncToGenerator(function*(){return t.makeRequest({event:e,request:e.request})})()}makeRequest({event:e,request:s}){var r=this;return babelHelpers.asyncToGenerator(function*(){"string"==typeof s&&(s=new Request(s));const n=r.n({request:s,event:e});let i=yield t.cacheWrapper.match({cacheName:r.e,request:s,event:e,matchOptions:r.r,plugins:r.t});if(i){if(e)try{e.waitUntil(n)}catch(e){}}else i=yield n;return i})()}n({request:e,event:r}){var n=this;return babelHelpers.asyncToGenerator(function*(){const i=yield s.fetchWrapper.fetch({request:e,event:r,fetchOptions:n.s,plugins:n.t}),u=t.cacheWrapper.put({cacheName:n.e,request:e,response:i.clone(),event:r,plugins:n.t});if(r)try{r.waitUntil(u)}catch(e){}return i})()}}var o=Object.freeze({CacheFirst:r,CacheOnly:n,NetworkFirst:u,NetworkOnly:l,StaleWhileRevalidate:c});const a={cacheFirst:r,cacheOnly:n,networkFirst:u,networkOnly:l,staleWhileRevalidate:c},h={};return Object.keys(a).forEach(e=>{h[e]=((t={})=>{return new(0,a[e])(Object.assign(t))})}),Object.assign(h,o)}(workbox.core._private,workbox.core._private,workbox.core._private);
this.workbox=this.workbox||{},this.workbox.strategies=function(e,t,s,n){"use strict";try{self.workbox.v["workbox:strategies:4.0.0-alpha.0"]=1}catch(e){}class r{constructor(t={}){this.e=e.cacheNames.getRuntimeName(t.cacheName),this.t=t.plugins||[],this.s=t.fetchOptions||null,this.n=t.matchOptions||null}async handle({event:e,request:t}){return this.makeRequest({event:e,request:t||e.request})}async makeRequest({event:e,request:s}){"string"==typeof s&&(s=new Request(s));let r,i=await t.cacheWrapper.match({cacheName:this.e,request:s,event:e,matchOptions:this.n,plugins:this.t});if(!i)try{i=await this.r(s,e)}catch(e){r=e}if(!i)throw new n.WorkboxError("no-response",{url:s.url,error:r});return i}async r(e,n){const r=await s.fetchWrapper.fetch({request:e,event:n,fetchOptions:this.s,plugins:this.t}),i=r.clone(),h=t.cacheWrapper.put({cacheName:this.e,request:e,response:i,event:n,plugins:this.t});if(n)try{n.waitUntil(h)}catch(e){}return r}}class i{constructor(t={}){this.e=e.cacheNames.getRuntimeName(t.cacheName),this.t=t.plugins||[],this.n=t.matchOptions||null}async handle({event:e,request:t}){return this.makeRequest({event:e,request:t||e.request})}async makeRequest({event:e,request:s}){"string"==typeof s&&(s=new Request(s));const r=await t.cacheWrapper.match({cacheName:this.e,request:s,event:e,matchOptions:this.n,plugins:this.t});if(!r)throw new n.WorkboxError("no-response",{url:s.url});return r}}var h={cacheWillUpdate:({response:e})=>e.ok||0===e.status?e:null};class u{constructor(t={}){if(this.e=e.cacheNames.getRuntimeName(t.cacheName),t.plugins){let e=t.plugins.some(e=>!!e.cacheWillUpdate);this.t=e?t.plugins:[h,...t.plugins]}else this.t=[h];this.i=t.networkTimeoutSeconds,this.s=t.fetchOptions||null,this.n=t.matchOptions||null}async handle({event:e,request:t}){return this.makeRequest({event:e,request:t||e.request})}async makeRequest({event:e,request:t}){const s=[];"string"==typeof t&&(t=new Request(t));const r=[];let i;if(this.i){const{id:n,promise:h}=this.h({request:t,event:e,logs:s});i=n,r.push(h)}const h=this.u({timeoutId:i,request:t,event:e,logs:s});r.push(h);let u=await Promise.race(r);if(u||(u=await h),!u)throw new n.WorkboxError("no-response",{url:t.url});return u}h({request:e,logs:t,event:s}){let n;return{promise:new Promise(t=>{n=setTimeout(async()=>{t(await this.a({request:e,event:s}))},1e3*this.i)}),id:n}}async u({timeoutId:e,request:n,logs:r,event:i}){let h,u;try{u=await s.fetchWrapper.fetch({request:n,event:i,fetchOptions:this.s,plugins:this.t})}catch(e){h=e}if(e&&clearTimeout(e),h||!u)u=await this.a({request:n,event:i});else{const e=u.clone(),s=t.cacheWrapper.put({cacheName:this.e,request:n,response:e,event:i,plugins:this.t});if(i)try{i.waitUntil(s)}catch(e){}}return u}a({event:e,request:s}){return t.cacheWrapper.match({cacheName:this.e,request:s,event:e,matchOptions:this.n,plugins:this.t})}}class a{constructor(t={}){this.e=e.cacheNames.getRuntimeName(t.cacheName),this.t=t.plugins||[],this.s=t.fetchOptions||null}async handle({event:e,request:t}){return this.makeRequest({event:e,request:t||e.request})}async makeRequest({event:e,request:t}){let r,i;"string"==typeof t&&(t=new Request(t));try{i=await s.fetchWrapper.fetch({request:t,event:e,fetchOptions:this.s,plugins:this.t})}catch(e){r=e}if(!i)throw new n.WorkboxError("no-response",{url:t.url,error:r});return i}}class c{constructor(t={}){if(this.e=e.cacheNames.getRuntimeName(t.cacheName),this.t=t.plugins||[],t.plugins){let e=t.plugins.some(e=>!!e.cacheWillUpdate);this.t=e?t.plugins:[h,...t.plugins]}else this.t=[h];this.s=t.fetchOptions||null,this.n=t.matchOptions||null}async handle({event:e,request:t}){return this.makeRequest({event:e,request:t||e.request})}async makeRequest({event:e,request:s}){"string"==typeof s&&(s=new Request(s));const r=this.r({request:s,event:e});let i,h=await t.cacheWrapper.match({cacheName:this.e,request:s,event:e,matchOptions:this.n,plugins:this.t});if(h){if(e)try{e.waitUntil(r)}catch(i){}}else try{h=await r}catch(e){i=e}if(!h)throw new n.WorkboxError("no-response",{url:s.url,error:i});return h}async r({request:e,event:n}){const r=await s.fetchWrapper.fetch({request:e,event:n,fetchOptions:this.s,plugins:this.t}),i=t.cacheWrapper.put({cacheName:this.e,request:e,response:r.clone(),event:n,plugins:this.t});if(n)try{n.waitUntil(i)}catch(e){}return r}}var o=Object.freeze({CacheFirst:r,CacheOnly:i,NetworkFirst:u,NetworkOnly:a,StaleWhileRevalidate:c});const l={cacheFirst:r,cacheOnly:i,networkFirst:u,networkOnly:a,staleWhileRevalidate:c},q={};return Object.keys(l).forEach(e=>{q[e]=((t={})=>{return new(0,l[e])(Object.assign(t))})}),Object.assign(q,o)}(workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private);
//# sourceMappingURL=workbox-strategies.prod.js.map
{
"name": "workbox-strategies",
"version": "3.6.2",
"license": "Apache-2.0",
"version": "4.0.0-alpha.0",
"license": "MIT",
"author": "Google's Web DevRel Team",

@@ -30,4 +30,5 @@ "description": "A service worker helper library implementing common caching strategies.",

"dependencies": {
"workbox-core": "^3.6.2"
}
"workbox-core": "^4.0.0-alpha.0"
},
"gitHead": "db1fb73fd32fbd5cbf42e246e6144011a5c6edc2"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc