This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
body { | |
display: flex; | |
} | |
.push { | |
width: 2000px; |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
body { | |
display: flex; | |
} | |
.push { | |
width: 2000px; |
This file contains hidden or 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
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 |
This file contains hidden or 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
serviceWorker.register({ | |
onUpdate: registration => { | |
setWaitingServiceWorker(registration.waiting); | |
setUpdateAvailable(true); | |
}, | |
onWaiting: waiting => { | |
setWaitingServiceWorker(waiting); | |
setUpdateAvailable(true); | |
} | |
}); |
This file contains hidden or 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
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); | |
} | |
} |
This file contains hidden or 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
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); | |
} | |
} |
This file contains hidden or 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
import { ServiceWorkerProvider, useServiceWorker } from './useServiceWorker'; | |
const App = () => { | |
const { isUpdateAvailable, updateAssets } = useServiceWorker(); | |
return ( | |
<div> | |
Hello, World! | |
{isUpdateAvailable && ( |
This file contains hidden or 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
function registerValidSW(swUrl, config) { | |
navigator.serviceWorker | |
.register(swUrl) | |
.then(registration => { | |
registration.onupdatefound = () => { | |
const installingWorker = registration.installing; | |
if (installingWorker == null) { | |
return; | |
} | |
installingWorker.onstatechange = () => { |
This file contains hidden or 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
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); |
This file contains hidden or 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
const [waitingServiceWorker, setWaitingServiceWorker] = useState(null); | |
const [isUpdateAvailable, setUpdateAvailable] = useState(false); | |
React.useEffect(() => { | |
serviceWorker.register({ | |
onUpdate: registration => { | |
setWaitingServiceWorker(registration.waiting); | |
setUpdateAvailable(true); | |
} | |
}); |