1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Create a Progressive Web App with React and Register a Service Worker

    Chris AchardChris Achard
    reactReact
    16.6.3-16.8.6
    javascriptJavaScript

    A Progressive Web App (PWA) can cache static assets, which allows an app to be rendered faster, and even allows it to operate offline!

    We’ll start a new app with create-react-app, and enable the service worker by registering it in index.js. Then we’ll build and serve a production version of the app - and show that it can work offline by disabling the server and refreshing the tab.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Create a new app with Create React App, called Todo PWA. Then CD into the new directory and start the app with yarn start, which will automatically bring up the app in a new web browser.

    I have a basic web server prepared, which includes a simple express app that can serve a list of to-do items, allows you to make a new item and also to delete an item from the list. That is running on localhost port 4567.

    For the React app itself, I added Bootstrap, which will come from a CDN, and have put the entire to-do list app in app.js. In the browser, we have a working, regular to-do list app. This is just a regular web app so far.

    A progressive web app is enabled by a service worker, which we can see in index.js. Old versions of Create React App turned on the service worker by default. Starting with Create React App 2.0we have to manually enable the service worker by calling serviceWorker.register.

    By default, that uses a cache-first strategy to serve all of our assets generated by the react app. If we open serviceworker.js, we can look at the register function that we just called.

    One really important thing to notice is that the service worker only registers in production mode. You could change this file to work in dev mode too, but that's advised against in the documentation because of caching problems that can be frustrating during development.

    If you do decide to go that route, you'll also need to change the default serviceworker.js file name because of a default serviceworker.js file that is served from React scripts in development mode.

    Instead, we'll test the service worker by building a production version of the app with yarn build and then serve that production version with serve -s build and then open localhost port 5000, where we can see the app running.

    Since we've registered the service worker, if we refresh now and open the Chrome DevTools to the console, we can see the message from Workbox showing us that the pre-cache is responding to the static assets.

    In the Network tab, we can see that the JavaScript and SVG assets are all being served now by the service worker instead of by the dev server. Since they come directly from the cache, your entire app will load faster than if it required a network call for each of those assets, which is one of the primary benefits of registering the service worker.

    In fact, we can now actually turn off the app server. If we refresh the app in the browser, we can still see it. There's nothing serving on localhost port 5000, but the app still loads because the service worker has been registered and is serving a cached version of the production app assets.

    Notice though that the service worker is only serving the static assets. The to-do items are still being loaded from the network, from our express web server. If we turn that off as well and refresh now, the list of items won't load because it's still trying to make a network call for that.

    The default service worker implementation from Create React App will cache all the static assets, but not any of the dynamic network calls that your app makes.