@geogirafe/lib-geoportal 1.2.0-dev.2896420147 → 1.2.0-dev.2897830140

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.2.0-dev.2896420147",
8
+ "version": "1.2.0-dev.2897830140",
9
9
  "type": "module",
10
10
  "engines": {
11
11
  "node": ">=20.19.0"
@@ -1 +1 @@
1
- {"version":"1.2.0-dev.2896420147", "build":"2896420147", "date":"30/09/2026"}
1
+ {"version":"1.2.0-dev.2897830140", "build":"2897830140", "date":"30/09/2026"}
@@ -13,5 +13,5 @@ export default class GeoGirafeApp {
13
13
  protected addCustomSerializers(): void;
14
14
  protected defineCoreComponents(): void;
15
15
  private initializeServiceWorker;
16
- private waitForServiceWorkerActivation;
16
+ private waitForServiceWorkerControl;
17
17
  }
@@ -175,7 +175,7 @@ export default class GeoGirafeApp {
175
175
  }
176
176
  let sw;
177
177
  try {
178
- sw = await this.waitForServiceWorkerActivation();
178
+ sw = await this.waitForServiceWorkerControl();
179
179
  }
180
180
  catch (err) {
181
181
  throw new Error(`Service worker registration failed. ${err}`, { cause: err });
@@ -213,22 +213,36 @@ export default class GeoGirafeApp {
213
213
  this.context.stateManager.state.application.isAuthInitialized = true;
214
214
  }
215
215
  }
216
- async waitForServiceWorkerActivation() {
217
- const registration = await navigator.serviceWorker.register('service-worker.js', { type: 'module' });
218
- if (registration.active) {
219
- return registration.active;
216
+ async waitForServiceWorkerControl() {
217
+ const sw = navigator.serviceWorker;
218
+ const registration = await sw.register('service-worker.js', { type: 'module' });
219
+ // If a service worker already controls the current page, everything is ready.
220
+ // `registration.active` only tells us that a service worker is active for the registration;
221
+ // `sw.controller` tells us that it actually controls this page.
222
+ if (sw.controller) {
223
+ return sw.controller;
220
224
  }
221
- const sw = registration.installing || registration.waiting;
222
- if (!sw) {
223
- throw new Error('No service worker is installing or waiting.');
225
+ // The page is not currently controlled by a service worker.
226
+ // This can happen, for example, after a force refresh (Ctrl+F5), even though
227
+ // an active service worker already exists. In that case, the service worker's
228
+ // `activate` event is not triggered again, so the `clients.claim()` call from
229
+ // the activation phase cannot take control of this newly loaded page.
230
+ // If an active service worker already exists, explicitly ask it to claim the
231
+ // current page. The service worker handles this message by calling `clients.claim()`.
232
+ const active = registration.active;
233
+ if (active) {
234
+ active.postMessage({ type: 'claim' });
224
235
  }
236
+ // Wait until the service worker takes control of the current page.
237
+ // Once `clients.claim()` succeeds, the browser updates `sw.controller` and
238
+ // dispatches the `controllerchange` event.
225
239
  return new Promise((resolve) => {
226
- sw.addEventListener('statechange', function onStateChange() {
227
- if (sw.state === 'activated') {
228
- sw.removeEventListener('statechange', onStateChange);
229
- resolve(sw);
240
+ sw.addEventListener('controllerchange', () => {
241
+ const controller = navigator.serviceWorker.controller;
242
+ if (controller) {
243
+ resolve(controller);
230
244
  }
231
- });
245
+ }, { once: true });
232
246
  });
233
247
  }
234
248
  }
@@ -37,6 +37,10 @@ sw.addEventListener('fetch', (event) => {
37
37
  event.respondWith(handleFetchEvent(event));
38
38
  });
39
39
  function handleMessage(event) {
40
+ if (event.data?.type === 'claim') {
41
+ event.waitUntil(sw.clients.claim());
42
+ return;
43
+ }
40
44
  if (!swHelper.isOriginAllowed(self.location.origin, event.origin)) {
41
45
  console.warn(`Message ignored: origin not allowed (${event.origin})`);
42
46
  return;
@@ -64,11 +68,6 @@ function handleActivate(event) {
64
68
  if ('clients' in sw) {
65
69
  await swHelperReady;
66
70
  await sw.clients.claim();
67
- const clientsList = await sw.clients.matchAll({ type: 'window' });
68
- for (const client of clientsList) {
69
- client.navigate(client.url);
70
- log('Page reloaded by the service worker.');
71
- }
72
71
  }
73
72
  })());
74
73
  }