ts-service-worker 1.1.2 → 1.2.0

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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  - provide declarative support for the most common caching strategies
4
4
  - supports easy integration of multiple caching strategies
5
- - produces a `service-worker.js` that requires no additional transpilation
5
+ - produces a `service-worker.js` that requires no additional transcompilation
6
6
  - avoids nesting and promise chains
7
7
  - is written in Typescript and fully typed
8
8
  - can scan your disk for resources to cache
package/dist/cli.mjs CHANGED
@@ -6551,7 +6551,9 @@ self.addEventListener('activate', function (event) {
6551
6551
  }))));
6552
6552
  });
6553
6553
  self.addEventListener('fetch', function (event) {
6554
- const url = event.request.url;
6554
+
6555
+ const url = event.request.url.replace(/#.*$/, '');
6556
+
6555
6557
  // Cache http and https only, skip unsupported chrome-extension:// and file://...
6556
6558
  if (!url.startsWith('http'))
6557
6559
  return;
@@ -6568,7 +6570,7 @@ self.addEventListener('fetch', function (event) {
6568
6570
  function matches(paths) {
6569
6571
  return paths &&
6570
6572
  paths.some(path => path instanceof RegExp ?
6571
- path.exec(url) : url.endsWith(path));
6573
+ path.exec(url) : url === path);
6572
6574
  }
6573
6575
  });
6574
6576
  // Caching strategies
@@ -6616,11 +6618,12 @@ function fetchRequest(event) {
6616
6618
  function fetchAndCache(event) {
6617
6619
  return fetchRequest(event)
6618
6620
  .then(response => {
6619
- return toCache(event, response).catch(e => {
6620
- log(\` - unable to cache \${event.request.url} \${e}\`);
6621
- return response;
6622
- })
6623
- });
6621
+ return toCache(event, response)
6622
+ })
6623
+ .catch(e => {
6624
+ log(\` - unable to fetch \${event.request.url} \${e}\`);
6625
+ throw e
6626
+ });
6624
6627
  }
6625
6628
  function fromCache(event) {
6626
6629
  return caches.open(CACHE_NAME).then(cache => cache.match(event.request));
@@ -6630,7 +6633,7 @@ function fileFromCache(name) {
6630
6633
  }
6631
6634
  function toCache(event, response) {
6632
6635
  if (!response.ok) {
6633
- log(\`request for \${event.request.url} failed with status \${response.statusText}\`);
6636
+ log(\`skipping cache of failed request \${event.request.url} with status \${response.status} \${response.statusText}\`);
6634
6637
  return response;
6635
6638
  }
6636
6639
  return caches.open(CACHE_NAME)
@@ -6649,6 +6652,12 @@ function log(s) {
6649
6652
  function regexEscape(s) {
6650
6653
  return s.replace(/([/.])/g, '\\\\$&');
6651
6654
  }
6655
+
6656
+ function withOrigin(path) {
6657
+ if (path.match(/https?:\\/\\//)) return path
6658
+ if (path.startsWith('/')) return self.origin + path
6659
+ return self.origin + '/' + path
6660
+ }
6652
6661
  `;
6653
6662
 
6654
6663
  // src/utils.mts
@@ -6710,7 +6719,7 @@ function evalTemplate(variables) {
6710
6719
  }
6711
6720
  function pathsToJS(paths) {
6712
6721
  const asArray = Array.isArray(paths) ? paths : [paths];
6713
- return `[${asArray.map(pathToJS).join(",")}]`;
6722
+ return `[${asArray.map(pathToJS).join(",")}].map(withOrigin)`;
6714
6723
  }
6715
6724
  function pathToJS(path3) {
6716
6725
  switch (typeof path3) {
package/dist/index.js CHANGED
@@ -6563,7 +6563,9 @@ self.addEventListener('activate', function (event) {
6563
6563
  }))));
6564
6564
  });
6565
6565
  self.addEventListener('fetch', function (event) {
6566
- const url = event.request.url;
6566
+
6567
+ const url = event.request.url.replace(/#.*$/, '');
6568
+
6567
6569
  // Cache http and https only, skip unsupported chrome-extension:// and file://...
6568
6570
  if (!url.startsWith('http'))
6569
6571
  return;
@@ -6580,7 +6582,7 @@ self.addEventListener('fetch', function (event) {
6580
6582
  function matches(paths) {
6581
6583
  return paths &&
6582
6584
  paths.some(path => path instanceof RegExp ?
6583
- path.exec(url) : url.endsWith(path));
6585
+ path.exec(url) : url === path);
6584
6586
  }
6585
6587
  });
6586
6588
  // Caching strategies
@@ -6628,11 +6630,12 @@ function fetchRequest(event) {
6628
6630
  function fetchAndCache(event) {
6629
6631
  return fetchRequest(event)
6630
6632
  .then(response => {
6631
- return toCache(event, response).catch(e => {
6632
- log(\` - unable to cache \${event.request.url} \${e}\`);
6633
- return response;
6634
- })
6635
- });
6633
+ return toCache(event, response)
6634
+ })
6635
+ .catch(e => {
6636
+ log(\` - unable to fetch \${event.request.url} \${e}\`);
6637
+ throw e
6638
+ });
6636
6639
  }
6637
6640
  function fromCache(event) {
6638
6641
  return caches.open(CACHE_NAME).then(cache => cache.match(event.request));
@@ -6642,7 +6645,7 @@ function fileFromCache(name) {
6642
6645
  }
6643
6646
  function toCache(event, response) {
6644
6647
  if (!response.ok) {
6645
- log(\`request for \${event.request.url} failed with status \${response.statusText}\`);
6648
+ log(\`skipping cache of failed request \${event.request.url} with status \${response.status} \${response.statusText}\`);
6646
6649
  return response;
6647
6650
  }
6648
6651
  return caches.open(CACHE_NAME)
@@ -6661,6 +6664,12 @@ function log(s) {
6661
6664
  function regexEscape(s) {
6662
6665
  return s.replace(/([/.])/g, '\\\\$&');
6663
6666
  }
6667
+
6668
+ function withOrigin(path) {
6669
+ if (path.match(/https?:\\/\\//)) return path
6670
+ if (path.startsWith('/')) return self.origin + path
6671
+ return self.origin + '/' + path
6672
+ }
6664
6673
  `;
6665
6674
 
6666
6675
  // src/utils.mts
@@ -6722,7 +6731,7 @@ function evalTemplate(variables) {
6722
6731
  }
6723
6732
  function pathsToJS(paths) {
6724
6733
  const asArray = Array.isArray(paths) ? paths : [paths];
6725
- return `[${asArray.map(pathToJS).join(",")}]`;
6734
+ return `[${asArray.map(pathToJS).join(",")}].map(withOrigin)`;
6726
6735
  }
6727
6736
  function pathToJS(path2) {
6728
6737
  switch (typeof path2) {