Skip to content

Instantly share code, notes, and snippets.

View FezVrasta's full-sized avatar
:electron:

Federico Zivolo FezVrasta

:electron:
View GitHub Profile
<!DOCTYPE html>
<html>
<head>
<style>
body {
display: flex;
}
.push {
width: 2000px;
<!DOCTYPE html>
<html>
<head>
<style>
body {
display: flex;
}
.push {
width: 2000px;
window.addEventListener('error', async err => {
// Since we are handling the error here, we must make
// sure we log it into the console nonetheless, otherwise
// it will be very difficult to understand why your app
// is crashing.
console.error(err);
// If no service worker is available, our work ends here
// because we don't need to unregister the service worker
// to make sure the user is able to get a newer version of
serviceWorker.register({
onUpdate: registration => {
setWaitingServiceWorker(registration.waiting);
setUpdateAvailable(true);
},
onWaiting: waiting => {
setWaitingServiceWorker(waiting);
setUpdateAvailable(true);
}
});
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
// WE ADD THE CODE BELOW
if (registration.waiting) {
if (config && config.onWaiting) {
config.onWaiting(registration.waiting);
}
}
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
// WE ADD THE CODE BELOW
if (registration.waiting) {
if (config && config.onWaiting) {
config.onWaiting(registration.waiting);
}
}
import { ServiceWorkerProvider, useServiceWorker } from './useServiceWorker';
const App = () => {
const { isUpdateAvailable, updateAssets } = useServiceWorker();
return (
<div>
Hello, World!
{isUpdateAvailable && (
function registerValidSW(swUrl, config) {
navigator.serviceWorker
.register(swUrl)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
const ServiceWorkerContext = React.createContext();
export const ServiceWorkerProvider = ({ children }) => {
const [waitingServiceWorker, setWaitingServiceWorker] = useState(null);
const [isUpdateAvailable, setUpdateAvailable] = useState(false);
React.useEffect(() => {
serviceWorker.register({
onUpdate: registration => {
setWaitingServiceWorker(registration.waiting);
const [waitingServiceWorker, setWaitingServiceWorker] = useState(null);
const [isUpdateAvailable, setUpdateAvailable] = useState(false);
React.useEffect(() => {
serviceWorker.register({
onUpdate: registration => {
setWaitingServiceWorker(registration.waiting);
setUpdateAvailable(true);
}
});