Notifying users about updates (create-react-app)

Articles|Håkon Underbakke | over 3 years ago

In serviceWorker.js there is a function that sits around line 74, called installingWorker.onstatechange. Inside of the function there is a condition checking if the state is “installed” and then logging out a message for the console. The goal here is to simply insert your own code below to notify the user of this outside of the console. Something like the following could work:

let newUpdateNotification = document.createElement("div"); // Create a div
newUpdateNotification.classList.add("newUpdateNotification"); // Give it a class so we can style it
newUpdateNotification.innerHTML = `A new update is available. Please close all instances of ... and reopen to install the update`; // Give the notification a message
document.body.appendChild(newUpdateNotification); // Append div to body

Now all you need is to add some styles to .newUpdateNotification and voilà, you successfully notify users about incoming updates!