|
35 | 35 | * |
36 | 36 | * This means that if you placed the files in your `/js/` directory, UpUp will only be able to show your offline content when users try to look inside the `/js/` directory. |
37 | 37 | * |
38 | | - * **This is why you should always place the script as close to the root of your site as possible (e.g. https://www.talater.com/upup.min.js).** |
| 38 | + * **This is why you should almost always place the script as close to the root of your site as possible (e.g. https://www.talater.com/upup.min.js).** |
| 39 | + * |
| 40 | + * #### Scope - Advanced |
| 41 | + * It is possible to keep `upup.min.js` outside the scope (e.g. in a CDN), as long as `upup.sw.min.js` is kept local (that file's location determines the scope). |
| 42 | + * If you choose to keep the two in separate directories, make sure to pass the `service-worker-url` [setting](https://github.com/TalAter/UpUp/tree/master/docs#settings). |
| 43 | + * ````html |
| 44 | + * <script src="//cdnjs.cloudflare.com/ajax/libs/UpUp/0.1.0/upup.min.js"></script> |
| 45 | + * <script> |
| 46 | + * UpUp.start({ |
| 47 | + * 'content-url': 'offline.html', |
| 48 | + * 'assets': ['/img/logo.png', '/css/style.css', 'headlines.json'], |
| 49 | + * 'service-worker-url': '/upup.sw.min.js' |
| 50 | + * }); |
| 51 | + * </script> |
| 52 | + * ```` |
39 | 53 | * |
40 | 54 | * ## Settings |
41 | 55 | * |
|
52 | 66 | * |
53 | 67 | * |
54 | 68 | * The settings object supports the following options: |
55 | | - * - `content-url` (String) The content to display when user is offline (url to the content that will be served) |
56 | | - * - `content` (String) The content to display when user is offline (plain text, HTML, etc.) |
57 | | - * - `assets` (Array) Array of assets to cache for offline access |
| 69 | + * - `content-url` (String) The content to display when user is offline (url to the content that will be served) |
| 70 | + * - `content` (String) The content to display when user is offline (plain text, HTML, etc.) |
| 71 | + * - `assets` (Array) Array of assets to cache for offline access |
| 72 | + * - `service-worker-url` (String) The url to the service worker file (`upup.sw.min.js`) |
| 73 | + * Allows loading `upup.min.js` from a CDN while `upup.sw.min.js` stays local (see [scope](https://github.com/TalAter/UpUp/blob/master/docs/README.md#scope)) |
58 | 74 | * |
59 | 75 | * # API Reference |
60 | 76 | */ |
|
75 | 91 |
|
76 | 92 | // Settings live here, and these are their defaults |
77 | 93 | var _settings = { |
78 | | - 'script': 'upup.sw.min.js' |
| 94 | + 'service-worker-url': 'upup.sw.min.js' |
79 | 95 | }; |
80 | 96 |
|
81 | 97 | var _debugState = false; |
|
104 | 120 | this.addSettings(settings); |
105 | 121 |
|
106 | 122 | // register the service worker |
107 | | - _serviceWorker.register(_settings.script, {scope: './'}).then(function(registration) { |
| 123 | + _serviceWorker.register(_settings['service-worker-url'], {scope: './'}).then(function(registration) { |
108 | 124 | // Registration was successful |
109 | 125 | if (_debugState) { |
110 | 126 | console.log('ServiceWorker registration successful with scope: %c'+registration.scope, _debugStyle); |
|
149 | 165 | } |
150 | 166 |
|
151 | 167 | // add new settings to our settings object |
152 | | - ['content', 'content-url', 'assets'].forEach(function(settingName) { |
153 | | - _settings[settingName] = settings[settingName] || null; |
| 168 | + ['content', 'content-url', 'assets', 'service-worker-url'].forEach(function(settingName) { |
| 169 | + if (settings[settingName] !== undefined) { |
| 170 | + _settings[settingName] = settings[settingName]; |
| 171 | + } |
154 | 172 | }); |
155 | 173 | }, |
156 | 174 |
|
|
0 commit comments