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.

Service Worker caches download links

See original GitHub issue

Is this a bug report?

Yes

Can you also reproduce the problem with npm 4.x?

Yes

Which terms did you search for in User Guide?

Service Worker and Caching

Environment

N/A

Steps to Reproduce

The default ServiceWorker appears to cache files that it has not seen or downloaded. I have an api call which basically generates a zip to download, and is accessible via a http link. When a user navigates to this download URL by clicking on a link, the service worker intercepts the request, and tries to service the default index page.

With the ServiceWorker active, it appears that it is trying to retrieve this file from cache rather than requesting it from the server.

  1. (I think this is important) Have the homepage set to "." in the package.json config.
  2. Have a link somewhere on your application that points to a file via an API call
  3. Click on the think with the service worker active

Expected Behavior

The browser would download the file (as it does without the service worker active)

Actual Behavior

The service worker intercepts the download and serves the normal index.html file.

Suggested Behaviour

Have the whitelist exposed as a configuration so that a set of paths don’t invoke the service worker.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
cheecommented, Sep 22, 2017

my advice here (not affiliated with create-react-app) is to build the service-worker.js yourself.

you don’t need to eject to take hold of this part of your project, you can leave the registerServiceWorker.js exactly as it is and do the following:

create a sw-precache config file:

// config/sw.js
module.exports = {
  staticFileGlobs: [
    'build/**/*.js',
    'build/**/*.css',
    'build/index.html'
  ],
  navigateFallback: '/index.html',
  // something like this should allow everything but files ending with `.zip`
  navigateFallbackWhitelist: [/^(?!.*[.]zip$).*$/],
  cacheId: 'my-magical-cache-machine'
}

then install sw-precache:

npm i -D sw-precache

and add something like this as part of your build script:

sw-precache --root='build/' --config config/sw.js 

this is what i’ve been doing in a couple of production applications for a month or two now and it works very nicely.

3reactions
yegodzcommented, May 3, 2018

never mind - just figured out.

You can overwrite create-react-app’s default service worker by running sw-precache directly in package.json after react-scripts build like so:

"build": "react-scripts build && cd build && sw-precache --config ../sw-precache-config.js",

Then you can add a sw-precache-config.js file in the root folder where you can control the options for the serviceworker created by sw-precache

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Service Workers - Web APIs | MDN
A service worker functions like a proxy server, allowing you to modify requests and responses replacing them with items from its own cache....
Read more >
HTML5 download link a[download] bypasses service-worker
It seems to me that HTML5 download links bypasses any registered Service-Worker? Is this intended design?
Read more >
Prefetching Links using Service Workers | by Chidume Nnamdi
If the browser support service worker, we download and register our service ... Now we need to open the cache storage using caches.open(…) ......
Read more >
Caching - web.dev
You can use the Cache Storage API to download, store, delete or update ... To manage the cache your service worker interacts with...
Read more >
468227 - Service worker doesn't get the fetch event ... - Monorail
Insert an image into ServiceWorker cache 2. Open URL in tab (Image displays normally) ... <a download> links aren't going through the service...
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