Skip to content

Commit

Permalink
Store the reason to reload
Browse files Browse the repository at this point in the history
Rename firstTime to !reloadedBySelf.
coepDegrading and reloadedBySelf share the same storage item.
Document that coi.doReload() can use the storage item to see the reason to reload.
  • Loading branch information
tamo committed Dec 4, 2023
1 parent 0a06be0 commit 5c727fe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ You can customize the behavior by defining a variable `coi` in the global scope
window.coi = {
// A function that is run to decide whether to register the SW or not.
// You could for instance make this return a value based on whether you actually need to be cross origin isolated or not.
// The variable "firstTime" is false only if it has been reloaded by itself.
shouldRegister: () => firstTime,
// Using "!reloadedBySelf" you can avoid infinite loops of reloading.
shouldRegister: () => !reloadedBySelf,
// If this function returns true, any existing service worker will be deregistered (and nothing else will happen).
shouldDeregister: () => false,
// A function that is run to decide whether to use "Cross-Origin-Embedder-Policy: credentialless" or not.
Expand All @@ -52,6 +52,7 @@ window.coi = {
coepDegrade: () => true,
// Override this if you want to prompt the user and do reload at your own leisure. Maybe show the user a message saying:
// "Click OK to refresh the page to enable <...>"
// You can see window.sessionStorage.getItem("coiReloadedBySelf") for the reason to reload.
doReload: () => window.location.reload(),
// Set to true if you don't want coi to log anything to the console.
quiet: false
Expand Down
14 changes: 6 additions & 8 deletions coi-serviceworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ if (typeof window === 'undefined') {

} else {
(() => {
const firstTime = !window.sessionStorage.getItem("coiReloadedBySelf");
const reloadedBySelf = window.sessionStorage.getItem("coiReloadedBySelf");
window.sessionStorage.removeItem("coiReloadedBySelf");
const coepDegrading = (reloadedBySelf == "coepdegrade");

// You can customize the behavior of this script through a global `coi` variable.
const coi = {
shouldRegister: () => firstTime,
shouldRegister: () => !reloadedBySelf,
shouldDeregister: () => false,
coepCredentialless: () => true,
coepDegrade: () => true,
Expand All @@ -83,10 +84,6 @@ if (typeof window === 'undefined') {
}
const coepHasFailed = window.sessionStorage.getItem("coiCoepHasFailed");

// See if it is in degradation process using a volatile storage item.
const coepDegrading = window.sessionStorage.getItem("coiCoepDegrading");
window.sessionStorage.removeItem("coiCoepDegrading");

if (controlling) {
// Reload only on the first failure.
const reloadToDegrade = coi.coepDegrade() && !(
Expand All @@ -99,8 +96,8 @@ if (typeof window === 'undefined') {
: coi.coepCredentialless(),
});
if (reloadToDegrade) {
window.sessionStorage.setItem("coiCoepDegrading", "true");
!coi.quiet && console.log("Reloading page to degrade COEP.");
window.sessionStorage.setItem("coiReloadedBySelf", "coepdegrade");
coi.doReload("coepdegrade");
}

Expand All @@ -123,16 +120,17 @@ if (typeof window === 'undefined') {
n.serviceWorker.register(window.document.currentScript.src).then(
(registration) => {
!coi.quiet && console.log("COOP/COEP Service Worker registered", registration.scope);
window.sessionStorage.setItem("coiReloadedBySelf", "true");

registration.addEventListener("updatefound", () => {
!coi.quiet && console.log("Reloading page to make use of updated COOP/COEP Service Worker.");
window.sessionStorage.setItem("coiReloadedBySelf", "updatefound");
coi.doReload();
});

// If the registration is active, but it's not controlling the page
if (registration.active && !n.serviceWorker.controller) {
!coi.quiet && console.log("Reloading page to make use of COOP/COEP Service Worker.");
window.sessionStorage.setItem("coiReloadedBySelf", "notcontrolling");
coi.doReload();
}
},
Expand Down
2 changes: 1 addition & 1 deletion coi-serviceworker.min.js

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

0 comments on commit 5c727fe

Please sign in to comment.