Skip to content

Commit d9efb60

Browse files
committed
Merged PR #52
2 parents b741c71 + 9df62f0 commit d9efb60

File tree

7 files changed

+50
-27
lines changed

7 files changed

+50
-27
lines changed

dist/upup.min.js

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/upup.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/upup.sw.min.js

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/upup.sw.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/upup.zip

-140 Bytes
Binary file not shown.

docs/README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,21 @@ UpUp can only serve offline content for requests within its scope. The scope is
2929

3030
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.
3131

32-
**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).**
32+
**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).**
33+
34+
#### Scope - Advanced
35+
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).
36+
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).
37+
````html
38+
<script src="//cdnjs.cloudflare.com/ajax/libs/UpUp/0.1.0/upup.min.js"></script>
39+
<script>
40+
UpUp.start({
41+
'content-url': 'offline.html',
42+
'assets': ['/img/logo.png', '/css/style.css', 'headlines.json'],
43+
'service-worker-url': '/upup.sw.min.js'
44+
});
45+
</script>
46+
````
3347

3448
## Settings
3549

@@ -45,9 +59,11 @@ same settings object directly to the start() method.
4559
UpUp.start({ content: 'Cannot reach site. Please check your internet connection.' });
4660

4761
The settings object supports the following options:
48-
- `content-url` (String) The content to display when user is offline (url to the content that will be served)
49-
- `content` (String) The content to display when user is offline (plain text, HTML, etc.)
50-
- `assets` (Array) Array of assets to cache for offline access
62+
- `content-url` (String) The content to display when user is offline (url to the content that will be served)
63+
- `content` (String) The content to display when user is offline (plain text, HTML, etc.)
64+
- `assets` (Array) Array of assets to cache for offline access
65+
- `service-worker-url` (String) The url to the service worker file (`upup.sw.min.js`)
66+
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))
5167

5268
# API Reference
5369

src/upup.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,21 @@
3535
*
3636
* 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.
3737
*
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+
* ````
3953
*
4054
* ## Settings
4155
*
@@ -52,9 +66,11 @@
5266
*
5367
*
5468
* 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))
5874
*
5975
* # API Reference
6076
*/
@@ -75,7 +91,7 @@
7591

7692
// Settings live here, and these are their defaults
7793
var _settings = {
78-
'script': 'upup.sw.min.js'
94+
'service-worker-url': 'upup.sw.min.js'
7995
};
8096

8197
var _debugState = false;
@@ -104,7 +120,7 @@
104120
this.addSettings(settings);
105121

106122
// register the service worker
107-
_serviceWorker.register(_settings.script, {scope: './'}).then(function(registration) {
123+
_serviceWorker.register(_settings['service-worker-url'], {scope: './'}).then(function(registration) {
108124
// Registration was successful
109125
if (_debugState) {
110126
console.log('ServiceWorker registration successful with scope: %c'+registration.scope, _debugStyle);
@@ -149,8 +165,10 @@
149165
}
150166

151167
// 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+
}
154172
});
155173
},
156174

0 commit comments

Comments
 (0)