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

[PWA] Use Workbox instead of sw-precache #2340

Closed
Sgiath opened this issue May 23, 2017 · 21 comments
Closed

[PWA] Use Workbox instead of sw-precache #2340

Sgiath opened this issue May 23, 2017 · 21 comments
Milestone

Comments

@Sgiath
Copy link

Sgiath commented May 23, 2017

It would be nice to use Workbox instead of sw-pracache for genrating service-worker.js file since it is officially supported by Google and has more features than sw-pracache. It has also official Webpack plugin so integration shouldn't be hard.
Some more info about Workbox is here https://developers.google.com/web/tools/workbox/

@Sgiath Sgiath changed the title PWA use Workbox instead of sw-precache [PWA] Use Workbox instead of sw-precache May 23, 2017
@wtgtybhertgeghgtwtg
Copy link
Contributor

It's not the best argument, but switching to workbox-sw would trim the dependency tree, too, since sw-precache depends on meow and update-notifier.

@gaearon
Copy link
Contributor

gaearon commented May 26, 2017

I don't really know much about either.
@jeffposnick Can you provide your insights?

@jeffposnick
Copy link
Contributor

We just launched Workbox v1.0 last week, and I think it would be prudent to give it some time to mature before trying to integrate it into a general-purpose project like create-react-app. (sw-precache is a mature project, having been around since early 2015.)

In the medium-term, yes, migration should hopefully be a smooth process and would make sense. Maybe keep this issue open to track that?

CC: @addyosmani

@gaearon
Copy link
Contributor

gaearon commented May 26, 2017

Sure, sounds good.

@sidharthachatterjee
Copy link

It's been a while since Workbox landed v1.0. I've used it in a few production apps and I think its mature enough to include in CRA.

What do you think, @gaearon and @jeffposnick? I'd be happy to work on a PR for this if you agree.

@jeffposnick
Copy link
Contributor

Hello @sidharthachatterjee! I'm glad that Workbox has been... working... for you 😄

We're working on a pretty major rewrite of the Webpack integration as part of our upcoming Workbox v3 release, and have felt that once that's ready, it would be the right time to start moving over projects like create-react-app to use Workbox.

Moving over to Workbox now would lead to another potentially large transition when we went from v2 to v3.

CC: @goldhand @addyosmani

@bob-lee
Copy link

bob-lee commented Dec 31, 2017

@jeffposnick trying to convert my personal project which was built on Angular and workbox-build@2.1.1 for caching static assets and dynamic contents of api calls, etc. to React using c-r-a. Wondering if it is possible to accomplish a 'complete' offline capability without doing eject?

Edit: Without doing eject, I could replace the default service-worker with workbox-generated one to add runtime caching, etc. Anyone interested can refer these changes:

// in registerServiceWorker.js
      const swUrl = `${process.env.PUBLIC_URL}/sw-default.js`;
      // const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;

@gaearon
Copy link
Contributor

gaearon commented Jan 8, 2018

@jeffposnick What's the current status on this?

@jeffposnick
Copy link
Contributor

Workbox v3 is currently in alpha, with a target for final release towards the end of the month: https://github.com/GoogleChrome/workbox/releases

Moving to Workbox v3 would give a nicer local development experience, as it includes logging information about what the service worker's doing enabled by default on localhost.

I've been locally testing out Workbox integration with projects like c-r-a and there shouldn't be any technical reason why we couldn't migrate in the future, assuming service worker generation remains part of the c-r-a project.

@davejm
Copy link
Contributor

davejm commented Mar 16, 2018

@jeffposnick @Sgiath @gaearon @wtgtybhertgeghgtwtg @sidharthachatterjee I just published an npm package that can remove the SWPrecacheWebpackPlugin and replace it with Workbox webpack plugins, allowing you to fully generate the service worker or injecting the precache assets into a custom service worker - all without ejecting.
Hopefully can be of some use until Workbox is integrated in c-r-a or for people that want config options/custom service worker without ejecting or forking! The default config should do the same as what the current master branch of c-r-a does.

https://www.npmjs.com/package/react-app-rewire-workbox

@davejm
Copy link
Contributor

davejm commented Mar 17, 2018

I'm going to try and make a PR to actually get this into c-r-a. My current idea to be able to customise the config is to look for a file like e.g. '.workbox-config.js' in the root directory. Would that be ok?

@vaibhavi3t
Copy link

@jeffposnick I have a very complex project which is built on CRA. Currently using sw-precache for caching, I want to use workbox for same. How can I do it?

