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

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.2966299016",
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.2966299016", "build":"2966299016", "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,13 +217,19 @@ 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
+ // 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);
232
+ }
227
233
  // The page is not currently controlled by a service worker.
228
234
  // This can happen, for example, after a force refresh (Ctrl+F5), even though
229
235
  // an active service worker already exists. In that case, the service worker's
@@ -247,4 +253,41 @@ export default class GeoGirafeApp {
247
253
  }, { once: true });
248
254
  });
249
255
  }
256
+ async registerServiceWorker() {
257
+ // The default scope of the service worker is the folder of service-worker.js (resolved against <base href>).
258
+ // When the application page is served from another path than its static files
259
+ // (e.g. page at /iframe and files at /static/), the page is outside of this scope and can't be controlled.
260
+ // In that case, try to register the service worker at path of the page as scope.
261
+ // This is only allowed by the browser if the server sends the "Service-Worker-Allowed" header.
262
+ const scriptUrl = new URL('service-worker.js', document.baseURI);
263
+ const defaultScope = new URL('./', scriptUrl).href;
264
+ if (!globalThis.location.href.startsWith(defaultScope)) {
265
+ const pageScope = new URL('./', globalThis.location.href).href;
266
+ try {
267
+ return await navigator.serviceWorker.register(scriptUrl, { type: 'module', scope: pageScope });
268
+ }
269
+ catch (err) {
270
+ console.warn(`Service worker could not be registered with the scope ${pageScope}. ` +
271
+ `Check that the server sends the header "Service-Worker-Allowed" with the service worker script.`, err);
272
+ }
273
+ }
274
+ return navigator.serviceWorker.register(scriptUrl, { type: 'module' });
275
+ }
276
+ async waitForServiceWorkerActivation(registration) {
277
+ if (registration.active) {
278
+ return registration.active;
279
+ }
280
+ const sw = registration.installing ?? registration.waiting;
281
+ if (!sw) {
282
+ throw new Error('No service worker is installing or waiting.');
283
+ }
284
+ return new Promise((resolve) => {
285
+ sw.addEventListener('statechange', function onStateChange() {
286
+ if (sw.state === 'activated') {
287
+ sw.removeEventListener('statechange', onStateChange);
288
+ resolve(sw);
289
+ }
290
+ });
291
+ });
292
+ }
250
293
  }