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.

Confusion about adding code to service-workers.js

See original GitHub issue

Hi

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:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
tszarzynskicommented, Nov 22, 2017

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

1reaction
Means88commented, Sep 22, 2017

eject it and check the sw-precache-webpack-plugin in webpack.config.prod.js. You can import your script with the option importScripts . https://github.com/goldhand/sw-precache-webpack-plugin#importscripts-usage-example

(#2253)

Read more comments on GitHub >

github_iconTop 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 >

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