@nypl/web-reader 0.2.0-alpha.8 → 0.2.0-alpha.9

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.
@@ -1210,6 +1210,7 @@ var IS_DEV = false;
1210
1210
 
1211
1211
  // src/ServiceWorker/constants.ts
1212
1212
  var WEBPUB_CACHE_NAME = "webpub-cache";
1213
+ var PRECACHE_PUBLICATIONS = "PRECACHE_PUBLICATIONS";
1213
1214
  var CACHE_EXPIRATION_SECONDS = 7 * 24 * 60 * 60;
1214
1215
 
1215
1216
  // src/ServiceWorker/sw.ts
@@ -1229,6 +1230,16 @@ function initWebReaderSW({
1229
1230
  }
1230
1231
  event.waitUntil(installSW);
1231
1232
  });
1233
+ self.addEventListener("message", (event) => __async(this, null, function* () {
1234
+ if (event.data.type === PRECACHE_PUBLICATIONS) {
1235
+ log("Precaching publications");
1236
+ if (typeof event.data.publications !== "object") {
1237
+ console.error("Precache event missing publications");
1238
+ return;
1239
+ }
1240
+ yield cachePublications(event.data.publications);
1241
+ }
1242
+ }));
1232
1243
  self.addEventListener("fetch", (event) => {
1233
1244
  if (event.request.method !== "GET") {
1234
1245
  return;
@@ -1253,6 +1264,61 @@ function initWebReaderSW({
1253
1264
  event.respondWith(matchOrFetch());
1254
1265
  });
1255
1266
  }
1267
+ function cachePublications(publications) {
1268
+ return __async(this, null, function* () {
1269
+ const cache = yield caches.open(WEBPUB_CACHE_NAME);
1270
+ const pubResults = yield Promise.allSettled(publications.map((pub) => __async(this, null, function* () {
1271
+ const finalManifestUrl = getProxiedUrl(pub.manifestUrl, pub.proxyUrl);
1272
+ const match = yield cache.match(finalManifestUrl);
1273
+ if (match) {
1274
+ log(`Manifest match found for ${finalManifestUrl}`);
1275
+ return __spreadProps(__spreadValues({}, pub), { manifest: yield match.json() });
1276
+ }
1277
+ const manifestResponse = yield fetch(finalManifestUrl);
1278
+ handleBadResponse(finalManifestUrl, manifestResponse);
1279
+ yield cache.put(finalManifestUrl, manifestResponse.clone());
1280
+ const manifest = yield manifestResponse.json();
1281
+ return __spreadProps(__spreadValues({}, pub), { manifest });
1282
+ })));
1283
+ const pubs = pubResults.map((result) => result.status === "fulfilled" ? result.value : void 0).filter(isPub);
1284
+ const promises = pubs.map((pub) => __async(this, null, function* () {
1285
+ var _a, _b;
1286
+ const resourceHrefs = extractHrefs((_a = pub.manifest.resources) != null ? _a : [], pub.manifestUrl, pub.proxyUrl);
1287
+ const readingOrderHrefs = extractHrefs((_b = pub.manifest.readingOrder) != null ? _b : [], pub.manifestUrl, pub.proxyUrl);
1288
+ const allResourcesToCache = Array.from(new Set([...resourceHrefs, ...readingOrderHrefs]));
1289
+ yield Promise.all(allResourcesToCache.map((url) => __async(this, null, function* () {
1290
+ const match = yield cache.match(url);
1291
+ if (match) {
1292
+ log(`Resource match found for ${url}`);
1293
+ return;
1294
+ }
1295
+ const response = yield fetch(url);
1296
+ handleBadResponse(url, response);
1297
+ return yield cache.put(url, response);
1298
+ })));
1299
+ }));
1300
+ return yield Promise.allSettled(promises);
1301
+ });
1302
+ }
1303
+ function isPub(maybe) {
1304
+ return !!maybe;
1305
+ }
1306
+ function handleBadResponse(url, response) {
1307
+ if (!response.ok) {
1308
+ const message = `Bad response status for: ${url}. Status: ${response.status}`;
1309
+ console.warn(message);
1310
+ throw new Error(message);
1311
+ }
1312
+ }
1313
+ function getProxiedUrl(url, proxyUrl) {
1314
+ return proxyUrl ? `${proxyUrl}${encodeURIComponent(url)}` : url;
1315
+ }
1316
+ function getAbsoluteUrl(maybeRelative, manifestUrl, proxyUrl) {
1317
+ return getProxiedUrl(new URL(maybeRelative, manifestUrl).toString(), proxyUrl);
1318
+ }
1319
+ function extractHrefs(links, manifestUrl, proxyUrl) {
1320
+ return links.map((res) => getAbsoluteUrl(res.href, manifestUrl, proxyUrl));
1321
+ }
1256
1322
  function log(message) {
1257
1323
  if (IS_DEV)
1258
1324
  console.log(`SW (${VERSION}) -`, message);