Is it possible to combine a dom-repeat on a platin

2019-09-08 19:48发布

问题:

I want to provide a user the ability to cache up to 2,600+ items, by groupings (categories of book, individual books, or possibly even just chapters of a certain book if they don't want the whole book). It is not possible, as far as I can tell, to precache all of these items because there are 2,600+ of them, and will be more in the future - the service worker will timeout with under a couple hundred. And since service workers either get all or none on install (if I understand correctly), do I need to use multiple services workers (with different ids?), or am I thinking about this wrong?

What I am thinking is something like...

<iron ajax></iron-ajax>
<template is="dom-repeat" items="...">
<platinum-sw-register auto-register clients-claim skip-waiting>
    <platinum-sw-cache default-cache-strategy="fastest"
        cache-config-file="../someGenerator.php"></platinum-sw-cache>
</platinum-sw-register>

In other words:

  1. Get a list of wanted URLs via iron-ajax (based upon what the user enables for cache)
  2. Iterate through the URLs as groups via dom-repeat
  3. Create a service worker with a customized cache-config for the URL group
  4. Repeat 2 and 3 until done, then present a toast

That someGenerator.php would return a JSON config setup for the particular group of URLs.

My app is a single page app - with neon-animated-pages - one page representing categories, one for book listings, one for table of contents for each book, and then one of each the chapter contents. All of the data is obtained via iron-ajax.

Here are some links to demonstrate the issues:

The App

A large non-functional cache-config generated

I suspect, in order to not have service workers errors due to redundancy, or overwrite existing caches, I will need to assign individual ids, and include them in the generated cache-configs. Does that sound right?

回答1:

No, I don't think that's the right approach. <dom-repeat> and creating multiple service workers isn't going to accomplish what you want.

It does look like you're bumping into some service worker-imposed timeouts during your install handler due to the delays in fetching the JSON configuration and performing all of the precaching. Taking a step back, are you sure that you need that entire set of URLs precached?

<platinum-sw> will give you runtime caching as well, so that when a browser loads a given URL when there's a network connection available, the resources will be automatically added to the cache and available offline during subsequent return visits.

There are other approaches that would use either window.caches to cache resources from within your controlled page, or using something like postMessage() to communicate a list of additional URLs to cache from your controlled page to your service worker. Both of those approaches would involve going beyond the default functionality you get from using <platinum-sw> and digging into the internals a bit.