1 | ---
|
2 | layout: doc_page
|
3 | title: Getting started
|
4 | description: Make your app offline first in about 30 minutes
|
5 | order: 1
|
6 | sections:
|
7 | - anchor: installation
|
8 | title: Installation
|
9 | - anchor: offline-first
|
10 | title: Offline first
|
11 | ---
|
12 |
|
13 |
|
14 | ### Installation
|
15 |
|
16 | To get started with Ember Service Worker, install the addon in your exisiting
|
17 | Ember.js project:
|
18 |
|
19 | {% highlight shell %}
|
20 | ember install ember-service-worker
|
21 | {% endhighlight %}
|
22 |
|
23 | If you now rebuild and visit your Ember app it should install a Service Worker
|
24 | when the page loads.
|
25 |
|
26 | ### Offline first
|
27 |
|
28 | Let's now add some plugins to make the app offline first. This is done by
|
29 | installing some aditional addons:
|
30 |
|
31 | {% highlight shell %}
|
32 | ember install ember-service-worker-index
|
33 | ember install ember-service-worker-asset-cache
|
34 | {% endhighlight %}
|
35 |
|
36 | These plugins will take care of caching your `index.html` page and static
|
37 | assets. If you again restart rebuild and visit your Ember app, it caches
|
38 | your `index.html` file and static assets. If you now disconnect your internet
|
39 | connection (or turn on 'offline' mode in your browser's dev tools) and refresh
|
40 | the page, your app should still load!
|
41 |
|
42 | There might be a small gotcha though, that is it doesn't cache any non-static
|
43 | resources, like requests to an API. To do this, we need to install another
|
44 | plugin:
|
45 |
|
46 | {% highlight shell %}
|
47 | ember install ember-service-worker-cache-fallback
|
48 | {% endhighlight %}
|
49 |
|
50 | This plugin will cache any request that you configure it to do so, but will only
|
51 | serve the contents from the cache when the resource won't load.
|
52 | To configure which resources it should cache we need to add some configuration to
|
53 | the `ember-cli-build.js` file that should be in the root of your Ember.js
|
54 | project. The configuration depends on what kind of resources your app loads.
|
55 | Here's an example configuration:
|
56 |
|
57 | {% highlight javascript %}
|
58 | var EmberApp = require('ember-cli/lib/broccoli/ember-app');
|
59 |
|
60 | module.exports = function(defaults) {
|
61 | var app = new EmberApp(defaults, {
|
62 | 'esw-cache-fallback': {
|
63 | patterns: [
|
64 | '/api/v1/(.+)'
|
65 | ],
|
66 | }
|
67 | });
|
68 |
|
69 | return app.toTree();
|
70 | };
|
71 | {% endhighlight %}
|
72 |
|
73 | The `patterns` property in the `esw-cache-fallback` configuration should be an
|
74 | array of regular expressions that describe the URLs it can cache. In the example
|
75 | it's configured to cache anything resource of which the URL starts with `/api/v1/`.
|
76 |
|
77 | Update the configuration to your liking and then rebuild and visit your app
|
78 | again. Now browse around your app for a few moments while online to prime the
|
79 | fallback cache, afterwards put your browser into offline mode and try to load a
|
80 | page you have visited before. If everything went well, it should now also serve
|
81 | your API responses from the cache.
|
82 |
|
83 | Congratulations! Your app is now offline first.
|