@geogirafe/lib-geoportal 1.1.0-dev.2602404359 → 1.1.0-dev.2610902439
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/components/about/component.js +1 -1
- package/components/getdirections/component.js +1 -0
- package/components/prototypebanner/component.js +1 -1
- package/components/treeview/treeviewroot/style.css +1 -1
- package/package.json +4 -3
- package/templates/index.html +1 -1
- package/templates/public/about.json +1 -1
- package/templates/tsconfig.json +1 -1
- package/templates/vite.config.js +1 -1
- package/tools/app/geogirafeapp.js +15 -6
- package/tools/configuration/girafeconfig.d.ts +2 -0
- package/tools/configuration/girafeconfig.js +23 -21
- package/tools/i18n/i18nmanager.js +1 -1
- package/tools/main.d.ts +2 -0
- package/tools/main.js +1 -0
- package/tools/offline/offlinemanager.d.ts +5 -4
- package/tools/offline/offlinemanager.js +50 -80
- package/tools/sw/service-worker.d.ts +1 -0
- package/tools/sw/service-worker.js +94 -0
- package/tools/sw/service-worker.tools.d.ts +83 -0
- package/tools/sw/service-worker.tools.js +321 -0
- package/tools/tests/mockconfig.d.ts +1 -1
- package/tools/tests/mockconfig.js +1 -1
- package/service-worker.js +0 -331
package/service-worker.js
DELETED
|
@@ -1,331 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
/**
|
|
4
|
-
* To allow offline mode, the first 300 queries made by the application will be cached.
|
|
5
|
-
* We could have implemented a more specific cache with file extensions for example
|
|
6
|
-
* But then we should define exactly what should be cached.
|
|
7
|
-
* This can be quite complex, because we cannot just cache all images,
|
|
8
|
-
* as some of them are WMTS or WMS results and others are icons.
|
|
9
|
-
* So a simple solution here is to cache all the first queries that are done
|
|
10
|
-
* by the application when it starts. With this we should have all the necessary
|
|
11
|
-
* cache to be able to start the application in offline mode
|
|
12
|
-
*
|
|
13
|
-
* The WMTS tiles and other results coming from OGC-Services will be cached on demand
|
|
14
|
-
* with the OfflineManager component of the application.
|
|
15
|
-
*/
|
|
16
|
-
const sw = globalThis;
|
|
17
|
-
const currentOrigin = self.location.origin;
|
|
18
|
-
/**
|
|
19
|
-
* The service worker state needs to be persisted
|
|
20
|
-
* because when not using the app, the service worker can be paused by the browser
|
|
21
|
-
* and in this case it will be reinitialized with the next query
|
|
22
|
-
* with a base default empty state.
|
|
23
|
-
* See: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope
|
|
24
|
-
*/
|
|
25
|
-
const stateDbName = 'geogirafe-sw-state';
|
|
26
|
-
const stateStoreName = 'state';
|
|
27
|
-
const stateKey = 'config';
|
|
28
|
-
let initialized = false;
|
|
29
|
-
let state = {
|
|
30
|
-
storeVersion: 1,
|
|
31
|
-
dbCacheName: 'geogirafe-cache',
|
|
32
|
-
tilesStoreName: 'tiles',
|
|
33
|
-
logLevel: 'warning',
|
|
34
|
-
audience: [],
|
|
35
|
-
audienceExcludedPaths: [],
|
|
36
|
-
accessToken: undefined,
|
|
37
|
-
loginState: undefined,
|
|
38
|
-
authMode: undefined,
|
|
39
|
-
refererPolicy: undefined
|
|
40
|
-
};
|
|
41
|
-
function isLoggedIn() {
|
|
42
|
-
return state.loginState === 'loggedIn' || state.loginState === 'issuer.loggedIn';
|
|
43
|
-
}
|
|
44
|
-
const offlineTimeout = 3000; // Timeout in case of not reachable IndexedDB. (3 sec.)
|
|
45
|
-
const appCacheName = 'pages'; // Name of the cache for application pages
|
|
46
|
-
const maxCacheCount = 300; // Number of queries that should be cached by the service-worker for offline usage.
|
|
47
|
-
let cacheCount = 0; // Counter related to the max value above
|
|
48
|
-
let audienceExcludedPathPatterns = []; // list of regex pattern built from state.audienceExcludedPaths
|
|
49
|
-
sw.addEventListener('message', handleMessage);
|
|
50
|
-
sw.addEventListener('install', handleInstall);
|
|
51
|
-
sw.addEventListener('activate', handleActivate);
|
|
52
|
-
sw.addEventListener('fetch', (event) => {
|
|
53
|
-
if (event.request.mode === 'navigate') {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
event.respondWith(handleFetchEvent(event));
|
|
57
|
-
});
|
|
58
|
-
function handleMessage(event) {
|
|
59
|
-
if (event.origin !== currentOrigin) {
|
|
60
|
-
console.warn(`Message ignored: origin not allowed (${event.origin})`);
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
const data = event.data;
|
|
64
|
-
if (data.logLevel) {
|
|
65
|
-
state.logLevel = data.logLevel;
|
|
66
|
-
log(`LogLevel changed: ${state.logLevel}`);
|
|
67
|
-
}
|
|
68
|
-
if (data.tilesStoreName) {
|
|
69
|
-
state.tilesStoreName = data.tilesStoreName;
|
|
70
|
-
log(`tilesStoreName changed: ${state.tilesStoreName}`);
|
|
71
|
-
}
|
|
72
|
-
if (data.storeVersion) {
|
|
73
|
-
state.storeVersion = data.storeVersion;
|
|
74
|
-
log(`storeVersion changed: ${state.storeVersion}`);
|
|
75
|
-
}
|
|
76
|
-
if (data.dbCacheName) {
|
|
77
|
-
state.dbCacheName = data.dbCacheName;
|
|
78
|
-
log(`dbCacheName changed: ${state.dbCacheName}`);
|
|
79
|
-
}
|
|
80
|
-
if (data.audience) {
|
|
81
|
-
state.audience = data.audience ?? [];
|
|
82
|
-
log(`audience changed: ${state.audience}`);
|
|
83
|
-
}
|
|
84
|
-
if (data.audienceExcludedPaths) {
|
|
85
|
-
state.audienceExcludedPaths = data.audienceExcludedPaths;
|
|
86
|
-
audienceExcludedPathPatterns = state.audienceExcludedPaths.map((str) => new RegExp(str));
|
|
87
|
-
log(`audienceExcludedPaths changed: ${state.audienceExcludedPaths}`);
|
|
88
|
-
}
|
|
89
|
-
if (data.access_token) {
|
|
90
|
-
state.accessToken = data.access_token;
|
|
91
|
-
log(`access_token changed: ${state.accessToken}`);
|
|
92
|
-
}
|
|
93
|
-
if (data.clear_access_token) {
|
|
94
|
-
state.accessToken = undefined;
|
|
95
|
-
log('access_token cleared');
|
|
96
|
-
}
|
|
97
|
-
if (data.authMode) {
|
|
98
|
-
state.authMode = data.authMode;
|
|
99
|
-
log(`authMode changed: ${state.authMode}`);
|
|
100
|
-
}
|
|
101
|
-
if (data.refererPolicy) {
|
|
102
|
-
state.refererPolicy = data.refererPolicy;
|
|
103
|
-
log(`refererPolicy changed: ${state.refererPolicy}`);
|
|
104
|
-
}
|
|
105
|
-
if (data.loginState) {
|
|
106
|
-
state.loginState = data.loginState;
|
|
107
|
-
log(`loginState changed: ${state.loginState}`);
|
|
108
|
-
}
|
|
109
|
-
event.waitUntil((async () => {
|
|
110
|
-
// Persist the service worker state before acknowledging the message.
|
|
111
|
-
await saveState();
|
|
112
|
-
if (data.messageId) {
|
|
113
|
-
const source = event.source;
|
|
114
|
-
if (source) {
|
|
115
|
-
source.postMessage({ messageId: data.messageId, status: 'ServiceWorker updated' });
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
})());
|
|
119
|
-
}
|
|
120
|
-
function handleInstall() {
|
|
121
|
-
sw.skipWaiting();
|
|
122
|
-
log('Service worker installed');
|
|
123
|
-
}
|
|
124
|
-
function handleActivate(event) {
|
|
125
|
-
event.waitUntil((async () => {
|
|
126
|
-
if ('clients' in sw) {
|
|
127
|
-
await sw.clients.claim();
|
|
128
|
-
const clientsList = await sw.clients.matchAll({ type: 'window' });
|
|
129
|
-
for (const client of clientsList) {
|
|
130
|
-
client.navigate(client.url);
|
|
131
|
-
log('Page reloaded by the service worker.');
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
})());
|
|
135
|
-
}
|
|
136
|
-
async function handleFetchEvent(event) {
|
|
137
|
-
if (!initialized) {
|
|
138
|
-
await loadState();
|
|
139
|
-
}
|
|
140
|
-
const newRequest = getRequest(event.request);
|
|
141
|
-
let response = await fetchAndCache(newRequest);
|
|
142
|
-
// Use cache only for GET queries
|
|
143
|
-
if (isCacheAllowed(event.request)) {
|
|
144
|
-
if (!response) {
|
|
145
|
-
// Fetch was unsuccessful. We try to load the data from cache.
|
|
146
|
-
response = await loadFromCache(event.request);
|
|
147
|
-
}
|
|
148
|
-
if (!response) {
|
|
149
|
-
// Not found in cache. We try to load from IndexedDB.
|
|
150
|
-
response = await loadFromIndexedDB(event.request);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return response ?? new Response(null, { status: 503 });
|
|
154
|
-
}
|
|
155
|
-
function isCacheAllowed(request) {
|
|
156
|
-
if (request.method !== 'GET') {
|
|
157
|
-
return false;
|
|
158
|
-
}
|
|
159
|
-
if (request.headers.get('Range')) {
|
|
160
|
-
// RANGE Request (for COG, FlatGeoBuf or GeoParquet for example)
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
|
-
return true;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Execute a fetch for the query, and cache the result if successful
|
|
167
|
-
* Returns null if unsuccessful
|
|
168
|
-
*/
|
|
169
|
-
async function fetchAndCache(request) {
|
|
170
|
-
try {
|
|
171
|
-
const response = await fetch(request);
|
|
172
|
-
if (cacheCount < maxCacheCount && isCacheAllowed(request) && response.ok && response.type !== 'opaque') {
|
|
173
|
-
// Fetch was successful. We cache the result if necessary and return the response.
|
|
174
|
-
cacheCount++;
|
|
175
|
-
log(`SW ${cacheCount}/${maxCacheCount} caching ${request.url} for offline use.`);
|
|
176
|
-
const copy = response.clone();
|
|
177
|
-
const cache = await caches.open(appCacheName);
|
|
178
|
-
await cache.put(request, copy);
|
|
179
|
-
}
|
|
180
|
-
return response;
|
|
181
|
-
}
|
|
182
|
-
catch {
|
|
183
|
-
return null;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
function getRequest(request) {
|
|
187
|
-
if (request.mode === 'no-cors') {
|
|
188
|
-
return request; // Cannot do anything here, the no-cors queries cannot be modified
|
|
189
|
-
}
|
|
190
|
-
try {
|
|
191
|
-
const requestedUrl = new URL(request.url);
|
|
192
|
-
const hostname = requestedUrl.hostname;
|
|
193
|
-
const shouldExclude = audienceExcludedPathPatterns.some((pattern) => pattern.test(requestedUrl.pathname));
|
|
194
|
-
if (state.audience?.includes(hostname) && !shouldExclude) {
|
|
195
|
-
// Prepare headers
|
|
196
|
-
const headers = new Headers(request.headers);
|
|
197
|
-
if (state.authMode == 'token' && isLoggedIn() && state.accessToken) {
|
|
198
|
-
headers.set('Authorization', `Bearer ${state.accessToken}`);
|
|
199
|
-
}
|
|
200
|
-
// Prepare options
|
|
201
|
-
const fetchOptions = {
|
|
202
|
-
headers: headers,
|
|
203
|
-
credentials: isLoggedIn() && state.authMode === 'cookie' ? 'include' : 'omit',
|
|
204
|
-
referrer: request.referrer,
|
|
205
|
-
referrerPolicy: state.refererPolicy
|
|
206
|
-
};
|
|
207
|
-
return new Request(request, fetchOptions);
|
|
208
|
-
}
|
|
209
|
-
// Return the initial request
|
|
210
|
-
return request;
|
|
211
|
-
}
|
|
212
|
-
catch (error) {
|
|
213
|
-
// In case of error, we return the initial request
|
|
214
|
-
log('Error while creating the request with authentication', error);
|
|
215
|
-
return request;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Try to load this request from local cache.
|
|
220
|
-
* Returns null if unsuccessful
|
|
221
|
-
*/
|
|
222
|
-
async function loadFromCache(request) {
|
|
223
|
-
const cachedResponse = await caches.match(request);
|
|
224
|
-
return cachedResponse || null;
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Try to load this request from indexedDB.
|
|
228
|
-
* Returns null if unsuccessful
|
|
229
|
-
*/
|
|
230
|
-
async function loadFromIndexedDB(request) {
|
|
231
|
-
try {
|
|
232
|
-
const database = await openIndexedDB();
|
|
233
|
-
const transaction = database.transaction([state.tilesStoreName], 'readonly');
|
|
234
|
-
const store = transaction.objectStore(state.tilesStoreName);
|
|
235
|
-
const index = store.index('url');
|
|
236
|
-
return new Promise((resolve, reject) => {
|
|
237
|
-
const dbRequest = index.get(request.url);
|
|
238
|
-
dbRequest.onsuccess = () => {
|
|
239
|
-
if (dbRequest.result) {
|
|
240
|
-
const blob = dbRequest.result.data;
|
|
241
|
-
log('Tile found in cache.');
|
|
242
|
-
resolve(new Response(blob));
|
|
243
|
-
}
|
|
244
|
-
else {
|
|
245
|
-
log('Tile not found in cache.');
|
|
246
|
-
resolve(new Response(null, { status: 204 }));
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
dbRequest.onerror = () => {
|
|
250
|
-
reject(new Error('Error querying IndexedDB Store.'));
|
|
251
|
-
};
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
catch {
|
|
255
|
-
return null;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Open IndexedDB
|
|
260
|
-
*/
|
|
261
|
-
async function openIndexedDB() {
|
|
262
|
-
return new Promise((resolve, reject) => {
|
|
263
|
-
const request = indexedDB.open(state.dbCacheName, state.storeVersion);
|
|
264
|
-
let timedOut = false;
|
|
265
|
-
const timeoutId = setTimeout(() => {
|
|
266
|
-
timedOut = true;
|
|
267
|
-
reject(new Error('SW Timeout while opening IndexedDB'));
|
|
268
|
-
}, offlineTimeout);
|
|
269
|
-
request.onerror = (event) => {
|
|
270
|
-
if (!timedOut) {
|
|
271
|
-
clearTimeout(timeoutId);
|
|
272
|
-
reject(event.target.error);
|
|
273
|
-
}
|
|
274
|
-
};
|
|
275
|
-
request.onsuccess = (event) => {
|
|
276
|
-
if (!timedOut) {
|
|
277
|
-
clearTimeout(timeoutId);
|
|
278
|
-
resolve(event.target.result);
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Open IndexedDB for the service worker state
|
|
285
|
-
*/
|
|
286
|
-
async function openStateDB() {
|
|
287
|
-
return new Promise((resolve, reject) => {
|
|
288
|
-
const request = indexedDB.open(stateDbName, 1);
|
|
289
|
-
request.onupgradeneeded = () => {
|
|
290
|
-
request.result.createObjectStore(stateStoreName);
|
|
291
|
-
};
|
|
292
|
-
request.onsuccess = () => resolve(request.result);
|
|
293
|
-
request.onerror = () => reject(request.error);
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Saves the service worker state
|
|
298
|
-
*/
|
|
299
|
-
async function saveState() {
|
|
300
|
-
log('Save state');
|
|
301
|
-
const db = await openStateDB();
|
|
302
|
-
return new Promise((resolve, reject) => {
|
|
303
|
-
const tx = db.transaction(stateStoreName, 'readwrite');
|
|
304
|
-
tx.objectStore(stateStoreName).put(state, stateKey);
|
|
305
|
-
tx.oncomplete = () => resolve();
|
|
306
|
-
tx.onerror = () => reject(tx.error);
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Reloads the service worker state from indexDB
|
|
311
|
-
*/
|
|
312
|
-
async function loadState() {
|
|
313
|
-
log('Reload state');
|
|
314
|
-
const db = await openStateDB();
|
|
315
|
-
const loadedState = await new Promise((resolve, reject) => {
|
|
316
|
-
const tx = db.transaction(stateStoreName, 'readonly');
|
|
317
|
-
const req = tx.objectStore(stateStoreName).get(stateKey);
|
|
318
|
-
req.onsuccess = () => resolve(req.result);
|
|
319
|
-
req.onerror = () => reject(req.error);
|
|
320
|
-
});
|
|
321
|
-
if (loadedState) {
|
|
322
|
-
state = loadedState;
|
|
323
|
-
audienceExcludedPathPatterns = state.audienceExcludedPaths.map((str) => new RegExp(str));
|
|
324
|
-
}
|
|
325
|
-
initialized = true;
|
|
326
|
-
}
|
|
327
|
-
function log(str, error) {
|
|
328
|
-
if (state.logLevel === 'debug') {
|
|
329
|
-
console.debug(`SW: ${str}`, error ?? '');
|
|
330
|
-
}
|
|
331
|
-
}
|