Skip to content

Commit

Permalink
Adding offline support notification
Browse files Browse the repository at this point in the history
When all offline files are cached, hnpwa displays a status icon
  • Loading branch information
johnbeatty committed Oct 22, 2020
1 parent 70678d9 commit 46e9271
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 21 deletions.
3 changes: 3 additions & 0 deletions app/assets/images/cloud-check.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions app/assets/images/cloud-download.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions app/javascript/controllers/service_worker_controller.js
@@ -0,0 +1,51 @@
import { Controller } from "stimulus";

export default class extends Controller {
static targets = ["pageSavedNotice", "savingPageNotice"];

initialize() {
if (navigator.serviceWorker) {
if (navigator.serviceWorker.controller) {
this.stateChange();
} else {
navigator.serviceWorker
.register("/service-worker.js", { scope: "./" })
.then(function (reg) {
console.log("[Companion]", "Service worker registered!");
console.log(reg);
});
navigator.serviceWorker.addEventListener(
"controllerchange",
this.controllerChange.bind(this)
);
}
}
}

controllerChange(event) {
console.log(
'[controllerchange] A "controllerchange" event has happened ' +
"within navigator.serviceWorker: ",
event
);
navigator.serviceWorker.controller.addEventListener(
"statechange",
this.stateChange.bind(this)
);
}

stateChange() {
let state = navigator.serviceWorker.controller.state;
console.log(
"[controllerchange][statechange] " + 'A "statechange" has occured: ',
navigator.serviceWorker.controller.state,
" state: ",
state
);

if (state === "activated" || state === "redundant") {
this.savingPageNoticeTarget.classList.add("is-hidden");
this.pageSavedNoticeTarget.classList.remove("is-hidden");
}
}
}
22 changes: 7 additions & 15 deletions app/javascript/packs/application.js
Expand Up @@ -7,20 +7,12 @@
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("@rails/ujs").start();
require("turbolinks").start();
require("@rails/activestorage").start();
require("channels");

import "controllers"
import "controllers";

import LocalTime from "local-time"
LocalTime.start()

if (navigator.serviceWorker) {
navigator.serviceWorker.register('/service-worker.js', { scope: './' })
.then(function(reg) {
console.log('[Companion]', 'Service worker registered!');
console.log(reg);
});
}
import LocalTime from "local-time";
LocalTime.start();
4 changes: 4 additions & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -19,6 +19,10 @@
<a class="navbar-item" href="/">
<%= image_tag "train_48.png", width: "23px", alt: "Home Page"%>
</a>
<div class="navbar-item" data-controller="service-worker">
<%= image_tag "cloud-check.svg", width: "23px", alt: "Webpage saved for offline use", data: { target:"service-worker.pageSavedNotice" }, class: "is-hidden" %>
<%= image_tag "cloud-download.svg", width: "23px", alt: "Saving webpage for offline use", data: { target:"service-worker.savingPageNotice" }, class: "" %>
</div>
<a role="button" class="navbar-burger burger" aria-label="menu" aria-expanded="false" data-target="bulma-navbar.burger" data-action="bulma-navbar#toggle">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
Expand Down
32 changes: 26 additions & 6 deletions app/views/service_worker/service_worker.js.erb
@@ -1,15 +1,33 @@
var CACHE_VERSION = 'v1';
var CACHE_NAME = CACHE_VERSION + ':sw-cache-';
var REQUIRED_FILES = [
'<%= asset_pack_path 'application.js' %>',
'<%= stylesheet_path 'application.css' %>',
'/',
'/top',
'/new',
'/show',
'/ask',
'/job',
'/offline.html',
'<%= asset_path 'cloud-check.svg' %>',
'<%= asset_path 'cloud-download.svg' %>',
'<%= asset_path 'train_48.png' %>',
'<%= asset_path 'train_192.png' %>',
'<%= asset_path 'train_512.png' %>',
];

function onInstall(event) {
console.log('[Serviceworker]', "Installing!", event);
event.waitUntil(
caches.open(CACHE_NAME).then(function prefill(cache) {
return cache.addAll([
'<%= asset_pack_path 'application.js' %>',
'<%= stylesheet_path 'application.css' %>',
'/offline.html',
]);
caches.open(CACHE_NAME)
.then(function prefill(cache) {
console.log('[install] Caches opened');
return cache.addAll(REQUIRED_FILES);
})
.then(function() {
console.log('[install] All required resources have been cached');
return self.skipWaiting();
})
);
}
Expand All @@ -30,6 +48,8 @@ function onActivate(event) {
);
})
);
console.log('[activate] Claiming ServiceWorker');
event.waitUntil(self.clients.claim());
}

// Borrowed from https://github.com/TalAter/UpUp
Expand Down

0 comments on commit 46e9271

Please sign in to comment.