12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121 |
- try {
- self['workbox:window:6.6.0'] && _();
- } catch (e) {}
- function messageSW(sw, data) {
- return new Promise(function (resolve) {
- var messageChannel = new MessageChannel();
- messageChannel.port1.onmessage = function (event) {
- resolve(event.data);
- };
- sw.postMessage(data, [messageChannel.port2]);
- });
- }
- function _defineProperties(target, props) {
- for (var i = 0; i < props.length; i++) {
- var descriptor = props[i];
- descriptor.enumerable = descriptor.enumerable || false;
- descriptor.configurable = true;
- if ("value" in descriptor) descriptor.writable = true;
- Object.defineProperty(target, descriptor.key, descriptor);
- }
- }
- function _createClass(Constructor, protoProps, staticProps) {
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
- if (staticProps) _defineProperties(Constructor, staticProps);
- return Constructor;
- }
- function _inheritsLoose(subClass, superClass) {
- subClass.prototype = Object.create(superClass.prototype);
- subClass.prototype.constructor = subClass;
- subClass.__proto__ = superClass;
- }
- function _unsupportedIterableToArray(o, minLen) {
- if (!o) return;
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
- var n = Object.prototype.toString.call(o).slice(8, -1);
- if (n === "Object" && o.constructor) n = o.constructor.name;
- if (n === "Map" || n === "Set") return Array.from(o);
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
- }
- function _arrayLikeToArray(arr, len) {
- if (len == null || len > arr.length) len = arr.length;
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
- return arr2;
- }
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
- var it;
- if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
- if (it) o = it;
- var i = 0;
- return function () {
- if (i >= o.length) return {
- done: true
- };
- return {
- done: false,
- value: o[i++]
- };
- };
- }
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
- }
- it = o[Symbol.iterator]();
- return it.next.bind(it);
- }
- try {
- self['workbox:core:6.6.0'] && _();
- } catch (e) {}
- var Deferred =
- function Deferred() {
- var _this = this;
- this.promise = new Promise(function (resolve, reject) {
- _this.resolve = resolve;
- _this.reject = reject;
- });
- };
- function dontWaitFor(promise) {
-
- void promise.then(function () {});
- }
- var logger = function () {
-
-
- if (!('__WB_DISABLE_DEV_LOGS' in globalThis)) {
- self.__WB_DISABLE_DEV_LOGS = false;
- }
- var inGroup = false;
- var methodToColorMap = {
- debug: "#7f8c8d",
- log: "#2ecc71",
- warn: "#f39c12",
- error: "#c0392b",
- groupCollapsed: "#3498db",
- groupEnd: null
- };
- var print = function print(method, args) {
- var _console2;
- if (self.__WB_DISABLE_DEV_LOGS) {
- return;
- }
- if (method === 'groupCollapsed') {
-
-
- if (/^((?!chrome|android).)*safari/i.test(navigator.userAgent)) {
- var _console;
- (_console = console)[method].apply(_console, args);
- return;
- }
- }
- var styles = ["background: " + methodToColorMap[method], "border-radius: 0.5em", "color: white", "font-weight: bold", "padding: 2px 0.5em"];
- var logPrefix = inGroup ? [] : ['%cworkbox', styles.join(';')];
- (_console2 = console)[method].apply(_console2, logPrefix.concat(args));
- if (method === 'groupCollapsed') {
- inGroup = true;
- }
- if (method === 'groupEnd') {
- inGroup = false;
- }
- };
- var api = {};
- var loggerMethods = Object.keys(methodToColorMap);
- var _loop = function _loop() {
- var key = _loggerMethods[_i];
- var method = key;
- api[method] = function () {
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
- print(method, args);
- };
- };
- for (var _i = 0, _loggerMethods = loggerMethods; _i < _loggerMethods.length; _i++) {
- _loop();
- }
- return api;
- }();
- var WorkboxEventTarget = function () {
- function WorkboxEventTarget() {
- this._eventListenerRegistry = new Map();
- }
-
- var _proto = WorkboxEventTarget.prototype;
- _proto.addEventListener = function addEventListener(type, listener) {
- var foo = this._getEventListenersByType(type);
- foo.add(listener);
- }
-
- ;
- _proto.removeEventListener = function removeEventListener(type, listener) {
- this._getEventListenersByType(type).delete(listener);
- }
-
- ;
- _proto.dispatchEvent = function dispatchEvent(event) {
- event.target = this;
- var listeners = this._getEventListenersByType(event.type);
- for (var _iterator = _createForOfIteratorHelperLoose(listeners), _step; !(_step = _iterator()).done;) {
- var listener = _step.value;
- listener(event);
- }
- }
-
- ;
- _proto._getEventListenersByType = function _getEventListenersByType(type) {
- if (!this._eventListenerRegistry.has(type)) {
- this._eventListenerRegistry.set(type, new Set());
- }
- return this._eventListenerRegistry.get(type);
- };
- return WorkboxEventTarget;
- }();
- function urlsMatch(url1, url2) {
- var _location = location,
- href = _location.href;
- return new URL(url1, href).href === new URL(url2, href).href;
- }
- var WorkboxEvent = function WorkboxEvent(type, props) {
- this.type = type;
- Object.assign(this, props);
- };
- function _await(value, then, direct) {
- if (direct) {
- return then ? then(value) : value;
- }
- if (!value || !value.then) {
- value = Promise.resolve(value);
- }
- return then ? value.then(then) : value;
- }
- var WAITING_TIMEOUT_DURATION = 200;
- function _async(f) {
- return function () {
- for (var args = [], i = 0; i < arguments.length; i++) {
- args[i] = arguments[i];
- }
- try {
- return Promise.resolve(f.apply(this, args));
- } catch (e) {
- return Promise.reject(e);
- }
- };
- }
- var REGISTRATION_TIMEOUT_DURATION = 60000;
- function _empty() {}
- var SKIP_WAITING_MESSAGE = {
- type: 'SKIP_WAITING'
- };
- function _awaitIgnored(value, direct) {
- if (!direct) {
- return value && value.then ? value.then(_empty) : Promise.resolve();
- }
- }
- var Workbox = function (_WorkboxEventTarget) {
- _inheritsLoose(Workbox, _WorkboxEventTarget);
-
-
- function Workbox(scriptURL, registerOptions) {
- var _this;
- if (registerOptions === void 0) {
- registerOptions = {};
- }
- _this = _WorkboxEventTarget.call(this) || this;
- _this._registerOptions = {};
- _this._updateFoundCount = 0;
- _this._swDeferred = new Deferred();
- _this._activeDeferred = new Deferred();
- _this._controllingDeferred = new Deferred();
- _this._registrationTime = 0;
- _this._ownSWs = new Set();
-
- _this._onUpdateFound = function () {
-
- var registration = _this._registration;
- var installingSW = registration.installing;
-
-
-
-
-
-
-
-
-
-
-
- var updateLikelyTriggeredExternally =
-
-
-
- _this._updateFoundCount > 0 ||
-
-
- !urlsMatch(installingSW.scriptURL, _this._scriptURL.toString()) ||
-
-
- performance.now() > _this._registrationTime + REGISTRATION_TIMEOUT_DURATION ?
-
- true : false;
- if (updateLikelyTriggeredExternally) {
- _this._externalSW = installingSW;
- registration.removeEventListener('updatefound', _this._onUpdateFound);
- } else {
-
-
- _this._sw = installingSW;
- _this._ownSWs.add(installingSW);
- _this._swDeferred.resolve(installingSW);
-
- {
- if (navigator.serviceWorker.controller) {
- logger.log('Updated service worker found. Installing now...');
- } else {
- logger.log('Service worker is installing...');
- }
- }
- }
-
- ++_this._updateFoundCount;
-
- installingSW.addEventListener('statechange', _this._onStateChange);
- };
-
- _this._onStateChange = function (originalEvent) {
-
- var registration = _this._registration;
- var sw = originalEvent.target;
- var state = sw.state;
- var isExternal = sw === _this._externalSW;
- var eventProps = {
- sw: sw,
- isExternal: isExternal,
- originalEvent: originalEvent
- };
- if (!isExternal && _this._isUpdate) {
- eventProps.isUpdate = true;
- }
- _this.dispatchEvent(new WorkboxEvent(state, eventProps));
- if (state === 'installed') {
-
-
-
-
-
-
-
-
- _this._waitingTimeout = self.setTimeout(function () {
-
- if (state === 'installed' && registration.waiting === sw) {
- _this.dispatchEvent(new WorkboxEvent('waiting', eventProps));
- {
- if (isExternal) {
- logger.warn('An external service worker has installed but is ' + 'waiting for this client to close before activating...');
- } else {
- logger.warn('The service worker has installed but is waiting ' + 'for existing clients to close before activating...');
- }
- }
- }
- }, WAITING_TIMEOUT_DURATION);
- } else if (state === 'activating') {
- clearTimeout(_this._waitingTimeout);
- if (!isExternal) {
- _this._activeDeferred.resolve(sw);
- }
- }
- {
- switch (state) {
- case 'installed':
- if (isExternal) {
- logger.warn('An external service worker has installed. ' + 'You may want to suggest users reload this page.');
- } else {
- logger.log('Registered service worker installed.');
- }
- break;
- case 'activated':
- if (isExternal) {
- logger.warn('An external service worker has activated.');
- } else {
- logger.log('Registered service worker activated.');
- if (sw !== navigator.serviceWorker.controller) {
- logger.warn('The registered service worker is active but ' + 'not yet controlling the page. Reload or run ' + '`clients.claim()` in the service worker.');
- }
- }
- break;
- case 'redundant':
- if (sw === _this._compatibleControllingSW) {
- logger.log('Previously controlling service worker now redundant!');
- } else if (!isExternal) {
- logger.log('Registered service worker now redundant!');
- }
- break;
- }
- }
- };
-
- _this._onControllerChange = function (originalEvent) {
- var sw = _this._sw;
- var isExternal = sw !== navigator.serviceWorker.controller;
-
-
-
- _this.dispatchEvent(new WorkboxEvent('controlling', {
- isExternal: isExternal,
- originalEvent: originalEvent,
- sw: sw,
- isUpdate: _this._isUpdate
- }));
- if (!isExternal) {
- {
- logger.log('Registered service worker now controlling this page.');
- }
- _this._controllingDeferred.resolve(sw);
- }
- };
-
- _this._onMessage = _async(function (originalEvent) {
-
-
- var data = originalEvent.data,
- ports = originalEvent.ports,
- source = originalEvent.source;
-
- return _await(_this.getSW(), function () {
- if (_this._ownSWs.has(source)) {
- _this.dispatchEvent(new WorkboxEvent('message', {
-
-
- data: data,
- originalEvent: originalEvent,
- ports: ports,
- sw: source
- }));
- }
- });
-
-
-
-
-
- });
- _this._scriptURL = scriptURL;
- _this._registerOptions = registerOptions;
-
-
- navigator.serviceWorker.addEventListener('message', _this._onMessage);
- return _this;
- }
-
- var _proto = Workbox.prototype;
- _proto.register = function register(_temp) {
- var _ref = _temp === void 0 ? {} : _temp,
- _ref$immediate = _ref.immediate,
- immediate = _ref$immediate === void 0 ? false : _ref$immediate;
- try {
- var _this3 = this;
- if ("dev" !== 'production') {
- if (_this3._registrationTime) {
- logger.error('Cannot re-register a Workbox instance after it has ' + 'been registered. Create a new instance instead.');
- return;
- }
- }
- return _invoke(function () {
- if (!immediate && document.readyState !== 'complete') {
- return _awaitIgnored(new Promise(function (res) {
- return window.addEventListener('load', res);
- }));
- }
- }, function () {
-
-
- _this3._isUpdate = Boolean(navigator.serviceWorker.controller);
-
-
- _this3._compatibleControllingSW = _this3._getControllingSWIfCompatible();
- return _await(_this3._registerScript(), function (_this2$_registerScrip) {
- _this3._registration = _this2$_registerScrip;
-
-
- if (_this3._compatibleControllingSW) {
- _this3._sw = _this3._compatibleControllingSW;
- _this3._activeDeferred.resolve(_this3._compatibleControllingSW);
- _this3._controllingDeferred.resolve(_this3._compatibleControllingSW);
- _this3._compatibleControllingSW.addEventListener('statechange', _this3._onStateChange, {
- once: true
- });
- }
-
-
-
-
- var waitingSW = _this3._registration.waiting;
- if (waitingSW && urlsMatch(waitingSW.scriptURL, _this3._scriptURL.toString())) {
-
-
- _this3._sw = waitingSW;
-
- dontWaitFor(Promise.resolve().then(function () {
- _this3.dispatchEvent(new WorkboxEvent('waiting', {
- sw: waitingSW,
- wasWaitingBeforeRegister: true
- }));
- if ("dev" !== 'production') {
- logger.warn('A service worker was already waiting to activate ' + 'before this script was registered...');
- }
- }));
- }
- if (_this3._sw) {
- _this3._swDeferred.resolve(_this3._sw);
- _this3._ownSWs.add(_this3._sw);
- }
- if ("dev" !== 'production') {
- logger.log('Successfully registered service worker.', _this3._scriptURL.toString());
- if (navigator.serviceWorker.controller) {
- if (_this3._compatibleControllingSW) {
- logger.debug('A service worker with the same script URL ' + 'is already controlling this page.');
- } else {
- logger.debug('A service worker with a different script URL is ' + 'currently controlling the page. The browser is now fetching ' + 'the new script now...');
- }
- }
- var currentPageIsOutOfScope = function currentPageIsOutOfScope() {
- var scopeURL = new URL(_this3._registerOptions.scope || _this3._scriptURL.toString(), document.baseURI);
- var scopeURLBasePath = new URL('./', scopeURL.href).pathname;
- return !location.pathname.startsWith(scopeURLBasePath);
- };
- if (currentPageIsOutOfScope()) {
- logger.warn('The current page is not in scope for the registered ' + 'service worker. Was this a mistake?');
- }
- }
- _this3._registration.addEventListener('updatefound', _this3._onUpdateFound);
- navigator.serviceWorker.addEventListener('controllerchange', _this3._onControllerChange);
- return _this3._registration;
- });
- });
- } catch (e) {
- return Promise.reject(e);
- }
- }
-
- ;
- _proto.update = function update() {
- try {
- var _this5 = this;
- if (!_this5._registration) {
- if ("dev" !== 'production') {
- logger.error('Cannot update a Workbox instance without ' + 'being registered. Register the Workbox instance first.');
- }
- return;
- }
- return _awaitIgnored(_this5._registration.update());
- } catch (e) {
- return Promise.reject(e);
- }
- }
-
- ;
-
- _proto.getSW = function getSW() {
-
-
- return this._sw !== undefined ? Promise.resolve(this._sw) : this._swDeferred.promise;
- }
-
-
-
- ;
- _proto.messageSW = function messageSW$1(data) {
- try {
- var _this7 = this;
- return _await(_this7.getSW(), function (sw) {
- return messageSW(sw, data);
- });
- } catch (e) {
- return Promise.reject(e);
- }
- }
-
- ;
- _proto.messageSkipWaiting = function messageSkipWaiting() {
- if (this._registration && this._registration.waiting) {
- void messageSW(this._registration.waiting, SKIP_WAITING_MESSAGE);
- }
- }
-
- ;
- _proto._getControllingSWIfCompatible = function _getControllingSWIfCompatible() {
- var controller = navigator.serviceWorker.controller;
- if (controller && urlsMatch(controller.scriptURL, this._scriptURL.toString())) {
- return controller;
- } else {
- return undefined;
- }
- }
-
- ;
- _proto._registerScript = function _registerScript() {
- try {
- var _this9 = this;
- return _catch(function () {
-
-
-
- return _await(navigator.serviceWorker.register(_this9._scriptURL, _this9._registerOptions), function (reg) {
-
-
-
- _this9._registrationTime = performance.now();
- return reg;
- });
- }, function (error) {
- if ("dev" !== 'production') {
- logger.error(error);
- }
- throw error;
- });
- } catch (e) {
- return Promise.reject(e);
- }
- };
- _createClass(Workbox, [{
- key: "active",
- get: function get() {
- return this._activeDeferred.promise;
- }
-
- }, {
- key: "controlling",
- get: function get() {
- return this._controllingDeferred.promise;
- }
- }]);
- return Workbox;
- }(WorkboxEventTarget);
- function _invoke(body, then) {
- var result = body();
- if (result && result.then) {
- return result.then(then);
- }
- return then(result);
- }
- function _catch(body, recover) {
- try {
- var result = body();
- } catch (e) {
- return recover(e);
- }
- if (result && result.then) {
- return result.then(void 0, recover);
- }
- return result;
- }
- export { Workbox, WorkboxEvent, messageSW };
|