Workbox.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { WorkboxEventTarget } from './utils/WorkboxEventTarget.js';
  2. import './_version.js';
  3. /**
  4. * A class to aid in handling service worker registration, updates, and
  5. * reacting to service worker lifecycle events.
  6. *
  7. * @fires [message]{@link module:workbox-window.Workbox#message}
  8. * @fires [installed]{@link module:workbox-window.Workbox#installed}
  9. * @fires [waiting]{@link module:workbox-window.Workbox#waiting}
  10. * @fires [controlling]{@link module:workbox-window.Workbox#controlling}
  11. * @fires [activated]{@link module:workbox-window.Workbox#activated}
  12. * @fires [redundant]{@link module:workbox-window.Workbox#redundant}
  13. * @fires [externalinstalled]{@link module:workbox-window.Workbox#externalinstalled}
  14. * @fires [externalwaiting]{@link module:workbox-window.Workbox#externalwaiting}
  15. * @fires [externalactivated]{@link module:workbox-window.Workbox#externalactivated}
  16. * @memberof module:workbox-window
  17. */
  18. declare class Workbox extends WorkboxEventTarget {
  19. private readonly _scriptURL;
  20. private readonly _registerOptions;
  21. private _updateFoundCount;
  22. private readonly _swDeferred;
  23. private readonly _activeDeferred;
  24. private readonly _controllingDeferred;
  25. private _registrationTime;
  26. private _isUpdate?;
  27. private _compatibleControllingSW?;
  28. private _registration?;
  29. private _sw?;
  30. private readonly _ownSWs;
  31. private _externalSW?;
  32. private _waitingTimeout?;
  33. /**
  34. * Creates a new Workbox instance with a script URL and service worker
  35. * options. The script URL and options are the same as those used when
  36. * calling `navigator.serviceWorker.register(scriptURL, options)`. See:
  37. * https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register
  38. *
  39. * @param {string} scriptURL The service worker script associated with this
  40. * instance.
  41. * @param {Object} [registerOptions] The service worker options associated
  42. * with this instance.
  43. */
  44. constructor(scriptURL: string, registerOptions?: {});
  45. /**
  46. * Registers a service worker for this instances script URL and service
  47. * worker options. By default this method delays registration until after
  48. * the window has loaded.
  49. *
  50. * @param {Object} [options]
  51. * @param {Function} [options.immediate=false] Setting this to true will
  52. * register the service worker immediately, even if the window has
  53. * not loaded (not recommended).
  54. */
  55. register({ immediate }?: {
  56. immediate?: boolean | undefined;
  57. }): Promise<ServiceWorkerRegistration | undefined>;
  58. /**
  59. * Checks for updates of the registered service worker.
  60. */
  61. update(): Promise<void>;
  62. /**
  63. * Resolves to the service worker registered by this instance as soon as it
  64. * is active. If a service worker was already controlling at registration
  65. * time then it will resolve to that if the script URLs (and optionally
  66. * script versions) match, otherwise it will wait until an update is found
  67. * and activates.
  68. *
  69. * @return {Promise<ServiceWorker>}
  70. */
  71. get active(): Promise<ServiceWorker>;
  72. /**
  73. * Resolves to the service worker registered by this instance as soon as it
  74. * is controlling the page. If a service worker was already controlling at
  75. * registration time then it will resolve to that if the script URLs (and
  76. * optionally script versions) match, otherwise it will wait until an update
  77. * is found and starts controlling the page.
  78. * Note: the first time a service worker is installed it will active but
  79. * not start controlling the page unless `clients.claim()` is called in the
  80. * service worker.
  81. *
  82. * @return {Promise<ServiceWorker>}
  83. */
  84. get controlling(): Promise<ServiceWorker>;
  85. /**
  86. * Resolves with a reference to a service worker that matches the script URL
  87. * of this instance, as soon as it's available.
  88. *
  89. * If, at registration time, there's already an active or waiting service
  90. * worker with a matching script URL, it will be used (with the waiting
  91. * service worker taking precedence over the active service worker if both
  92. * match, since the waiting service worker would have been registered more
  93. * recently).
  94. * If there's no matching active or waiting service worker at registration
  95. * time then the promise will not resolve until an update is found and starts
  96. * installing, at which point the installing service worker is used.
  97. *
  98. * @return {Promise<ServiceWorker>}
  99. */
  100. getSW(): Promise<ServiceWorker>;
  101. /**
  102. * Sends the passed data object to the service worker registered by this
  103. * instance (via [`getSW()`]{@link module:workbox-window.Workbox#getSW}) and resolves
  104. * with a response (if any).
  105. *
  106. * A response can be set in a message handler in the service worker by
  107. * calling `event.ports[0].postMessage(...)`, which will resolve the promise
  108. * returned by `messageSW()`. If no response is set, the promise will never
  109. * resolve.
  110. *
  111. * @param {Object} data An object to send to the service worker
  112. * @return {Promise<Object>}
  113. */
  114. messageSW(data: object): Promise<any>;
  115. /**
  116. * Checks for a service worker already controlling the page and returns
  117. * it if its script URL matches.
  118. *
  119. * @private
  120. * @return {ServiceWorker|undefined}
  121. */
  122. private _getControllingSWIfCompatible;
  123. /**
  124. * Registers a service worker for this instances script URL and register
  125. * options and tracks the time registration was complete.
  126. *
  127. * @private
  128. */
  129. private _registerScript;
  130. /**
  131. * @private
  132. */
  133. private readonly _onUpdateFound;
  134. /**
  135. * @private
  136. * @param {Event} originalEvent
  137. */
  138. private readonly _onStateChange;
  139. /**
  140. * @private
  141. * @param {Event} originalEvent
  142. */
  143. private readonly _onControllerChange;
  144. /**
  145. * @private
  146. * @param {Event} originalEvent
  147. */
  148. private readonly _onMessage;
  149. }
  150. export { Workbox };
  151. /**
  152. * The `message` event is dispatched any time a `postMessage` is received.
  153. *
  154. * @event module:workbox-window.Workbox#message
  155. * @type {WorkboxEvent}
  156. * @property {*} data The `data` property from the original `message` event.
  157. * @property {Event} originalEvent The original [`message`]{@link https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent}
  158. * event.
  159. * @property {string} type `message`.
  160. * @property {Workbox} target The `Workbox` instance.
  161. */
  162. /**
  163. * The `installed` event is dispatched if the state of a
  164. * [`Workbox`]{@link module:workbox-window.Workbox} instance's
  165. * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
  166. * changes to `installed`.
  167. *
  168. * Then can happen either the very first time a service worker is installed,
  169. * or after an update to the current service worker is found. In the case
  170. * of an update being found, the event's `isUpdate` property will be `true`.
  171. *
  172. * @event module:workbox-window.Workbox#installed
  173. * @type {WorkboxEvent}
  174. * @property {ServiceWorker} sw The service worker instance.
  175. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  176. * event.
  177. * @property {boolean|undefined} isUpdate True if a service worker was already
  178. * controlling when this `Workbox` instance called `register()`.
  179. * @property {string} type `installed`.
  180. * @property {Workbox} target The `Workbox` instance.
  181. */
  182. /**
  183. * The `waiting` event is dispatched if the state of a
  184. * [`Workbox`]{@link module:workbox-window.Workbox} instance's
  185. * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
  186. * changes to `installed` and then doesn't immediately change to `activating`.
  187. * It may also be dispatched if a service worker with the same
  188. * [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
  189. * was already waiting when the [`register()`]{@link module:workbox-window.Workbox#register}
  190. * method was called.
  191. *
  192. * @event module:workbox-window.Workbox#waiting
  193. * @type {WorkboxEvent}
  194. * @property {ServiceWorker} sw The service worker instance.
  195. * @property {Event|undefined} originalEvent The original
  196. * [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  197. * event, or `undefined` in the case where the service worker was waiting
  198. * to before `.register()` was called.
  199. * @property {boolean|undefined} isUpdate True if a service worker was already
  200. * controlling when this `Workbox` instance called `register()`.
  201. * @property {boolean|undefined} wasWaitingBeforeRegister True if a service worker with
  202. * a matching `scriptURL` was already waiting when this `Workbox`
  203. * instance called `register()`.
  204. * @property {string} type `waiting`.
  205. * @property {Workbox} target The `Workbox` instance.
  206. */
  207. /**
  208. * The `controlling` event is dispatched if a
  209. * [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
  210. * fires on the service worker [container]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer}
  211. * and the [`scriptURL`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/scriptURL}
  212. * of the new [controller]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/controller}
  213. * matches the `scriptURL` of the `Workbox` instance's
  214. * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}.
  215. *
  216. * @event module:workbox-window.Workbox#controlling
  217. * @type {WorkboxEvent}
  218. * @property {ServiceWorker} sw The service worker instance.
  219. * @property {Event} originalEvent The original [`controllerchange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/oncontrollerchange}
  220. * event.
  221. * @property {boolean|undefined} isUpdate True if a service worker was already
  222. * controlling when this service worker was registered.
  223. * @property {string} type `controlling`.
  224. * @property {Workbox} target The `Workbox` instance.
  225. */
  226. /**
  227. * The `activated` event is dispatched if the state of a
  228. * [`Workbox`]{@link module:workbox-window.Workbox} instance's
  229. * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
  230. * changes to `activated`.
  231. *
  232. * @event module:workbox-window.Workbox#activated
  233. * @type {WorkboxEvent}
  234. * @property {ServiceWorker} sw The service worker instance.
  235. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  236. * event.
  237. * @property {boolean|undefined} isUpdate True if a service worker was already
  238. * controlling when this `Workbox` instance called `register()`.
  239. * @property {string} type `activated`.
  240. * @property {Workbox} target The `Workbox` instance.
  241. */
  242. /**
  243. * The `redundant` event is dispatched if the state of a
  244. * [`Workbox`]{@link module:workbox-window.Workbox} instance's
  245. * [registered service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-precaching#def-registered-sw}
  246. * changes to `redundant`.
  247. *
  248. * @event module:workbox-window.Workbox#redundant
  249. * @type {WorkboxEvent}
  250. * @property {ServiceWorker} sw The service worker instance.
  251. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  252. * event.
  253. * @property {boolean|undefined} isUpdate True if a service worker was already
  254. * controlling when this `Workbox` instance called `register()`.
  255. * @property {string} type `redundant`.
  256. * @property {Workbox} target The `Workbox` instance.
  257. */
  258. /**
  259. * The `externalinstalled` event is dispatched if the state of an
  260. * [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}
  261. * changes to `installed`.
  262. *
  263. * @event module:workbox-window.Workbox#externalinstalled
  264. * @type {WorkboxEvent}
  265. * @property {ServiceWorker} sw The service worker instance.
  266. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  267. * event.
  268. * @property {string} type `externalinstalled`.
  269. * @property {Workbox} target The `Workbox` instance.
  270. */
  271. /**
  272. * The `externalwaiting` event is dispatched if the state of an
  273. * [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}
  274. * changes to `waiting`.
  275. *
  276. * @event module:workbox-window.Workbox#externalwaiting
  277. * @type {WorkboxEvent}
  278. * @property {ServiceWorker} sw The service worker instance.
  279. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  280. * event.
  281. * @property {string} type `externalwaiting`.
  282. * @property {Workbox} target The `Workbox` instance.
  283. */
  284. /**
  285. * The `externalactivated` event is dispatched if the state of an
  286. * [external service worker]{@link https://developers.google.com/web/tools/workbox/modules/workbox-window#when_an_unexpected_version_of_the_service_worker_is_found}
  287. * changes to `activated`.
  288. *
  289. * @event module:workbox-window.Workbox#externalactivated
  290. * @type {WorkboxEvent}
  291. * @property {ServiceWorker} sw The service worker instance.
  292. * @property {Event} originalEvent The original [`statechange`]{@link https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker/onstatechange}
  293. * event.
  294. * @property {string} type `externalactivated`.
  295. * @property {Workbox} target The `Workbox` instance.
  296. */