question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi, Can you please provide a demo code regarding how to alter the service-worker original logic?
I managed to append code, and I can’t manage to exclude some URL paths from being cached.

I tried the following code:

self.addEventListener('fetch', function (event) {
    console.log("Fetch url:", event.request.url); // tslint:disable-line
    if (event.request.url.indexOf('auth') > -1) {
         console.log("Returning false"); // tslint:disable-line
         return false;
     }
});

Logs are written, but the path is being cached anyway.

Also, if I want to replace the whole service worker CRA code, how can I predict webpack’s file names ahead? I don’t want to eject for this.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
tszarzynskicommented, Aug 24, 2018

@CodeJjang I played a little bit and this piece of code seems to do the job:

var blacklist = ['index.html'];

for (let [key, val] of urlsToCacheKeys) {
  if (blacklist.some(e => val.match(e))) {
    urlsToCacheKeys.delete(key);
  }
}

You just have to append it to original SW.

1reaction
tszarzynskicommented, Aug 24, 2018

@CodeJjang Ok, I get your point now.

First thing, the purpose of this script wasn’t to amend caching strategy of CRA. Only adding some extra features on top of it. Or replacing it completely if necessary. It’s a pretty dummy thing.

If I understand correctly you want to implement your own caching strategy.

I can suggest to solutions here:

  1. If you look into the code of Sw generated by CRA you’ll find an array called precacheConfig at the beginning of the file. I’d try to append a piece of code that filters out the elements that you don’t want to be cached from that list. Sounds a little bit hacky, but could possibly work if you just want to blacklist one thing.

  2. You can also completely remove SW generated by CRA and build your own one. Have a look at sw-precache. That’s what CRA is using under the hood. It supports glob pattern matching in a config file. So you don’t really have to know the actual filenames generated by CRA build script.

Does it make sense?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Demo Definition & Meaning - Merriam-Webster
a · an example of a product that is not yet ready to be sold ; b · a recording intended to show...
Read more >
demo - Wiktionary
(informal) A demonstration or visual explanation. · (informal) A recording of a song meant to demonstrate its overall sound for the purpose of...
Read more >
Demo - Wikipedia
Demo, usually short for demonstration, may refer to: Contents. 1 Music and film; 2 Computing and technology; 3 People; 4 Other uses; 5...
Read more >
Demo Definition & Meaning - Dictionary.com
to try out or exhibit the use of (a product, process, or the like): You can demo the game without downloading or buying...
Read more >
DEMO: HOME
Menswear has a legacy—DEMO boldly writes a new chapter inspired by its rich heritage. Classical elements are contrasted with contemporary styling to subvert ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found