@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.
package/dist/esm/index.js CHANGED
@@ -88687,53 +88687,22 @@ function useWebReader(args) {
88687
88687
  import React68 from "react";
88688
88688
 
88689
88689
  // src/ServiceWorker/constants.ts
88690
- var WEBPUB_CACHE_NAME = "webpub-cache";
88690
+ var PRECACHE_PUBLICATIONS = "PRECACHE_PUBLICATIONS";
88691
88691
  var CACHE_EXPIRATION_SECONDS = 7 * 24 * 60 * 60;
88692
88692
 
88693
88693
  // src/ServiceWorker/client.ts
88694
88694
  function usePublicationSW(publications) {
88695
88695
  React68.useEffect(() => {
88696
- function cachePublications() {
88697
- return __async(this, null, function* () {
88698
- const cache = yield caches.open(WEBPUB_CACHE_NAME);
88699
- const promises = publications.map((pub) => __async(this, null, function* () {
88700
- var _a, _b;
88701
- const finalManifestUrl = getProxiedUrl(pub.manifestUrl, pub.proxyUrl);
88702
- const manifestResponse = yield fetch(finalManifestUrl);
88703
- handleBadResponse(finalManifestUrl, manifestResponse);
88704
- yield cache.put(finalManifestUrl, manifestResponse.clone());
88705
- const manifest = yield manifestResponse.json();
88706
- const resourceHrefs = extractHrefs((_a = manifest.resources) != null ? _a : [], pub.manifestUrl, pub.proxyUrl);
88707
- const readingOrderHrefs = extractHrefs((_b = manifest.readingOrder) != null ? _b : [], pub.manifestUrl, pub.proxyUrl);
88708
- const allResourcesToCache = Array.from(new Set([...resourceHrefs, ...readingOrderHrefs]));
88709
- yield Promise.all(allResourcesToCache.map((url) => __async(this, null, function* () {
88710
- const response = yield fetch(url);
88711
- handleBadResponse(url, response);
88712
- return yield cache.put(url, response);
88713
- })));
88714
- }));
88715
- return yield Promise.allSettled(promises);
88716
- });
88696
+ var _a;
88697
+ if ("serviceWorker" in navigator) {
88698
+ const message = {
88699
+ type: PRECACHE_PUBLICATIONS,
88700
+ publications
88701
+ };
88702
+ (_a = navigator.serviceWorker.controller) == null ? void 0 : _a.postMessage(message);
88717
88703
  }
88718
- cachePublications();
88719
88704
  }, [publications]);
88720
88705
  }
88721
- function handleBadResponse(url, response) {
88722
- if (!response.ok) {
88723
- const message = `Bad response status for: ${url}. Status: ${response.status}`;
88724
- console.warn(message);
88725
- throw new Error(message);
88726
- }
88727
- }
88728
- function getProxiedUrl(url, proxyUrl) {
88729
- return proxyUrl ? `${proxyUrl}${encodeURIComponent(url)}` : url;
88730
- }
88731
- function getAbsoluteUrl(maybeRelative, manifestUrl, proxyUrl) {
88732
- return getProxiedUrl(new URL(maybeRelative, manifestUrl).toString(), proxyUrl);
88733
- }
88734
- function extractHrefs(links, manifestUrl, proxyUrl) {
88735
- return links.map((res) => getAbsoluteUrl(res.href, manifestUrl, proxyUrl));
88736
- }
88737
88706
 
88738
88707
  // src/index.tsx
88739
88708
  var WebReader = (_a) => {