Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow runtimeCaching option without ejecting #3137

Closed
jliebrand opened this issue Sep 15, 2017 · 5 comments
Closed

Allow runtimeCaching option without ejecting #3137

jliebrand opened this issue Sep 15, 2017 · 5 comments

Comments

@jliebrand
Copy link

This is more of a feature request than a bug. But I love the fact CRA comes with a working SW. However, I'd like like to add another domain to the things which our automatically cached. But I'd really like to not have to eject.

Is there anyway to make the runtimeCaching option available through the package json? Or perhaps a way to get to the SW at runtime to configure it with additional domains to cache?

@stereobooster
Copy link
Contributor

stereobooster commented Sep 18, 2017

One way to solve is in the same manner as c-r-a deals with sass, e.g. does not create built-in solution, but creates easy-to-integrate outside solution

yarn build && yarn regenerate-sw

where yarn regenerate-sw will read contents of manifest.json, add more resources (what ever you need) and generate new service-worker.js

@jliebrand
Copy link
Author

ah great; i'll look in to that (although slightly unclear right now how i'd "add more resources" or indeed change the runtimeCaching option for specific domains)

@stereobooster
Copy link
Contributor

stereobooster commented Sep 18, 2017

I'm looking into this too (have similar issue). Custom SW is just a tip of the iceberg. There are also UI problems, and need to choose proper strategy.

This can be a starting point https://github.com/codebusking/next-hnpwa-guide-kit/blob/master/generate-sw.js

UPD:

package.json

"build": "react-scripts build && yarn run generate-sw",
"generate-sw": "sw-precache --root=build --config scripts/sw-precache-config.js && uglifyjs build/service-worker.js -o build/service-worker.js"

scripts/sw-precache-config.js

module.exports = {
  staticFileGlobs: [
    'build/static/css/*.css',
    'build/static/js/*.js',
    'build/index.html',
    // add more here
  ],
  stripPrefix: 'build',
  runtimeCaching: [{
    urlPattern: /images/,
    handler: 'cacheFirst'
  }]
};

@stereobooster
Copy link
Contributor

stereobooster commented Sep 18, 2017

@Swizec you might be interested in this issue too.

Also important bits:

I'm using events to pass infor about service worker to UI

console.log('New content is available; please refresh.');
let event = new CustomEvent("serviceNotification", {
  detail: {
    state: 'newContent'
  }
});
window.document.dispatchEvent(event);

Other interesting thought: if you use redux you can refresh UI without user action (theoretically, like HMR #2304).

@Timer
Copy link
Contributor

Timer commented Sep 26, 2018

Dupe of #4169

@Timer Timer closed this as completed Sep 26, 2018
@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants