| 1 | /*
|
|---|
| 2 | Copyright 2018 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import '../_version.mjs';
|
|---|
| 10 |
|
|---|
| 11 | const CDN_PATH = `WORKBOX_CDN_ROOT_URL`;
|
|---|
| 12 |
|
|---|
| 13 | const MODULE_KEY_TO_NAME_MAPPING = {
|
|---|
| 14 | /**
|
|---|
| 15 | * @name backgroundSync
|
|---|
| 16 | * @memberof workbox
|
|---|
| 17 | * @see module:workbox-background-sync
|
|---|
| 18 | */
|
|---|
| 19 | backgroundSync: 'background-sync',
|
|---|
| 20 | /**
|
|---|
| 21 | * @name broadcastUpdate
|
|---|
| 22 | * @memberof workbox
|
|---|
| 23 | * @see module:workbox-broadcast-update
|
|---|
| 24 | */
|
|---|
| 25 | broadcastUpdate: 'broadcast-update',
|
|---|
| 26 | /**
|
|---|
| 27 | * @name cacheableResponse
|
|---|
| 28 | * @memberof workbox
|
|---|
| 29 | * @see module:workbox-cacheable-response
|
|---|
| 30 | */
|
|---|
| 31 | cacheableResponse: 'cacheable-response',
|
|---|
| 32 | /**
|
|---|
| 33 | * @name core
|
|---|
| 34 | * @memberof workbox
|
|---|
| 35 | * @see module:workbox-core
|
|---|
| 36 | */
|
|---|
| 37 | core: 'core',
|
|---|
| 38 | /**
|
|---|
| 39 | * @name expiration
|
|---|
| 40 | * @memberof workbox
|
|---|
| 41 | * @see module:workbox-expiration
|
|---|
| 42 | */
|
|---|
| 43 | expiration: 'expiration',
|
|---|
| 44 | /**
|
|---|
| 45 | * @name googleAnalytics
|
|---|
| 46 | * @memberof workbox
|
|---|
| 47 | * @see module:workbox-google-analytics
|
|---|
| 48 | */
|
|---|
| 49 | googleAnalytics: 'offline-ga',
|
|---|
| 50 | /**
|
|---|
| 51 | * @name navigationPreload
|
|---|
| 52 | * @memberof workbox
|
|---|
| 53 | * @see module:workbox-navigation-preload
|
|---|
| 54 | */
|
|---|
| 55 | navigationPreload: 'navigation-preload',
|
|---|
| 56 | /**
|
|---|
| 57 | * @name precaching
|
|---|
| 58 | * @memberof workbox
|
|---|
| 59 | * @see module:workbox-precaching
|
|---|
| 60 | */
|
|---|
| 61 | precaching: 'precaching',
|
|---|
| 62 | /**
|
|---|
| 63 | * @name rangeRequests
|
|---|
| 64 | * @memberof workbox
|
|---|
| 65 | * @see module:workbox-range-requests
|
|---|
| 66 | */
|
|---|
| 67 | rangeRequests: 'range-requests',
|
|---|
| 68 | /**
|
|---|
| 69 | * @name routing
|
|---|
| 70 | * @memberof workbox
|
|---|
| 71 | * @see module:workbox-routing
|
|---|
| 72 | */
|
|---|
| 73 | routing: 'routing',
|
|---|
| 74 | /**
|
|---|
| 75 | * @name strategies
|
|---|
| 76 | * @memberof workbox
|
|---|
| 77 | * @see module:workbox-strategies
|
|---|
| 78 | */
|
|---|
| 79 | strategies: 'strategies',
|
|---|
| 80 | /**
|
|---|
| 81 | * @name streams
|
|---|
| 82 | * @memberof workbox
|
|---|
| 83 | * @see module:workbox-streams
|
|---|
| 84 | */
|
|---|
| 85 | streams: 'streams',
|
|---|
| 86 | /**
|
|---|
| 87 | * @name recipes
|
|---|
| 88 | * @memberof workbox
|
|---|
| 89 | * @see module:workbox-recipes
|
|---|
| 90 | */
|
|---|
| 91 | recipes: 'recipes',
|
|---|
| 92 | };
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * This class can be used to make it easy to use the various parts of
|
|---|
| 96 | * Workbox.
|
|---|
| 97 | *
|
|---|
| 98 | * @private
|
|---|
| 99 | */
|
|---|
| 100 | export class WorkboxSW {
|
|---|
| 101 | /**
|
|---|
| 102 | * Creates a proxy that automatically loads workbox namespaces on demand.
|
|---|
| 103 | *
|
|---|
| 104 | * @private
|
|---|
| 105 | */
|
|---|
| 106 | constructor() {
|
|---|
| 107 | this.v = {};
|
|---|
| 108 | this._options = {
|
|---|
| 109 | debug: self.location.hostname === 'localhost',
|
|---|
| 110 | modulePathPrefix: null,
|
|---|
| 111 | modulePathCb: null,
|
|---|
| 112 | };
|
|---|
| 113 |
|
|---|
| 114 | this._env = this._options.debug ? 'dev' : 'prod';
|
|---|
| 115 | this._modulesLoaded = false;
|
|---|
| 116 |
|
|---|
| 117 | return new Proxy(this, {
|
|---|
| 118 | get(target, key) {
|
|---|
| 119 | if (target[key]) {
|
|---|
| 120 | return target[key];
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | const moduleName = MODULE_KEY_TO_NAME_MAPPING[key];
|
|---|
| 124 | if (moduleName) {
|
|---|
| 125 | target.loadModule(`workbox-${moduleName}`);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | return target[key];
|
|---|
| 129 | },
|
|---|
| 130 | });
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * Updates the configuration options. You can specify whether to treat as a
|
|---|
| 135 | * debug build and whether to use a CDN or a specific path when importing
|
|---|
| 136 | * other workbox-modules
|
|---|
| 137 | *
|
|---|
| 138 | * @param {Object} [options]
|
|---|
| 139 | * @param {boolean} [options.debug] If true, `dev` builds are using, otherwise
|
|---|
| 140 | * `prod` builds are used. By default, `prod` is used unless on localhost.
|
|---|
| 141 | * @param {Function} [options.modulePathPrefix] To avoid using the CDN with
|
|---|
| 142 | * `workbox-sw` set the path prefix of where modules should be loaded from.
|
|---|
| 143 | * For example `modulePathPrefix: '/third_party/workbox/v3.0.0/'`.
|
|---|
| 144 | * @param {workbox~ModulePathCallback} [options.modulePathCb] If defined,
|
|---|
| 145 | * this callback will be responsible for determining the path of each
|
|---|
| 146 | * workbox module.
|
|---|
| 147 | *
|
|---|
| 148 | * @alias workbox.setConfig
|
|---|
| 149 | */
|
|---|
| 150 | setConfig(options = {}) {
|
|---|
| 151 | if (!this._modulesLoaded) {
|
|---|
| 152 | Object.assign(this._options, options);
|
|---|
| 153 | this._env = this._options.debug ? 'dev' : 'prod';
|
|---|
| 154 | } else {
|
|---|
| 155 | throw new Error('Config must be set before accessing workbox.* modules');
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Load a Workbox module by passing in the appropriate module name.
|
|---|
| 161 | *
|
|---|
| 162 | * This is not generally needed unless you know there are modules that are
|
|---|
| 163 | * dynamically used and you want to safe guard use of the module while the
|
|---|
| 164 | * user may be offline.
|
|---|
| 165 | *
|
|---|
| 166 | * @param {string} moduleName
|
|---|
| 167 | *
|
|---|
| 168 | * @alias workbox.loadModule
|
|---|
| 169 | */
|
|---|
| 170 | loadModule(moduleName) {
|
|---|
| 171 | const modulePath = this._getImportPath(moduleName);
|
|---|
| 172 | try {
|
|---|
| 173 | importScripts(modulePath);
|
|---|
| 174 | this._modulesLoaded = true;
|
|---|
| 175 | } catch (err) {
|
|---|
| 176 | // TODO Add context of this error if using the CDN vs the local file.
|
|---|
| 177 |
|
|---|
| 178 | // We can't rely on workbox-core being loaded so using console
|
|---|
| 179 | // eslint-disable-next-line
|
|---|
| 180 | console.error(
|
|---|
| 181 | `Unable to import module '${moduleName}' from '${modulePath}'.`);
|
|---|
| 182 | throw err;
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /**
|
|---|
| 187 | * This method will get the path / CDN URL to be used for importScript calls.
|
|---|
| 188 | *
|
|---|
| 189 | * @param {string} moduleName
|
|---|
| 190 | * @return {string} URL to the desired module.
|
|---|
| 191 | *
|
|---|
| 192 | * @private
|
|---|
| 193 | */
|
|---|
| 194 | _getImportPath(moduleName) {
|
|---|
| 195 | if (this._options.modulePathCb) {
|
|---|
| 196 | return this._options.modulePathCb(moduleName, this._options.debug);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | // TODO: This needs to be dynamic some how.
|
|---|
| 200 | let pathParts = [CDN_PATH];
|
|---|
| 201 |
|
|---|
| 202 | const fileName = `${moduleName}.${this._env}.js`;
|
|---|
| 203 |
|
|---|
| 204 | const pathPrefix = this._options.modulePathPrefix;
|
|---|
| 205 | if (pathPrefix) {
|
|---|
| 206 | // Split to avoid issues with developers ending / not ending with slash
|
|---|
| 207 | pathParts = pathPrefix.split('/');
|
|---|
| 208 |
|
|---|
| 209 | // We don't need a slash at the end as we will be adding
|
|---|
| 210 | // a filename regardless
|
|---|
| 211 | if (pathParts[pathParts.length - 1] === '') {
|
|---|
| 212 | pathParts.splice(pathParts.length - 1, 1);
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | pathParts.push(fileName);
|
|---|
| 217 |
|
|---|
| 218 | return pathParts.join('/');
|
|---|
| 219 | }
|
|---|
| 220 | }
|
|---|