⚠️ This lesson is retired and might contain outdated information.

Register a Service Worker in Production

Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 2 months ago

Having a Service Worker created is not enough, we still need to register it into the browser to make it work.

I’ll show you in this lesson how to register a service worker in the app.

Instructor: [00:00] To make it simple, I'm going to register the service worker in the main service file. We have to use the Navigator API, which has a service worker property with a register method. The register method takes the URL of the service worker, but not the path to the file.

[00:21] Just as you would do it if you want to open the service worker from the browser, the register method returns a promise. We can do them, and check if the service worker has been registered. Let's print a console log -- service worker is registered.

[00:40] There are some cases though where we don't want to register a service worker. For example, we have to check if the browser has support for service workers, by checking that the service worker property from Navigator exists.

[00:55] Let's create a variable for that condition, shouldServiceWorker. First, we have to check that the service worker property exists in Navigator, so we can use it to register the service worker conditionally. We only generate the service worker in production, when we run npm run build.

[01:20] In fact, the service worker webpack plugin doesn't work very well with the webpack dev server, so there is no point in trying to register the service worker in development mode. Let's add the other condition to register it only in production.

[01:34] Let's say production is equal to process.env.node_env equal to production. Then say shouldServiceWorker register when the service worker is in Navigator and is in production mode. In order to try the service worker registration, since we are using it only in product, we have to run npm run build.

[02:06] Then we will need some kind of http server, so I installed http server from npm. Run this, and then let's reload the URL.

[02:22] We see in the console, service worker registered. If we go to the application, we see here this service worker has been registered. We can also check in the cache storage that this entry has been created and these files have been cached.

[02:43] The application should be working offline. In order to try that, let's upload this application somewhere, for example by using search. Then I'm going to open this URL in the browser. Remember that service workers only works under https.

[03:11] Now our files have been cached, so if I go and turn off the WiFi and I reload here, it's still working. The only thing is, the images haven't been cached yet, but we'll see that later.

egghead
egghead
~ 2 hours ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today