Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When all offline files are cached, hnpwa displays a status icon
- Loading branch information
1 parent
70678d9
commit 46e9271
Showing
6 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters