@geogirafe/lib-geoportal 1.3.0-dev.2938437315 → 1.3.0-dev.2966740745

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "name": "GeoGirafe PSC",
6
6
  "url": "https://doc.geogirafe.org"
7
7
  },
8
- "version": "1.3.0-dev.2938437315",
8
+ "version": "1.3.0-dev.2966740745",
9
9
  "type": "module",
10
10
  "engines": {
11
11
  "node": ">=20.19.0"
@@ -1 +1 @@
1
- {"version":"1.3.0-dev.2938437315", "build":"2938437315", "date":"12/10/2026"}
1
+ {"version":"1.3.0-dev.2966740745", "build":"2966740745", "date":"20/10/2026"}
@@ -14,4 +14,6 @@ export default class GeoGirafeApp {
14
14
  protected defineCoreComponents(): void;
15
15
  private initializeServiceWorker;
16
16
  private waitForServiceWorkerControl;
17
+ private registerServiceWorker;
18
+ private waitForServiceWorkerActivation;
17
19
  }
@@ -217,23 +217,18 @@ export default class GeoGirafeApp {
217
217
  }
218
218
  async waitForServiceWorkerControl() {
219
219
  const sw = navigator.serviceWorker;
220
- const registration = await sw.register('service-worker.js', { type: 'module' });
220
+ const registration = await this.registerServiceWorker();
221
221
  // If a service worker already controls the current page, everything is ready.
222
222
  // `registration.active` only tells us that a service worker is active for the registration;
223
223
  // `sw.controller` tells us that it actually controls this page.
224
224
  if (sw.controller) {
225
225
  return sw.controller;
226
226
  }
227
- // The page is not currently controlled by a service worker.
228
- // This can happen, for example, after a force refresh (Ctrl+F5), even though
229
- // an active service worker already exists. In that case, the service worker's
230
- // `activate` event is not triggered again, so the `clients.claim()` call from
231
- // the activation phase cannot take control of this newly loaded page.
232
- // If an active service worker already exists, explicitly ask it to claim the
233
- // current page. The service worker handles this message by calling `clients.claim()`.
234
- const active = registration.active;
235
- if (active) {
236
- active.postMessage({ type: 'claim' });
227
+ // If the page is still outside of the service worker scope (see registerServiceWorker),
228
+ // the `controllerchange` event will never be fired, so we only wait for the service worker to be activated.
229
+ if (!globalThis.location.href.startsWith(registration.scope)) {
230
+ console.warn(`The current page is outside of the service worker scope (${registration.scope}), it won't be controlled by the service worker.`);
231
+ return this.waitForServiceWorkerActivation(registration);
237
232
  }
238
233
  // Wait until the service worker takes control of the current page.
239
234
  // Once `clients.claim()` succeeds, the browser updates `sw.controller` and
@@ -245,6 +240,60 @@ export default class GeoGirafeApp {
245
240
  resolve(controller);
246
241
  }
247
242
  }, { once: true });
243
+ // The page is not currently controlled by a service worker.
244
+ // This can happen, for example, after a force refresh (Ctrl+F5), even though
245
+ // an active service worker already exists. In that case, the service worker's
246
+ // `activate` event is not triggered again, so the `clients.claim()` call from
247
+ // the activation phase cannot take control of this newly loaded page.
248
+ // If an active service worker already exists, explicitly ask it to claim the
249
+ // current page. The service worker handles this message by calling `clients.claim()`.
250
+ const active = registration.active;
251
+ if (active) {
252
+ active.postMessage({ type: 'claim' });
253
+ }
254
+ // Safety check: This is exactly the same test as the begining of the method
255
+ // but the worker may have claimed the page before the listener
256
+ // above was registered, in which case `controllerchange` never fires.
257
+ if (sw.controller) {
258
+ resolve(sw.controller);
259
+ }
260
+ });
261
+ }
262
+ async registerServiceWorker() {
263
+ // The default scope of the service worker is the folder of service-worker.js (resolved against <base href>).
264
+ // When the application page is served from another path than its static files
265
+ // (e.g. page at /iframe and files at /static/), the page is outside of this scope and can't be controlled.
266
+ // In that case, try to register the service worker at path of the page as scope.
267
+ // This is only allowed by the browser if the server sends the "Service-Worker-Allowed" header.
268
+ const scriptUrl = new URL('service-worker.js', document.baseURI);
269
+ const defaultScope = new URL('./', scriptUrl).href;
270
+ if (!globalThis.location.href.startsWith(defaultScope)) {
271
+ const pageScope = new URL('./', globalThis.location.href).href;
272
+ try {
273
+ return await navigator.serviceWorker.register(scriptUrl, { type: 'module', scope: pageScope });
274
+ }
275
+ catch (err) {
276
+ console.warn(`Service worker could not be registered with the scope ${pageScope}. ` +
277
+ `Check that the server sends the header "Service-Worker-Allowed" with the service worker script.`, err);
278
+ }
279
+ }
280
+ return navigator.serviceWorker.register(scriptUrl, { type: 'module' });
281
+ }
282
+ async waitForServiceWorkerActivation(registration) {
283
+ if (registration.active) {
284
+ return registration.active;
285
+ }
286
+ const sw = registration.installing ?? registration.waiting;
287
+ if (!sw) {
288
+ throw new Error('No service worker is installing or waiting.');
289
+ }
290
+ return new Promise((resolve) => {
291
+ sw.addEventListener('statechange', function onStateChange() {
292
+ if (sw.state === 'activated') {
293
+ sw.removeEventListener('statechange', onStateChange);
294
+ resolve(sw);
295
+ }
296
+ });
248
297
  });
249
298
  }
250
299
  }