textbrowser 0.54.10 → 0.54.11
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/CHANGES.md +4 -0
- package/dist/sw-helper.js +32 -3
- package/package.json +1 -1
package/CHANGES.md
CHANGED
package/dist/sw-helper.js
CHANGED
|
@@ -249,7 +249,9 @@ function swHelper (self) {
|
|
|
249
249
|
}, 10000);
|
|
250
250
|
// .map((url) => url === 'index.html' ? new Request(url, {cache: 'reload'}) : url)
|
|
251
251
|
try {
|
|
252
|
-
const
|
|
252
|
+
const maxConcurrentPrefetches = 8;
|
|
253
|
+
const prefetchTimeoutMs = 120000;
|
|
254
|
+
const fetchAndCacheAsset = async (urlToPrefetch, idx) => {
|
|
253
255
|
// This constructs a new URL object using the service worker's script
|
|
254
256
|
// location as the base for relative URLs.
|
|
255
257
|
pendingAssets.add(urlToPrefetch);
|
|
@@ -260,8 +262,14 @@ function swHelper (self) {
|
|
|
260
262
|
const url = new URL(urlToPrefetch, location.href);
|
|
261
263
|
url.search += (url.search ? '&' : '?') + 'cache-bust=' + now;
|
|
262
264
|
const request = new Request(url, {mode: 'no-cors'});
|
|
265
|
+
const controller = new AbortController();
|
|
266
|
+
const timeout = setTimeout(() => {
|
|
267
|
+
controller.abort();
|
|
268
|
+
}, prefetchTimeoutMs);
|
|
263
269
|
try {
|
|
264
|
-
const response = await fetch(request
|
|
270
|
+
const response = await fetch(request, {
|
|
271
|
+
signal: controller.signal
|
|
272
|
+
});
|
|
265
273
|
if (response.status >= 400) {
|
|
266
274
|
throw new Error('request for ' + urlToPrefetch +
|
|
267
275
|
' failed with status ' + response.statusText);
|
|
@@ -272,15 +280,36 @@ function swHelper (self) {
|
|
|
272
280
|
return;
|
|
273
281
|
} catch (error) {
|
|
274
282
|
pendingAssets.delete(urlToPrefetch);
|
|
283
|
+
if (
|
|
284
|
+
error && typeof error === 'object' && 'name' in error &&
|
|
285
|
+
error.name === 'AbortError'
|
|
286
|
+
) {
|
|
287
|
+
error = new Error(
|
|
288
|
+
`Timed out after ${prefetchTimeoutMs}ms while fetching ${urlToPrefetch}`
|
|
289
|
+
);
|
|
290
|
+
}
|
|
275
291
|
logError(
|
|
276
292
|
/** @type {Error} */
|
|
277
293
|
(error),
|
|
278
294
|
`Not caching ${urlToPrefetch} due to ${error}`
|
|
279
295
|
);
|
|
280
296
|
throw error;
|
|
297
|
+
} finally {
|
|
298
|
+
clearTimeout(timeout);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
let nextAssetIndex = 0;
|
|
303
|
+
const workers = Array.from({
|
|
304
|
+
length: Math.min(maxConcurrentPrefetches, urlsToPrefetch.length)
|
|
305
|
+
}, async () => {
|
|
306
|
+
while (nextAssetIndex < urlsToPrefetch.length) {
|
|
307
|
+
const idx = nextAssetIndex;
|
|
308
|
+
nextAssetIndex++;
|
|
309
|
+
await fetchAndCacheAsset(urlsToPrefetch[idx], idx);
|
|
281
310
|
}
|
|
282
311
|
});
|
|
283
|
-
await Promise.all(
|
|
312
|
+
await Promise.all(workers);
|
|
284
313
|
log('Install: Pre-fetching complete.');
|
|
285
314
|
} catch (error) {
|
|
286
315
|
logError(
|