Confusion about adding code to service-workers.js
See original GitHub issueHi
I’m a bit confused and I think there is a quick answer to this:
As I have understood, this boilerplate is creating it’s own service-worker.js
file.
But what do I do when I want to add code referring to the the self
object?
Example code:
self.addEventListener('notificationclick', function(event) {...})
By reading tutorials and documentation on service workers, I have the opinion that the code above should be placed inside the service-worker.js
file. But what to do when it is generated?
Am I on the wrong path or is there something I am missing?
All answers is much appreciated!
Issue Analytics
- State:
- Created 6 years ago
- Comments:5
Top Results From Across the Web
Service Workers in JS and offline reading - Medium
Before installing a Service Worker, you need to register one from your main JavaScript file. This can be done thanks to the method...
Read more >javascript - Confused on service worker with NextJS & Express
So if I create the server using http instead of Express then the service worker will work. Here's a repo with the code...
Read more >Service workers tutorial | Uploadcare Blog
To summarize, a web worker is a JavaScript file that runs in a different thread, but still can communicate with our application code....
Read more >Using Service Workers - Web APIs | MDN
The service worker code is in a JavaScript file residing inside our app (note this is the file's URL relative to the origin,...
Read more >Simple Use Case for Service Workers | by John Tucker
We first add the service worker loader code to… index.html. and create the service worker code… worker.js ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We had a similar problem while working on a project recently and we didn’t want to “eject”. Instead, we created a little tool that allows you to append custom service worker code to the one generated by CRA. It’s not an ideal solution but worked well for us.
Have a look here: https://github.com/bbhlondon/cra-append-sw
eject it and check the
sw-precache-webpack-plugin
inwebpack.config.prod.js
. You can import your script with the optionimportScripts
. https://github.com/goldhand/sw-precache-webpack-plugin#importscripts-usage-example(#2253)