@simonedavico
Copy link

@vaibhavi3t I think your best bet right now is https://github.com/davejm/react-app-rewire-workbox

@braj065
Copy link

braj065 commented Jun 16, 2018

@vaibhavi3t I have setup my CRA project to work with workbox 3.0. The setup is complete but i am stuck at one point. Let me describe the setup
i) Under root of the CRA project create a file 'workbox-config.js'. Whose content should be as follows
module.exports = {
globDirectory: "build/",
globPatterns: [
"**/*.{json,ico,html,js,css,woff2,woff}"
],
swSrc: "./src/myservice-worker.js",
swDest: "./build/myservice-worker.js",
globIgnores: ["../workbox-config.js", "asset-manifest.json"]
};

The glob pattern you need to figure out according to your preference.
ii) Then create a file in src/myservice-worker.js. In which mandatory things are
importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/3.2.0/workbox-sw.js"
);
workbox.skipWaiting();
workbox.clientsClaim();
Apart from this you can use registerRoute() and other eventListeners. Remember workbox 3.0 doesn't needs a constructor to create workbox object.

iii)Create a function to register our custom myservice-worker.js in src/mysw-register.js
export default function LocalServiceWorkerRegister() {
const swPath = ${process.env.PUBLIC_URL}/myservice-worker.js;
console.log("PUBURL", process.env.PUBLIC_URL);
if ("serviceWorker" in navigator) {
console.log("1");
window.addEventListener("load", function() {
console.log("2");
navigator.serviceWorker.register(swPath).then(
function(registration) {
console.log("3");
// Registration was successful
console.log(
"ServiceWorker registration successful with scope: ",
registration.scope
);
},
function(err) {
// registration failed :(
console.log("ServiceWorker registration failed: ", err);
}
);
});
}
}

iv) Modify src/index.js by removing existing 'registerServiceWorker' call and add your own two lines to call your registration
import LocalServiceWorkerRegister from "./mysw-register";
LocalServiceWorkerRegister();

v) Now modify package.json build script
"build": "react-scripts build && workbox injectManifest workbox-config.js"

vi) npm install workbox-cli and workbox-sw in prod scope(not in dev scope).
vii) Now run 'npm run build' and then 'serve -s build'(after npm install -g serve)

@vaibhavi3t If you have any clue about below problem please share

Now @jeffposnick i seek your help. In my react app i have different components and path for them mentioned in src/App.js
div className="container" Route exact path="/initial" component={Initial} / Route exact path="/orderreceive" component={Receive} Route exact path="/orderdeliver" component={Deliver} div

To cache them, i am not sure whether i can precache them or use registerroute(), what should i do. I tried with both precache and registerRoute() but the components are not getting cached. I ran only the frontend-'serve -s build' and both nodejs backend and frontend, still the components are not getting cached. Index.html gets cached but the div id='root' is always empty. I am running it in localhost. Please provide an example if possible.

@albert-olive
Copy link

I succeded adding workbox in CRA but I have some doubts. In the cache storage I keep having old sw-precache storage. So now I have duplicated storages. If you see the image, I'm not calling anymore the old service worker.

image

Also I don't know why but workbox create two, one it finishes with -temp and has nothing inisde and the other is were I have all the data.

image

@kevinah95
Copy link

@simonedavico I wrote a Medium inspired in your comment. If anyone is interested in how to use the plugin, I am glad to share it: Using Workbox with Create React App (Without Ejecting)

@Twaha-Rahman
Copy link

Twaha-Rahman commented Aug 25, 2018

@gaearon So, what is the progress so far? Can anybody tell me when Workbox will become a part of CRA. I just want to know this because I am going to create a PWA in the near future...

@jeffposnick
Copy link
Contributor

There's an open PR containing a Workbox implementation: #4169

@Twaha-Rahman
Copy link

There's an open PR containing a Workbox implementation: #4169

Thanks!

@Timer Timer closed this as completed Nov 1, 2018
@Timer Timer removed this from the 100.0.0 milestone Nov 1, 2018
@Timer Timer added this to the 2.x milestone Nov 1, 2018
@helloitsm3
Copy link

What's the progress for workbox to work on CRA?

@jeffposnick
Copy link
Contributor

The current version of c-r-a uses Workbox for service worker generation instead of sw-precache.

You need to opt-in to registering the service worker, though.

https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

@lock lock bot locked and limited conversation to collaborators Jan 8, 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