Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit

Permalink
[TASK] Add simple offline service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
xf- committed Mar 11, 2018
1 parent 8e8cdd6 commit 87eb98f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ insert_final_newline = true
[*.{js,html,scss,json,yml,md}]
indent_size = 2
indent_style = space


[assets/favicon/manifest.json]
indent_size = 4
5 changes: 4 additions & 1 deletion assets/favicon/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "Meshviewer",
"short_name": "Meshviewer",
"icons": [
{
"src": "./android-chrome-192x192.png",
Expand All @@ -14,6 +15,8 @@
],
"theme_color": "#dc0067",
"background_color": "#dc0067",
"start_url": ".",
"display": "standalone",
"orientation": "portrait"
}
}

2 changes: 1 addition & 1 deletion gulp/tasks/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = function (gulp, plugins, config) {
return function copy() {
gulp.src(['html/*.html', 'assets/favicon/*'])
.pipe(gulp.dest(config.build));
gulp.src(['assets/logo.svg'])
gulp.src(['assets/logo.svg', 'service-worker.js'])
.pipe(gulp.dest(config.build));
gulp.src(['node_modules/promise-polyfill/dist/promise.js', 'polyfill.js'])
.pipe(gulp.dest(config.build + '/vendor'));
Expand Down
23 changes: 23 additions & 0 deletions html/offline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Freifunk Regensburg e.V. - Meshviewer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="main.css" inline>
</head>
<body>
<div class="loader">
<p>
Your are Offline!<br />
<img inline src="logo.svg" class="spinner" alt="Loading ..."/>
<br />
No connection available.
<br /><br /><button onclick="location.reload(true)" class="btn text" aria-label="Try to reload">Try to reload</button><br />
</p>
<noscript>
<strong>JavaScript required</strong>
</noscript>
</div>
</body>
</html>
4 changes: 4 additions & 0 deletions polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ if (typeof Object.assign !== 'function') {

window.CustomEvent = CustomEvent;
})();

if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service-worker.js');
}
23 changes: 23 additions & 0 deletions service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
self.addEventListener('install', function (event) {
var offlineRequest = new Request('offline.html');
event.waitUntil(
fetch(offlineRequest).then(function (response) {
return caches.open('offline').then(function (cache) {
return cache.put(offlineRequest, response);
});
})
);
});

self.addEventListener('fetch', function (event) {
var request = event.request;
if (request.method === 'GET') {
event.respondWith(
fetch(request).catch(function () {
return caches.open('offline').then(function (cache) {
return cache.match('offline.html');
});
})
);
}
});

0 comments on commit 87eb98f

Please sign in to comment.