Service workers have magical powers. To know more, read here.
If you are building Ember apps for your users who would be accessing them from mobile networks, it could mean they have extremely slow network connections on the move. In order to load your apps faster, service workers could be an effective strategy. ember-service-worker
is an Ember addon that takes care of service worker registrations, asset caching, etc.
To install ember-service-worker
in your Ember apps, go ahead and perform the following command:
ember install ember-service-worker
Doing the above, and starting your Ember app, you would see that your app has registered a service-worker.
Notice the sw-registration.js
file? You should also notice the following message that says “Service worker registration succeeded”.
In order to start asset caching, you need to install an ember-service-worker
plugin called ember-service-worker-asset-cache
.
ember install ember-service-worker-asset-cache
Once this addon is installed, inspect the Network tab on Chrome and you should see the assets (CSS and JS) are now served from “ServiceWorker”.
But if you notice, localhost (index.html) file is not cached. Don’t worry, another ember-service-worker
plugin called ember-service-worker-index
has got you covered.
ember install ember-service-worker-index
Well, you can now turn off your “Internet” and refresh the page, your app will still load. Apparently, your app is now “Offline first”.
Now that your assets and index.html files are cached, they should be faster on mobile networks that are slow.
-
If you had followed the above, step-by-step, you might not have seen some things don’t work as mentioned in the screenshots. This is likely because the service workers we installed along with the caching plugins, would have cached your assets. In Google Chrome, you can toggle the “Update on Reload” setting from the “Application” tab of Developer tools. Another way is to find your service worker from “Application” tab of Developer tools and hit “Unregister” and reload the page. Second time reload should resolve the issue.
-
Do not fingerprint service-worker files. By default,
sw-registration.js
file will not be fingerprinted. This is to make sure your app’s service worker registration and asset cache are not lost on rebuild and reload.