Answers.Body: <p>So you shouldn't try to come up with a hashed file naming scheme for busting the cache; instead use one of Workbox's <a href="https://developers.google.com/web/tools/workbox/modules/workbox-strategies" rel="nofollow noreferrer">Strategies</a> -- I think a great pattern for what you're trying to do is <a href="https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#stale-while-revalidate" rel="nofollow noreferrer">Stale While Revalidate</a></p>
  
  <p>TL;DR Workbox has strategies for doing what you're trying to do.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-15T03:17:04.557Z
Answers.Text: Thanks Andrew, but this is what I'm wondering: does re-validating mean going back to the server and requesting the page (and querying the DB) even if the page hasn't changed? I'd imagine that with a manifest, if the hashed file hasn't changed, then the SW would simply serve from the cache and stay away from the server until instructed to do so.
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-24T18:58:02.167Z
Answers.Text: There are many strategies you can adopt... my point is really that Workbox can handle this for you, you're not going to need template logic.
Answers.UserDisplayName: andrew.welch
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-26T01:22:56.967Z
Answers.Text: It took me a while to wrap my head around all this, but the precache manifest is useful primarily for static assets (js, css...). In my case, stale-while-revalidate is probably the way to go for content, provided I can workout how to refresh the page when new content is available.
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-06-14T18:02:44.217Z
Answers.LastActivityDate: 2019-06-14T18:02:44.217Z
Answers.UserDisplayName: andrew.welch
Body: <p>I'm thinking of using <a href="https://developers.google.com/web/tools/workbox/guides/precache-files/" rel="nofollow noreferrer">Google's Workbox to pre-cache a bunch of files</a>, and I've been scratching my head as to how to generate unique hashes for files when they change. Something like this:</p>
  
  <pre><code> workbox.precaching.precacheAndRoute([
      { 
        url: '/',
        revision: '383676',
      },
      { 
        url: '/about',
        revision: 213142,
      },
      { 
        town: '/location',
        revision: 742125,
      },
    ]);
  </code></pre>
  
  <p>Ideally the revision number would change when the entry or related entries included in the page are changed/saved. I got this working but I'm not sure how to get related content to clear the cache:</p>
  
  <pre><code>// service-worker.js
  
  {%- cache using key 'abc' -%}
      { 
        url: {{ craft.entries.id(64).one.uri }},
        revision: {{ random(100000, 999999) }},
      },
  {%- endcache -%}
  </code></pre>
  
  <p>This seems to work when said entry is saved, but I'm not sure how to deal with a related entry being saved. I'm also not sure this is the most performant way to go.</p>
  
Tags: <cache><routes>
Title: Generating a precache manifest for service workers
ThreadText: # Generating a precache manifest for service workers
  
  (Asked by: username_0 on 2019-06-14)
  
  I'm thinking of using [Google's Workbox to pre-cache a bunch of files](https://developers.google.com/web/tools/workbox/guides/precache-files/), and I've been scratching my head as to how to generate unique hashes for files when they change. Something like this:
  
  ```
   workbox.precaching.precacheAndRoute([
      { 
        url: '/',
        revision: '383676',
      },
      { 
        url: '/about',
        revision: 213142,
      },
      { 
        town: '/location',
        revision: 742125,
      },
    ]);
  
  ```
  
  Ideally the revision number would change when the entry or related entries included in the page are changed/saved. I got this working but I'm not sure how to get related content to clear the cache:
  
  ```
  // service-worker.js
  
  {%- cache using key 'abc' -%}
      { 
        url: {{ craft.entries.id(64).one.uri }},
        revision: {{ random(100000, 999999) }},
      },
  {%- endcache -%}
  
  ```
  
  This seems to work when said entry is saved, but I'm not sure how to deal with a related entry being saved. I'm also not sure this is the most performant way to go.
  
  **Tags:** cache, routes
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-06-14. Score: 1 | Accepted answer
  
  So you shouldn't try to come up with a hashed file naming scheme for busting the cache; instead use one of Workbox's [Strategies](https://developers.google.com/web/tools/workbox/modules/workbox-strategies) -- I think a great pattern for what you're trying to do is [Stale While Revalidate](https://developers.google.com/web/fundamentals/instant-and-offline/offline-cookbook/#stale-while-revalidate)
  
  TL;DR Workbox has strategies for doing what you're trying to do.
  
  > **Comments:**
  > **username_0**: Thanks Andrew, but this is what I'm wondering: does re-validating mean going back to the server and requesting the page (and querying the DB) even if the page hasn't changed? I'd imagine that with a manifest, if the hashed file hasn't changed, then the SW would simply serve from the cache and stay away from the server until instructed to do so.  
  > **username_1**: There are many strategies you can adopt... my point is really that Workbox can handle this for you, you're not going to need template logic.  
  > **username_0**: It took me a while to wrap my head around all this, but the precache manifest is useful primarily for static assets (js, css...). In my case, stale-while-revalidate is probably the way to go for content, provided I can workout how to refresh the page when new content is available.  
  
  ---
  
  