123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- import '../_version.mjs';
- const CDN_PATH = `WORKBOX_CDN_ROOT_URL`;
- const MODULE_KEY_TO_NAME_MAPPING = {
-
- backgroundSync: 'background-sync',
-
- broadcastUpdate: 'broadcast-update',
-
- cacheableResponse: 'cacheable-response',
-
- core: 'core',
-
- expiration: 'expiration',
-
- googleAnalytics: 'offline-ga',
-
- navigationPreload: 'navigation-preload',
-
- precaching: 'precaching',
-
- rangeRequests: 'range-requests',
-
- routing: 'routing',
-
- strategies: 'strategies',
-
- streams: 'streams',
-
- recipes: 'recipes',
- };
- export class WorkboxSW {
-
- constructor() {
- this.v = {};
- this._options = {
- debug: self.location.hostname === 'localhost',
- modulePathPrefix: null,
- modulePathCb: null,
- };
- this._env = this._options.debug ? 'dev' : 'prod';
- this._modulesLoaded = false;
- return new Proxy(this, {
- get(target, key) {
- if (target[key]) {
- return target[key];
- }
- const moduleName = MODULE_KEY_TO_NAME_MAPPING[key];
- if (moduleName) {
- target.loadModule(`workbox-${moduleName}`);
- }
- return target[key];
- },
- });
- }
-
- setConfig(options = {}) {
- if (!this._modulesLoaded) {
- Object.assign(this._options, options);
- this._env = this._options.debug ? 'dev' : 'prod';
- } else {
- throw new Error('Config must be set before accessing workbox.* modules');
- }
- }
-
- loadModule(moduleName) {
- const modulePath = this._getImportPath(moduleName);
- try {
- importScripts(modulePath);
- this._modulesLoaded = true;
- } catch (err) {
-
-
-
- console.error(
- `Unable to import module '${moduleName}' from '${modulePath}'.`);
- throw err;
- }
- }
-
- _getImportPath(moduleName) {
- if (this._options.modulePathCb) {
- return this._options.modulePathCb(moduleName, this._options.debug);
- }
-
- let pathParts = [CDN_PATH];
- const fileName = `${moduleName}.${this._env}.js`;
- const pathPrefix = this._options.modulePathPrefix;
- if (pathPrefix) {
-
- pathParts = pathPrefix.split('/');
-
-
- if (pathParts[pathParts.length - 1] === '') {
- pathParts.splice(pathParts.length - 1, 1);
- }
- }
- pathParts.push(fileName);
- return pathParts.join('/');
- }
- }
|