Socket
Socket
Sign inDemoInstall

workbox-routing

Package Overview
Dependencies
1
Maintainers
4
Versions
95
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.0.0-beta.0 to 4.0.0-beta.1

registerNavigationRoute.mjs

341

build/workbox-routing.dev.js
this.workbox = this.workbox || {};
this.workbox.routing = (function (assert_mjs,logger_mjs,WorkboxError_mjs,getFriendlyURL_mjs,cacheNames_mjs) {
this.workbox.routing = (function (exports,assert_mjs,logger_mjs,WorkboxError_mjs,getFriendlyURL_mjs,cacheNames_mjs) {
'use strict';
try {
self.workbox.v['workbox:routing:4.0.0-beta.0'] = 1;
self.workbox.v['workbox:routing:4.0.0-beta.1'] = 1;
} catch (e) {} // eslint-disable-line

@@ -51,3 +51,3 @@

var normalizeHandler = (handler => {
const normalizeHandler = handler => {
if (handler && typeof handler === 'object') {

@@ -78,3 +78,3 @@ {

}
});
};

@@ -514,3 +514,3 @@ /*

logger_mjs.logger.groupCollapsed(`View request details here.`);
logger_mjs.logger.unprefixed.log(request);
logger_mjs.logger.log(request);
logger_mjs.logger.groupEnd();

@@ -541,4 +541,4 @@ logger_mjs.logger.groupEnd();

logger_mjs.logger.groupCollapsed(`Error thrown when responding to: ` + ` ${getFriendlyURL_mjs.getFriendlyURL(url)}. Falling back to Catch Handler.`);
logger_mjs.logger.unprefixed.error(`Error thrown by:`, route);
logger_mjs.logger.unprefixed.error(err);
logger_mjs.logger.error(`Error thrown by:`, route);
logger_mjs.logger.error(err);
logger_mjs.logger.groupEnd();

@@ -724,3 +724,3 @@ }

/*
Copyright 2018 Google LLC
Copyright 2019 Google LLC

@@ -731,145 +731,74 @@ Use of this source code is governed by an MIT-style

*/
let defaultRouter;
/**
* The router class exposed on the routing namespace.
* A single instance of this class is exposed because most users will only
* need one router, and this version offers the most features and flexibility.
* Creates a new, singleton Router instance if one does not exist. If one
* does already exist, that instance is returned.
*
* @private
* @return {Router}
*/
class DefaultRouter extends Router {
/**
* The default router automatically adds fetch and cache message listeners.
*/
constructor() {
super();
this.addFetchListener();
this.addCacheListener();
const getOrCreateDefaultRouter = () => {
if (!defaultRouter) {
defaultRouter = new Router(); // The helpers that use the default Router assume these listeners exist.
defaultRouter.addFetchListener();
defaultRouter.addCacheListener();
}
/**
* Easily register a RegExp, string, or function with a caching
* strategy to the Router.
*
* This method will generate a Route for you if needed and
* call [Router.registerRoute()]{@link
* workbox.routing.Router#registerRoute}.
*
* @param {
* RegExp|
* string|
* workbox.routing.Route~matchCallback|
* workbox.routing.Route
* } capture
* If the capture param is a `Route`, all other arguments will be ignored.
* @param {workbox.routing.Route~handlerCallback} handler A callback
* function that returns a Promise resulting in a Response.
* @param {string} [method='GET'] The HTTP method to match the Route
* against.
* @return {workbox.routing.Route} The generated `Route`(Useful for
* unregistering).
*
* @alias workbox.routing.registerRoute
*/
return defaultRouter;
};
registerRoute(capture, handler, method = 'GET') {
let route;
/*
Copyright 2019 Google LLC
if (typeof capture === 'string') {
const captureUrl = new URL(capture, location);
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.
*/
/**
* Registers a route that will return a precached file for a navigation
* request. This is useful for the
* [application shell pattern]{@link https://developers.google.com/web/fundamentals/architecture/app-shell}.
*
* This method will generate a
* [NavigationRoute]{@link workbox.routing.NavigationRoute}
* and call
* [Router.registerRoute()]{@link workbox.routing.Router#registerRoute} on a
* singleton Router instance.
*
* @param {string} cachedAssetUrl
* @param {Object} [options]
* @param {string} [options.cacheName] Cache name to store and retrieve
* requests. Defaults to precache cache name provided by
* [workbox-core.cacheNames]{@link workbox.core.cacheNames}.
* @param {Array<RegExp>} [options.blacklist=[]] If any of these patterns
* match, the route will not handle the request (even if a whitelist entry
* matches).
* @param {Array<RegExp>} [options.whitelist=[/./]] If any of these patterns
* match the URL's pathname and search parameter, the route will handle the
* request (assuming the blacklist doesn't match).
* @return {workbox.routing.NavigationRoute} Returns the generated
* Route.
*
* @alias workbox.routing.registerNavigationRoute
*/
{
if (!(capture.startsWith('/') || capture.startsWith('http'))) {
throw new WorkboxError_mjs.WorkboxError('invalid-string', {
moduleName: 'workbox-routing',
className: 'DefaultRouter',
funcName: 'registerRoute',
paramName: 'capture'
});
} // We want to check if Express-style wildcards are in the pathname only.
// TODO: Remove this log message in v4.
const valueToCheck = capture.startsWith('http') ? captureUrl.pathname : capture; // See https://github.com/pillarjs/path-to-regexp#parameters
const wildcards = '[*:?+]';
if (valueToCheck.match(new RegExp(`${wildcards}`))) {
logger_mjs.logger.debug(`The '$capture' parameter contains an Express-style wildcard ` + `character (${wildcards}). Strings are now always interpreted as ` + `exact matches; use a RegExp for partial or wildcard matches.`);
}
}
const matchCallback = ({
url
}) => {
{
if (url.pathname === captureUrl.pathname && url.origin !== captureUrl.origin) {
logger_mjs.logger.debug(`${capture} only partially matches the cross-origin URL ` + `${url}. This route will only handle cross-origin requests ` + `if they match the entire URL.`);
}
}
return url.href === captureUrl.href;
};
route = new Route(matchCallback, handler, method);
} else if (capture instanceof RegExp) {
route = new RegExpRoute(capture, handler, method);
} else if (typeof capture === 'function') {
route = new Route(capture, handler, method);
} else if (capture instanceof Route) {
route = capture;
} else {
throw new WorkboxError_mjs.WorkboxError('unsupported-route-type', {
moduleName: 'workbox-routing',
className: 'DefaultRouter',
funcName: 'registerRoute',
paramName: 'capture'
});
}
super.registerRoute(route);
return route;
const registerNavigationRoute = (cachedAssetUrl, options = {}) => {
{
assert_mjs.assert.isType(cachedAssetUrl, 'string', {
moduleName: 'workbox-routing',
funcName: 'registerNavigationRoute',
paramName: 'cachedAssetUrl'
});
}
/**
* Register a route that will return a precached file for a navigation
* request. This is useful for the
* [application shell pattern]{@link https://developers.google.com/web/fundamentals/architecture/app-shell}.
*
* This method will generate a
* [NavigationRoute]{@link workbox.routing.NavigationRoute}
* and call
* [Router.registerRoute()]{@link workbox.routing.Router#registerRoute}
* .
*
* @param {string} cachedAssetUrl
* @param {Object} [options]
* @param {string} [options.cacheName] Cache name to store and retrieve
* requests. Defaults to precache cache name provided by
* [workbox-core.cacheNames]{@link workbox.core.cacheNames}.
* @param {Array<RegExp>} [options.blacklist=[]] If any of these patterns
* match, the route will not handle the request (even if a whitelist entry
* matches).
* @param {Array<RegExp>} [options.whitelist=[/./]] If any of these patterns
* match the URL's pathname and search parameter, the route will handle the
* request (assuming the blacklist doesn't match).
* @return {workbox.routing.NavigationRoute} Returns the generated
* Route.
*
* @alias workbox.routing.registerNavigationRoute
*/
const cacheName = cacheNames_mjs.cacheNames.getPrecacheName(options.cacheName);
registerNavigationRoute(cachedAssetUrl, options = {}) {
{
assert_mjs.assert.isType(cachedAssetUrl, 'string', {
moduleName: 'workbox-routing',
className: '[default export]',
funcName: 'registerNavigationRoute',
paramName: 'cachedAssetUrl'
const handler = async () => {
try {
const response = await caches.match(cachedAssetUrl, {
cacheName
});
}
const cacheName = cacheNames_mjs.cacheNames.getPrecacheName(options.cacheName);
const handler = () => caches.match(cachedAssetUrl, {
cacheName
}).then(response => {
if (response) {

@@ -882,3 +811,3 @@ return response;

throw new Error(`The cache ${cacheName} did not have an entry for ` + `${cachedAssetUrl}.`);
}).catch(error => {
} catch (error) {
// If there's either a cache miss, or the caches.match() call threw

@@ -889,3 +818,3 @@ // an exception, then attempt to fulfill the navigation request with

{
logger_mjs.logger.debug(`Unable to respond to navigation request with ` + `cached response: ${error.message}. Falling back to network.`);
logger_mjs.logger.debug(`Unable to respond to navigation request with ` + `cached response. Falling back to network.`, error);
} // This might still fail if the browser is offline...

@@ -895,16 +824,16 @@

return fetch(cachedAssetUrl);
});
}
};
const route = new NavigationRoute(handler, {
whitelist: options.whitelist,
blacklist: options.blacklist
});
super.registerRoute(route);
return route;
}
const route = new NavigationRoute(handler, {
whitelist: options.whitelist,
blacklist: options.blacklist
});
const defaultRouter = getOrCreateDefaultRouter();
defaultRouter.registerRoute(route);
return route;
};
}
/*
Copyright 2018 Google LLC
Copyright 2019 Google LLC

@@ -915,11 +844,85 @@ Use of this source code is governed by an MIT-style

*/
/**
* Easily register a RegExp, string, or function with a caching
* strategy to a singleton Router instance.
*
* This method will generate a Route for you if needed and
* call [Router.registerRoute()]{@link
* workbox.routing.Router#registerRoute}.
*
* @param {
* RegExp|
* string|
* workbox.routing.Route~matchCallback|
* workbox.routing.Route
* } capture
* If the capture param is a `Route`, all other arguments will be ignored.
* @param {workbox.routing.Route~handlerCallback} handler A callback
* function that returns a Promise resulting in a Response.
* @param {string} [method='GET'] The HTTP method to match the Route
* against.
* @return {workbox.routing.Route} The generated `Route`(Useful for
* unregistering).
*
* @alias workbox.routing.registerRoute
*/
var publicAPI = /*#__PURE__*/Object.freeze({
DefaultRouter: DefaultRouter,
RegExpRoute: RegExpRoute,
Route: Route,
Router: Router,
NavigationRoute: NavigationRoute
});
const registerRoute = (capture, handler, method = 'GET') => {
let route;
if (typeof capture === 'string') {
const captureUrl = new URL(capture, location);
{
if (!(capture.startsWith('/') || capture.startsWith('http'))) {
throw new WorkboxError_mjs.WorkboxError('invalid-string', {
moduleName: 'workbox-routing',
funcName: 'registerRoute',
paramName: 'capture'
});
} // We want to check if Express-style wildcards are in the pathname only.
// TODO: Remove this log message in v4.
const valueToCheck = capture.startsWith('http') ? captureUrl.pathname : capture; // See https://github.com/pillarjs/path-to-regexp#parameters
const wildcards = '[*:?+]';
if (valueToCheck.match(new RegExp(`${wildcards}`))) {
logger_mjs.logger.debug(`The '$capture' parameter contains an Express-style wildcard ` + `character (${wildcards}). Strings are now always interpreted as ` + `exact matches; use a RegExp for partial or wildcard matches.`);
}
}
const matchCallback = ({
url
}) => {
{
if (url.pathname === captureUrl.pathname && url.origin !== captureUrl.origin) {
logger_mjs.logger.debug(`${capture} only partially matches the cross-origin URL ` + `${url}. This route will only handle cross-origin requests ` + `if they match the entire URL.`);
}
}
return url.href === captureUrl.href;
};
route = new Route(matchCallback, handler, method);
} else if (capture instanceof RegExp) {
route = new RegExpRoute(capture, handler, method);
} else if (typeof capture === 'function') {
route = new Route(capture, handler, method);
} else if (capture instanceof Route) {
route = capture;
} else {
throw new WorkboxError_mjs.WorkboxError('unsupported-route-type', {
moduleName: 'workbox-routing',
funcName: 'registerRoute',
paramName: 'capture'
});
}
const defaultRouter = getOrCreateDefaultRouter();
defaultRouter.registerRoute(route);
return route;
};
/*

@@ -934,20 +937,16 @@ Copyright 2018 Google LLC

{
assert_mjs.assert.isSwEnv('workbox-routing');
assert_mjs.assert.isSWEnv('workbox-routing');
}
var defaultExport = new DefaultRouter();
exports.NavigationRoute = NavigationRoute;
exports.RegExpRoute = RegExpRoute;
exports.registerNavigationRoute = registerNavigationRoute;
exports.registerRoute = registerRoute;
exports.Route = Route;
exports.Router = Router;
/*
Copyright 2018 Google LLC
return exports;
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);
}({},workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private));
return finalExport;
}(workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private,workbox.core._private));
//# sourceMappingURL=workbox-routing.dev.js.map

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

this.workbox=this.workbox||{},this.workbox.routing=function(t,e){"use strict";try{self.workbox.v["workbox:routing:4.0.0-beta.0"]=1}catch(t){}const r="GET";var s=t=>t&&"object"==typeof t?t:{handle:t};class n{constructor(t,e,n){this.handler=s(e),this.match=t,this.method=n||r}}class o extends n{constructor(t,{whitelist:e=[/./],blacklist:r=[]}={}){super(t=>this.t(t),t),this.e=e,this.r=r}t({url:t,request:e}){if("navigate"!==e.mode)return!1;const r=t.pathname+t.search;for(const t of this.r)if(t.test(r))return!1;return!!this.e.some(t=>t.test(r))}}class i extends n{constructor(t,e,r){super(({url:e})=>{const r=t.exec(e.href);return r?e.origin!==location.origin&&0!==r.index?null:r.slice(1):null},e,r)}}class u{constructor(){this.s=new Map}get routes(){return this.s}addFetchListener(){self.addEventListener("fetch",t=>{const{request:e}=t,r=this.handleRequest({request:e,event:t});r&&t.respondWith(r)})}addCacheListener(){self.addEventListener("message",t=>{if("CACHE_URLS"===t.data.type&&"workbox-window"===t.data.meta){const{urlsToCache:e}=t.data.payload;for(const r of e){const e=new Request(r);this.handleRequest({request:e,event:t})}}})}handleRequest({request:t,event:e}){const r=new URL(t.url,location);if(!r.protocol.startsWith("http"))return;let s,{params:n,route:o}=this.findMatchingRoute({url:r,request:t,event:e}),i=o&&o.handler;if(!i&&this.n&&(i=this.n),i){try{s=i.handle({url:r,request:t,event:e,params:n})}catch(t){s=Promise.reject(t)}return s&&this.o&&(s=s.catch(t=>this.o.handle({url:r,event:e,err:t}))),s}}findMatchingRoute({url:t,request:e,event:r}){const s=this.s.get(e.method)||[];for(const n of s){let s,o=n.match({url:t,request:e,event:r});if(o)return Array.isArray(o)&&o.length>0?s=o:o.constructor===Object&&Object.keys(o).length>0&&(s=o),{route:n,params:s}}return{}}setDefaultHandler(t){this.n=s(t)}setCatchHandler(t){this.o=s(t)}registerRoute(t){this.s.has(t.method)||this.s.set(t.method,[]),this.s.get(t.method).push(t)}unregisterRoute(e){if(!this.s.has(e.method))throw new t.WorkboxError("unregister-route-but-not-found-with-method",{method:e.method});const r=this.s.get(e.method).indexOf(e);if(!(r>-1))throw new t.WorkboxError("unregister-route-route-not-registered");this.s.get(e.method).splice(r,1)}}class c extends u{constructor(){super(),this.addFetchListener(),this.addCacheListener()}registerRoute(e,r,s="GET"){let o;if("string"==typeof e){const t=new URL(e,location);o=new n(({url:e})=>e.href===t.href,r,s)}else if(e instanceof RegExp)o=new i(e,r,s);else if("function"==typeof e)o=new n(e,r,s);else{if(!(e instanceof n))throw new t.WorkboxError("unsupported-route-type",{moduleName:"workbox-routing",className:"DefaultRouter",funcName:"registerRoute",paramName:"capture"});o=e}return super.registerRoute(o),o}registerNavigationRoute(t,r={}){const s=e.cacheNames.getPrecacheName(r.cacheName),n=new o(()=>caches.match(t,{cacheName:s}).then(e=>{if(e)return e;throw new Error(`The cache ${s} did not have an entry for `+`${t}.`)}).catch(e=>fetch(t)),{whitelist:r.whitelist,blacklist:r.blacklist});return super.registerRoute(n),n}}var a=Object.freeze({DefaultRouter:c,RegExpRoute:i,Route:n,Router:u,NavigationRoute:o}),h=new c;return Object.assign(h,a)}(workbox.core._private,workbox.core._private);
this.workbox=this.workbox||{},this.workbox.routing=function(t,e,r){"use strict";try{self.workbox.v["workbox:routing:4.0.0-beta.1"]=1}catch(t){}const s="GET",n=t=>t&&"object"==typeof t?t:{handle:t};class o{constructor(t,e,r){this.handler=n(e),this.match=t,this.method=r||s}}class i extends o{constructor(t,{whitelist:e=[/./],blacklist:r=[]}={}){super(t=>this.t(t),t),this.e=e,this.r=r}t({url:t,request:e}){if("navigate"!==e.mode)return!1;const r=t.pathname+t.search;for(const t of this.r)if(t.test(r))return!1;return!!this.e.some(t=>t.test(r))}}class u extends o{constructor(t,e,r){super(({url:e})=>{const r=t.exec(e.href);return r?e.origin!==location.origin&&0!==r.index?null:r.slice(1):null},e,r)}}class c{constructor(){this.s=new Map}get routes(){return this.s}addFetchListener(){self.addEventListener("fetch",t=>{const{request:e}=t,r=this.handleRequest({request:e,event:t});r&&t.respondWith(r)})}addCacheListener(){self.addEventListener("message",t=>{if("CACHE_URLS"===t.data.type&&"workbox-window"===t.data.meta){const{urlsToCache:e}=t.data.payload;for(const r of e){const e=new Request(r);this.handleRequest({request:e,event:t})}}})}handleRequest({request:t,event:e}){const r=new URL(t.url,location);if(!r.protocol.startsWith("http"))return;let s,{params:n,route:o}=this.findMatchingRoute({url:r,request:t,event:e}),i=o&&o.handler;if(!i&&this.n&&(i=this.n),i){try{s=i.handle({url:r,request:t,event:e,params:n})}catch(t){s=Promise.reject(t)}return s&&this.o&&(s=s.catch(t=>this.o.handle({url:r,event:e,err:t}))),s}}findMatchingRoute({url:t,request:e,event:r}){const s=this.s.get(e.method)||[];for(const n of s){let s,o=n.match({url:t,request:e,event:r});if(o)return Array.isArray(o)&&o.length>0?s=o:o.constructor===Object&&Object.keys(o).length>0&&(s=o),{route:n,params:s}}return{}}setDefaultHandler(t){this.n=n(t)}setCatchHandler(t){this.o=n(t)}registerRoute(t){this.s.has(t.method)||this.s.set(t.method,[]),this.s.get(t.method).push(t)}unregisterRoute(t){if(!this.s.has(t.method))throw new e.WorkboxError("unregister-route-but-not-found-with-method",{method:t.method});const r=this.s.get(t.method).indexOf(t);if(!(r>-1))throw new e.WorkboxError("unregister-route-route-not-registered");this.s.get(t.method).splice(r,1)}}let h;const a=()=>(h||((h=new c).addFetchListener(),h.addCacheListener()),h);return t.NavigationRoute=i,t.RegExpRoute=u,t.registerNavigationRoute=((t,e={})=>{const s=r.cacheNames.getPrecacheName(e.cacheName),n=new i(async()=>{try{const e=await caches.match(t,{cacheName:s});if(e)return e;throw new Error(`The cache ${s} did not have an entry for `+`${t}.`)}catch(e){return fetch(t)}},{whitelist:e.whitelist,blacklist:e.blacklist});return a().registerRoute(n),n}),t.registerRoute=((t,r,s="GET")=>{let n;if("string"==typeof t){const e=new URL(t,location);n=new o(({url:t})=>t.href===e.href,r,s)}else if(t instanceof RegExp)n=new u(t,r,s);else if("function"==typeof t)n=new o(t,r,s);else{if(!(t instanceof o))throw new e.WorkboxError("unsupported-route-type",{moduleName:"workbox-routing",funcName:"registerRoute",paramName:"capture"});n=t}return a().registerRoute(n),n}),t.Route=o,t.Router=c,t}({},workbox.core._private,workbox.core._private);
//# sourceMappingURL=workbox-routing.prod.js.map
{
"name": "workbox-routing",
"version": "4.0.0-beta.0",
"version": "4.0.0-beta.1",
"license": "MIT",

@@ -30,5 +30,5 @@ "author": "Google's Web DevRel Team",

"dependencies": {
"workbox-core": "^4.0.0-beta.0"
"workbox-core": "^4.0.0-beta.1"
},
"gitHead": "bc90cc4bdb1f8ad435564aa84b0c90acfac611e2"
"gitHead": "dda07c48e184f57ce5dfe3b3e93af316989a0877"
}

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc