Workbox.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. Copyright 2019 Google LLC
  3. Use of this source code is governed by an MIT-style
  4. license that can be found in the LICENSE file or at
  5. https://opensource.org/licenses/MIT.
  6. */
  7. import { Deferred } from 'workbox-core/_private/Deferred.js';
  8. import { dontWaitFor } from 'workbox-core/_private/dontWaitFor.js';
  9. import { logger } from 'workbox-core/_private/logger.js';
  10. import { messageSW } from './messageSW.js';
  11. import { WorkboxEventTarget } from './utils/WorkboxEventTarget.js';
  12. import { urlsMatch } from './utils/urlsMatch.js';
  13. import { WorkboxEvent } from './utils/WorkboxEvent.js';
  14. import './_version.js';
  15. // The time a SW must be in the waiting phase before we can conclude
  16. // `skipWaiting()` wasn't called. This 200 amount wasn't scientifically
  17. // chosen, but it seems to avoid false positives in my testing.
  18. const WAITING_TIMEOUT_DURATION = 200;
  19. // The amount of time after a registration that we can reasonably conclude
  20. // that the registration didn't trigger an update.
  21. const REGISTRATION_TIMEOUT_DURATION = 60000;
  22. /**
  23. * A class to aid in handling service worker registration, updates, and
  24. * reacting to service worker lifecycle events.
  25. *
  26. * @fires [message]{@link module:workbox-window.Workbox#message}
  27. * @fires [installed]{@link module:workbox-window.Workbox#installed}
  28. * @fires [waiting]{@link module:workbox-window.Workbox#waiting}
  29. * @fires [controlling]{@link module:workbox-window.Workbox#controlling}
  30. * @fires [activated]{@link module:workbox-window.Workbox#activated}
  31. * @fires [redundant]{@link module:workbox-window.Workbox#redundant}
  32. * @fires [externalinstalled]{@link module:workbox-window.Workbox#externalinstalled}
  33. * @fires [externalwaiting]{@link module:workbox-window.Workbox#externalwaiting}
  34. * @fires [externalactivated]{@link module:workbox-window.Workbox#externalactivated}
  35. * @memberof module:workbox-window
  36. */
  37. class Workbox extends WorkboxEventTarget {
  38. /**
  39. * Creates a new Workbox instance with a script URL and service worker
  40. * options. The script URL and options are the same as those used when
  41. * calling `navigator.serviceWorker.register(scriptURL, options)`. See:
  42. * https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register
  43. *
  44. * @param {string} scriptURL The service worker script associated with this
  45. * instance.
  46. * @param {Object} [registerOptions] The service worker options associated
  47. * with this instance.
  48. */
  49. constructor(scriptURL, registerOptions = {}) {
  50. super();
  51. this._registerOptions = {};
  52. this._updateFoundCount = 0;
  53. // Deferreds we can resolve later.
  54. this._swDeferred = new Deferred();
  55. this._activeDeferred = new Deferred();
  56. this._controllingDeferred = new Deferred();
  57. this._registrationTime = 0;
  58. this._ownSWs = new Set();
  59. /**
  60. * @private
  61. */
  62. this._onUpdateFound = () => {
  63. // `this._registration` will never be `undefined` after an update is found.
  64. const registration = this._registration;
  65. const installingSW = registration.installing;
  66. // If the script URL passed to `navigator.serviceWorker.register()` is
  67. // different from the current controlling SW's script URL, we know any
  68. // successful registration calls will trigger an `updatefound` event.
  69. // But if the registered script URL is the same as the current controlling
  70. // SW's script URL, we'll only get an `updatefound` event if the file
  71. // changed since it was last registered. This can be a problem if the user
  72. // opens up the same page in a different tab, and that page registers
  73. // a SW that triggers an update. It's a problem because this page has no
  74. // good way of knowing whether the `updatefound` event came from the SW
  75. // script it registered or from a registration attempt made by a newer
  76. // version of the page running in another tab.
  77. // To minimize the possibility of a false positive, we use the logic here:
  78. const updateLikelyTriggeredExternally =
  79. // Since we enforce only calling `register()` once, and since we don't
  80. // add the `updatefound` event listener until the `register()` call, if
  81. // `_updateFoundCount` is > 0 then it means this method has already
  82. // been called, thus this SW must be external
  83. this._updateFoundCount > 0 ||
  84. // If the script URL of the installing SW is different from this
  85. // instance's script URL, we know it's definitely not from our
  86. // registration.
  87. !urlsMatch(installingSW.scriptURL, this._scriptURL) ||
  88. // If all of the above are false, then we use a time-based heuristic:
  89. // Any `updatefound` event that occurs long after our registration is
  90. // assumed to be external.
  91. (performance.now() >
  92. this._registrationTime + REGISTRATION_TIMEOUT_DURATION) ?
  93. // If any of the above are not true, we assume the update was
  94. // triggered by this instance.
  95. true : false;
  96. if (updateLikelyTriggeredExternally) {
  97. this._externalSW = installingSW;
  98. registration.removeEventListener('updatefound', this._onUpdateFound);
  99. }
  100. else {
  101. // If the update was not triggered externally we know the installing
  102. // SW is the one we registered, so we set it.
  103. this._sw = installingSW;
  104. this._ownSWs.add(installingSW);
  105. this._swDeferred.resolve(installingSW);
  106. // The `installing` state isn't something we have a dedicated
  107. // callback for, but we do log messages for it in development.
  108. if (process.env.NODE_ENV !== 'production') {
  109. if (navigator.serviceWorker.controller) {
  110. logger.log('Updated service worker found. Installing now...');
  111. }
  112. else {
  113. logger.log('Service worker is installing...');
  114. }
  115. }
  116. }
  117. // Increment the `updatefound` count, so future invocations of this
  118. // method can be sure they were triggered externally.
  119. ++this._updateFoundCount;
  120. // Add a `statechange` listener regardless of whether this update was
  121. // triggered externally, since we have callbacks for both.
  122. installingSW.addEventListener('statechange', this._onStateChange);
  123. };
  124. /**
  125. * @private
  126. * @param {Event} originalEvent
  127. */
  128. this._onStateChange = (originalEvent) => {
  129. // `this._registration` will never be `undefined` after an update is found.
  130. const registration = this._registration;
  131. const sw = originalEvent.target;
  132. const { state } = sw;
  133. const isExternal = sw === this._externalSW;
  134. const eventPrefix = isExternal ? 'external' : '';
  135. const eventProps = {
  136. sw,
  137. originalEvent
  138. };
  139. if (!isExternal && this._isUpdate) {
  140. eventProps.isUpdate = true;
  141. }
  142. this.dispatchEvent(new WorkboxEvent(eventPrefix + state, eventProps));
  143. if (state === 'installed') {
  144. // This timeout is used to ignore cases where the service worker calls
  145. // `skipWaiting()` in the install event, thus moving it directly in the
  146. // activating state. (Since all service workers *must* go through the
  147. // waiting phase, the only way to detect `skipWaiting()` called in the
  148. // install event is to observe that the time spent in the waiting phase
  149. // is very short.)
  150. // NOTE: we don't need separate timeouts for the own and external SWs
  151. // since they can't go through these phases at the same time.
  152. this._waitingTimeout = self.setTimeout(() => {
  153. // Ensure the SW is still waiting (it may now be redundant).
  154. if (state === 'installed' && registration.waiting === sw) {
  155. this.dispatchEvent(new WorkboxEvent(eventPrefix + 'waiting', eventProps));
  156. if (process.env.NODE_ENV !== 'production') {
  157. if (isExternal) {
  158. logger.warn('An external service worker has installed but is ' +
  159. 'waiting for this client to close before activating...');
  160. }
  161. else {
  162. logger.warn('The service worker has installed but is waiting ' +
  163. 'for existing clients to close before activating...');
  164. }
  165. }
  166. }
  167. }, WAITING_TIMEOUT_DURATION);
  168. }
  169. else if (state === 'activating') {
  170. clearTimeout(this._waitingTimeout);
  171. if (!isExternal) {
  172. this._activeDeferred.resolve(sw);
  173. }
  174. }
  175. if (process.env.NODE_ENV !== 'production') {
  176. switch (state) {
  177. case 'installed':
  178. if (isExternal) {
  179. logger.warn('An external service worker has installed. ' +
  180. 'You may want to suggest users reload this page.');
  181. }
  182. else {
  183. logger.log('Registered service worker installed.');
  184. }
  185. break;
  186. case 'activated':
  187. if (isExternal) {
  188. logger.warn('An external service worker has activated.');
  189. }
  190. else {
  191. logger.log('Registered service worker activated.');
  192. if (sw !== navigator.serviceWorker.controller) {
  193. logger.warn('The registered service worker is active but ' +
  194. 'not yet controlling the page. Reload or run ' +
  195. '`clients.claim()` in the service worker.');
  196. }
  197. }
  198. break;
  199. case 'redundant':
  200. if (sw === this._compatibleControllingSW) {
  201. logger.log('Previously controlling service worker now redundant!');
  202. }
  203. else if (!isExternal) {
  204. logger.log('Registered service worker now redundant!');
  205. }
  206. break;
  207. }
  208. }
  209. };
  210. /**
  211. * @private
  212. * @param {Event} originalEvent
  213. */
  214. this._onControllerChange = (originalEvent) => {
  215. const sw = this._sw;
  216. if (sw === navigator.serviceWorker.controller) {
  217. this.dispatchEvent(new WorkboxEvent('controlling', {
  218. sw,
  219. originalEvent,
  220. isUpdate: this._isUpdate,
  221. }));
  222. if (process.env.NODE_ENV !== 'production') {
  223. logger.log('Registered service worker now controlling this page.');
  224. }
  225. this._controllingDeferred.resolve(sw);
  226. }
  227. };
  228. /**
  229. * @private
  230. * @param {Event} originalEvent
  231. */
  232. this._onMessage = async (originalEvent) => {
  233. const { data, source } = originalEvent;
  234. // Wait until there's an "own" service worker. This is used to buffer
  235. // `message` events that may be received prior to calling `register()`.
  236. await this.getSW();
  237. // If the service worker that sent the message is in the list of own
  238. // service workers for this instance, dispatch a `message` event.
  239. // NOTE: we check for all previously owned service workers rather than
  240. // just the current one because some messages (e.g. cache updates) use
  241. // a timeout when sent and may be delayed long enough for a service worker
  242. // update to be found.
  243. if (this._ownSWs.has(source)) {
  244. this.dispatchEvent(new WorkboxEvent('message', {
  245. data,
  246. sw: source,
  247. originalEvent,
  248. }));
  249. }
  250. };
  251. this._scriptURL = scriptURL;
  252. this._registerOptions = registerOptions;
  253. // Add a message listener immediately since messages received during
  254. // page load are buffered only until the DOMContentLoaded event:
  255. // https://github.com/GoogleChrome/workbox/issues/2202
  256. navigator.serviceWorker.addEventListener('message', this._onMessage);
  257. }
  258. /**
  259. * Registers a service worker for this instances script URL and service
  260. * worker options. By default this method delays registration until after
  261. * the window has loaded.
  262. *
  263. * @param {Object} [options]
  264. * @param {Function} [options.immediate=false] Setting this to true will
  265. * register the service worker immediately, even if the window has
  266. * not loaded (not recommended).
  267. */
  268. async register({ immediate = false } = {}) {
  269. if (process.env.NODE_ENV !== 'production') {
  270. if (this._registrationTime) {
  271. logger.error('Cannot re-register a Workbox instance after it has ' +
  272. 'been registered. Create a new instance instead.');
  273. return;
  274. }
  275. }
  276. if (!immediate && document.readyState !== 'complete') {
  277. await new Promise((res) => window.addEventListener('load', res));
  278. }
  279. // Set this flag to true if any service worker was controlling the page
  280. // at registration time.
  281. this._isUpdate = Boolean(navigator.serviceWorker.controller);
  282. // Before registering, attempt to determine if a SW is already controlling
  283. // the page, and if that SW script (and version, if specified) matches this
  284. // instance's script.
  285. this._compatibleControllingSW = this._getControllingSWIfCompatible();
  286. this._registration = await this._registerScript();
  287. // If we have a compatible controller, store the controller as the "own"
  288. // SW, resolve active/controlling deferreds and add necessary listeners.
  289. if (this._compatibleControllingSW) {
  290. this._sw = this._compatibleControllingSW;
  291. this._activeDeferred.resolve(this._compatibleControllingSW);
  292. this._controllingDeferred.resolve(this._compatibleControllingSW);
  293. this._compatibleControllingSW.addEventListener('statechange', this._onStateChange, { once: true });
  294. }
  295. // If there's a waiting service worker with a matching URL before the
  296. // `updatefound` event fires, it likely means that this site is open
  297. // in another tab, or the user refreshed the page (and thus the previous
  298. // page wasn't fully unloaded before this page started loading).
  299. // https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#waiting
  300. const waitingSW = this._registration.waiting;
  301. if (waitingSW && urlsMatch(waitingSW.scriptURL, this._scriptURL)) {
  302. // Store the waiting SW as the "own" Sw, even if it means overwriting
  303. // a compatible controller.
  304. this._sw = waitingSW;
  305. // Run this in the next microtask, so any code that adds an event
  306. // listener after awaiting `register()` will get this event.
  307. dontWaitFor(Promise.resolve().then(() => {
  308. this.dispatchEvent(new WorkboxEvent('waiting', {
  309. sw: waitingSW,
  310. wasWaitingBeforeRegister: true,
  311. }));
  312. if (process.env.NODE_ENV !== 'production') {
  313. logger.warn('A service worker was already waiting to activate ' +
  314. 'before this script was registered...');
  315. }
  316. }));
  317. }
  318. // If an "own" SW is already set, resolve the deferred.
  319. if (this._sw) {
  320. this._swDeferred.resolve(this._sw);
  321. this._ownSWs.add(this._sw);
  322. }
  323. if (process.env.NODE_ENV !== 'production') {
  324. logger.log('Successfully registered service worker.', this._scriptURL);
  325. if (navigator.serviceWorker.controller) {
  326. if (this._compatibleControllingSW) {
  327. logger.debug('A service worker with the same script URL ' +
  328. 'is already controlling this page.');
  329. }
  330. else {
  331. logger.debug('A service worker with a different script URL is ' +
  332. 'currently controlling the page. The browser is now fetching ' +
  333. 'the new script now...');
  334. }
  335. }
  336. const currentPageIsOutOfScope = () => {
  337. const scopeURL = new URL(this._registerOptions.scope || this._scriptURL, document.baseURI);
  338. const scopeURLBasePath = new URL('./', scopeURL.href).pathname;
  339. return !location.pathname.startsWith(scopeURLBasePath);
  340. };
  341. if (currentPageIsOutOfScope()) {
  342. logger.warn('The current page is not in scope for the registered ' +
  343. 'service worker. Was this a mistake?');
  344. }
  345. }
  346. this._registration.addEventListener('updatefound', this._onUpdateFound);
  347. navigator.serviceWorker.addEventListener('controllerchange', this._onControllerChange, { once: true });
  348. return this._registration;
  349. }
  350. /**
  351. * Checks for updates of the registered service worker.
  352. */
  353. async update() {
  354. if (!this._registration) {
  355. if (process.env.NODE_ENV !== 'production') {
  356. logger.error('Cannot update a Workbox instance without ' +
  357. 'being registered. Register the Workbox instance first.');
  358. }
  359. return;
  360. }
  361. // Try to update registration
  362. await this._registration.update();
  363. }
  364. /**
  365. * Resolves to the service worker registered by this instance as soon as it
  366. * is active. If a service worker was already controlling at registration
  367. * time then it will resolve to that if the script URLs (and optionally
  368. * script versions) match, otherwise it will wait until an update is found
  369. * and activates.
  370. *
  371. * @return {Promise<ServiceWorker>}
  372. */
  373. get active() {
  374. return this._activeDeferred.promise;
  375. }
  376. /**
  377. * Resolves to the service worker registered by this instance as soon as it
  378. * is controlling the page. If a service worker was already controlling at
  379. * registration time then it will resolve to that if the script URLs (and
  380. * optionally script versions) match, otherwise it will wait until an update
  381. * is found and starts controlling the page.
  382. * Note: the first time a service worker is installed it will active but
  383. * not start controlling the page unless `clients.claim()` is called in the
  384. * service worker.
  385. *
  386. * @return {Promise<ServiceWorker>}
  387. */
  388. get controlling() {
  389. return this._controllingDeferred.promise;
  390. }
  391. /**
  392. * Resolves with a reference to a service worker that matches the script URL
  393. * of this instance, as soon as it's available.
  394. *
  395. * If, at registration time, there's already an active or waiting service
  396. * worker with a matching script URL, it will be used (with the waiting
  397. * service worker taking precedence over the active service worker if both
  398. * match, since the waiting service worker would have been registered more
  399. * recently).
  400. * If there's no matching active or waiting service worker at registration
  401. * time then the promise will not resolve until an update is found and starts
  402. * installing, at which point the installing service worker is used.
  403. *
  404. * @return {Promise<ServiceWorker>}
  405. */
  406. async getSW() {
  407. // If `this._sw` is set, resolve with that as we want `getSW()` to
  408. // return the correct (new) service worker if an update is found.
  409. return this._sw !== undefined ? this._sw : this._swDeferred.promise;
  410. }
  411. /**
  412. * Sends the passed data object to the service worker registered by this
  413. * instance (via [`getSW()`]{@link module:workbox-window.Workbox#getSW}) and resolves
  414. * with a response (if any).
  415. *
  416. * A response can be set in a message handler in the service worker by
  417. * calling `event.ports[0].postMessage(...)`, which will resolve the promise
  418. * returned by `messageSW()`. If no response is set, the promise will never
  419. * resolve.
  420. *
  421. * @param {Object} data An object to send to the service worker
  422. * @return {Promise<Object>}
  423. */
  424. async messageSW(data) {
  425. const sw = await this.getSW();
  426. return messageSW(sw, data);
  427. }
  428. /**
  429. * Checks for a service worker already controlling the page and returns
  430. * it if its script URL matches.
  431. *
  432. * @private
  433. * @return {ServiceWorker|undefined}
  434. */
  435. _getControllingSWIfCompatible() {
  436. const controller = navigator.serviceWorker.controller;
  437. if (controller && urlsMatch(controller.scriptURL, this._scriptURL)) {
  438. return controller;
  439. }
  440. else {
  441. return undefined;
  442. }
  443. }
  444. /**
  445. * Registers a service worker for this instances script URL and register
  446. * options and tracks the time registration was complete.
  447. *
  448. * @private
  449. */
  450. async _registerScript() {
  451. try {
  452. const reg = await navigator.serviceWorker.register(this._scriptURL, this._registerOptions);
  453. // Keep track of when registration happened, so it can be used in the
  454. // `this._onUpdateFound` heuristic. Also use the presence of this
  455. // property as a way to see if `.register()` has been called.
  456. this._registrationTime = performance.now();
  457. return reg;
  458. }
  459. catch (error) {
  460. if (process.env.NODE_ENV !== 'production') {
  461. logger.error(error);
  462. }
  463. // Re-throw the error.
  464. throw error;
  465. }
  466. }
  467. }
  468. export { Workbox };
  469. // The jsdoc comments below outline the events this instance may dispatch:
  470. // -----------------------------------------------------------------------
  471. /**
  472. * The `message` event is dispatched any time a `postMessage` is received.
  473. *
  474. * @event module:workbox-window.Workbox#message
  475. * @type {WorkboxEvent}
  476. * @property {*} data The `data` property from the original `message` event.
  477. * @property {Event} originalEvent The original [`message`]{@link https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent}
  478. * event.
  479. * @property {string} type `message`.
  480. * @property {Workbox} target The `Workbox` instance.
  481. */
  482. /**
  483. * The `installed` event is dispatched if the state of a
  484. * [`Workbox`]{@link module:workbox-window.Workbox} instance's
  485. * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
  486. * changes to `installed`.
  487. *
  488. * Then can happen either the very first time a service worker is installed,
  489. * or after an update to the current service worker is found. In the case
  490. * of an update being found, the event's `isUpdate` property will be `true`.
  491. *
  492. * @event module:workbox-window.Workbox#installed
  493. * @type {WorkboxEvent}
  494. * @property {ServiceWorker} sw The service worker instance.
  495. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  496. * event.
  497. * @property {boolean|undefined} isUpdate True if a service worker was already
  498. * controlling when this `Workbox` instance called `register()`.
  499. * @property {string} type `installed`.
  500. * @property {Workbox} target The `Workbox` instance.
  501. */
  502. /**
  503. * The `waiting` event is dispatched if the state of a
  504. * [`Workbox`]{@link module:workbox-window.Workbox} instance's
  505. * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
  506. * changes to `installed` and then doesn't immediately change to `activating`.
  507. * It may also be dispatched if a service worker with the same
  508. * [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
  509. * was already waiting when the [`register()`]{@link module:workbox-window.Workbox#register}
  510. * method was called.
  511. *
  512. * @event module:workbox-window.Workbox#waiting
  513. * @type {WorkboxEvent}
  514. * @property {ServiceWorker} sw The service worker instance.
  515. * @property {Event|undefined} originalEvent The original
  516. * [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  517. * event, or `undefined` in the case where the service worker was waiting
  518. * to before `.register()` was called.
  519. * @property {boolean|undefined} isUpdate True if a service worker was already
  520. * controlling when this `Workbox` instance called `register()`.
  521. * @property {boolean|undefined} wasWaitingBeforeRegister True if a service worker with
  522. * a matching `scriptURL` was already waiting when this `Workbox`
  523. * instance called `register()`.
  524. * @property {string} type `waiting`.
  525. * @property {Workbox} target The `Workbox` instance.
  526. */
  527. /**
  528. * The `controlling` event is dispatched if a
  529. * [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
  530. * fires on the service worker [container]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer}
  531. * and the [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
  532. * of the new [controller]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/controller}
  533. * matches the `scriptURL` of the `Workbox` instance's
  534. * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}.
  535. *
  536. * @event module:workbox-window.Workbox#controlling
  537. * @type {WorkboxEvent}
  538. * @property {ServiceWorker} sw The service worker instance.
  539. * @property {Event} originalEvent The original [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
  540. * event.
  541. * @property {boolean|undefined} isUpdate True if a service worker was already
  542. * controlling when this service worker was registered.
  543. * @property {string} type `controlling`.
  544. * @property {Workbox} target The `Workbox` instance.
  545. */
  546. /**
  547. * The `activated` event is dispatched if the state of a
  548. * [`Workbox`]{@link module:workbox-window.Workbox} instance's
  549. * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
  550. * changes to `activated`.
  551. *
  552. * @event module:workbox-window.Workbox#activated
  553. * @type {WorkboxEvent}
  554. * @property {ServiceWorker} sw The service worker instance.
  555. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  556. * event.
  557. * @property {boolean|undefined} isUpdate True if a service worker was already
  558. * controlling when this `Workbox` instance called `register()`.
  559. * @property {string} type `activated`.
  560. * @property {Workbox} target The `Workbox` instance.
  561. */
  562. /**
  563. * The `redundant` event is dispatched if the state of a
  564. * [`Workbox`]{@link module:workbox-window.Workbox} instance's
  565. * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
  566. * changes to `redundant`.
  567. *
  568. * @event module:workbox-window.Workbox#redundant
  569. * @type {WorkboxEvent}
  570. * @property {ServiceWorker} sw The service worker instance.
  571. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  572. * event.
  573. * @property {boolean|undefined} isUpdate True if a service worker was already
  574. * controlling when this `Workbox` instance called `register()`.
  575. * @property {string} type `redundant`.
  576. * @property {Workbox} target The `Workbox` instance.
  577. */
  578. /**
  579. * The `externalinstalled` event is dispatched if the state of an
  580. * [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}
  581. * changes to `installed`.
  582. *
  583. * @event module:workbox-window.Workbox#externalinstalled
  584. * @type {WorkboxEvent}
  585. * @property {ServiceWorker} sw The service worker instance.
  586. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  587. * event.
  588. * @property {string} type `externalinstalled`.
  589. * @property {Workbox} target The `Workbox` instance.
  590. */
  591. /**
  592. * The `externalwaiting` event is dispatched if the state of an
  593. * [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}
  594. * changes to `waiting`.
  595. *
  596. * @event module:workbox-window.Workbox#externalwaiting
  597. * @type {WorkboxEvent}
  598. * @property {ServiceWorker} sw The service worker instance.
  599. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  600. * event.
  601. * @property {string} type `externalwaiting`.
  602. * @property {Workbox} target The `Workbox` instance.
  603. */
  604. /**
  605. * The `externalactivated` event is dispatched if the state of an
  606. * [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}
  607. * changes to `activated`.
  608. *
  609. * @event module:workbox-window.Workbox#externalactivated
  610. * @type {WorkboxEvent}
  611. * @property {ServiceWorker} sw The service worker instance.
  612. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  613. * event.
  614. * @property {string} type `externalactivated`.
  615. * @property {Workbox} target The `Workbox` instance.
  616. */