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

Add a default service-worker #1165

Closed
anz000 opened this issue Nov 20, 2019 · 12 comments
Closed

Add a default service-worker #1165

anz000 opened this issue Nov 20, 2019 · 12 comments
Labels

Comments

@anz000
Copy link

anz000 commented Nov 20, 2019

Any updates on [ Implement Service Worker caching ] - #276 ?

Or, I'd love to hear about any workarounds people have figured out. Thanks.

@fivethreeo
Copy link
Collaborator

I could try doing a razzle plugin if someone tells/shows me a example of how it should work.

@anz000
Copy link
Author

anz000 commented Nov 21, 2019

Some one has a plugin related to this @ https://www.npmjs.com/package/razzle-plugin-serviceworker . I couldn’t get it to work. I ran into some issues with the library it was running under the hood.

I ran through jared’s commit @ e5a0248 . I could get the file service-worker.js to load in the browser, however I couldn't get it to work as expected. The commit is pretty self - explanatory of what he wanted to do.

@fivethreeo
Copy link
Collaborator

Did you try razzle-plugin-serviceworker with yarn start or yarn start:prod?

@anz000
Copy link
Author

anz000 commented Nov 21, 2019

Nope. I had tested it on the build version. I even deployed it to my test server to test it.

@fivethreeo
Copy link
Collaborator

Using any other plugins or code splitting?

@anz000
Copy link
Author

anz000 commented Nov 22, 2019

As it turns out, it was my mistake. I had put the razzle.config.js file inside the src directory but it was supposed to go in the root directory.

I deployed and it's working. My only concern is previously it had service-worker.js file registered for the domain. The plugin creates sw.js by default. I hope that the updated service worker overwrites the old one on clients that had the old service-worker with a different name. I might as well override the name so that it is more clearer to people who might work on the project. Thanks.

@anz000
Copy link
Author

anz000 commented Nov 22, 2019

Further update based on the behavior I saw:
[service-worker.js : old] & [sw.js : new]

After name change:
On load, the sw.js doesn't gets installed, when forceRefreshed it gets installed but waits to be activated. The service-worker.js is still functioning. I load the application in a different tab, I see the same old content with service-worker.js still active. Hard Refresh didn't help too.

Closed all tabs that had the application running, and then hit the application. The sw.js is now active with the new contents.

The sw.js could have been activated forcefully (programmatically as well as through console). The service-worker.js was active because there were clients using it. If sw.js were allowed to be active rightaway then there would be two different version of the application in different tabs. Once service-worker.js is not serving any clients, then sw.js kicks in and becomes active.

I tried changing the name of the service-worker in razzle.config.js file, but couldn't get it working. I'm hoping that the service-worker will eventually expire on end-users browsers (looks like 1 day), and new one kick in.

@stale stale bot added the stale label Jan 21, 2020
@nimaa77
Copy link
Collaborator

nimaa77 commented Mar 17, 2020

consider using workbox package :)
it's easy and awesome

and I think you should move this issue to razzle-plugin-serviceworker

@nimaa77 nimaa77 closed this as completed Mar 17, 2020
@NickCis
Copy link
Contributor

NickCis commented Mar 29, 2020

On most projects that I've built with razzle, I end up copying create react app's workbox configuration and sw registration.

I've written a razzle plugin that does just that: razzle-plugin-workbox feel free to use it if you find it useful :)

@behnam-maqsudi
Copy link

@NickCis how can i use injectManifest in razzle-plugin-workbox?

@NickCis
Copy link
Contributor

NickCis commented Aug 29, 2020

@benamm you can't. The plugin is just a wrapper for WorkboxWebpackPlugin.GenerateSW, you can check the source code here. If you want to add the injectManifest feature, you can send the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants