Skip to content

Commit

Permalink
Add: service worker for cache
Browse files Browse the repository at this point in the history
  • Loading branch information
qinwf committed Nov 16, 2016
1 parent 29c65d7 commit fba8b98
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
10 changes: 10 additions & 0 deletions _layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ <h3 class="masthead-title">
.,... .+...
Join us! https://rweekly.org/about.html `, "font-family:monospace")

if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
}
</script>

{% include inf.html %}
Expand Down
23 changes: 23 additions & 0 deletions bower_components/sw-toolbox/companion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
(function() {
'use strict';
var workerScript = document.currentScript && document.currentScript.dataset.serviceWorker;

if (workerScript && 'serviceWorker' in navigator) {
navigator.serviceWorker.register(workerScript);
}
})();
16 changes: 16 additions & 0 deletions bower_components/sw-toolbox/sw-toolbox.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bower_components/sw-toolbox/sw-toolbox.js.map

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(global => {
'use strict';

// Load the sw-tookbox library.
importScripts('/bower_components/sw-toolbox/sw-toolbox.js');

// Turn on debug logging, visible in the Developer Tools' console.
global.toolbox.options.debug = true;

// Set up a handler for HTTP GET requests:
// - /\.ytimg\.com\// will match any requests whose URL contains 'ytimg.com'.
// A narrower RegExp could be used, but just checking for ytimg.com anywhere
// in the URL should be fine for this sample.
// - toolbox.cacheFirst let us to use the predefined cache strategy for those
// requests.
// global.toolbox.router.get(/\.(cloudflare)|(googleapis)\.com\//, global.toolbox.cacheFirst, {
// // Use a dedicated cache for the responses, separate from the default cache.
// cache: {
// name: 'cdnjs',
// // Expire any entries that are older than 30 seconds.
// maxAgeSeconds: 60 * 60 * 100
// }
// });

global.toolbox.precache(['/index.html', '/random.html', '/about.html',"/","/page2","/page2/index.html","/archive.html"]);
// global.toolbox.options.cache.maxAgeSeconds = 60 * 15;

// Request the resource from both the cache and the network in parallel.
// Respond with whichever returns first. Usually this will be the cached
// version, if there is one. On the one hand this strategy will always make a
// network request, even if the resource is cached. On the other hand,
// if/when the network request completes the cache is updated, so that future
// cache reads will be more up-to-date.
global.toolbox.router.default = global.toolbox.fastest;

// Boilerplate to ensure our service worker takes control of the page as soon
// as possible.
global.addEventListener('install',
event => event.waitUntil(global.skipWaiting()));
global.addEventListener('activate',
event => event.waitUntil(global.clients.claim()));
})(self);

0 comments on commit fba8b98

Please sign in to comment.