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 CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGES to `textbrowser`
2
2
 
3
+ ## 0.54.11
4
+
5
+ - fix: max concurrent prefetches
6
+
3
7
  ## 0.54.10
4
8
 
5
9
  - fix: more logging
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 cachePromises = urlsToPrefetch.map(async (urlToPrefetch, idx) => {
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(cachePromises);
312
+ await Promise.all(workers);
284
313
  log('Install: Pre-fetching complete.');
285
314
  } catch (error) {
286
315
  logError(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "textbrowser",
3
- "version": "0.54.10",
3
+ "version": "0.54.11",
4
4
  "description": "Multilinear text browser",
5
5
  "type": "module",
6
6
  "main": "dist/index-es.min.js",