-
-
Save kellenmace/309b93c366372e5d08f567b987a521ce to your computer and use it in GitHub Desktop.
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
// gatsby-browser.js | |
import { SERVICE_WORKER_UPDATE_FOUND } from "./src/components/navLink" | |
/** | |
* Set global variable to true when service worker has found an | |
* update. This is used to trigger a page reload on route change. | |
*/ | |
export function onServiceWorkerUpdateReady() { | |
window[SERVICE_WORKER_UPDATE_FOUND] = true | |
} | |
// src/components/navLink.js | |
export const SERVICE_WORKER_UPDATE_FOUND = "myAppServiceWorkerUpdateFound" | |
function NavLink({ children, ...props }) { | |
const isOnline = useNetwork() | |
// If we're online and service worker has found an update, | |
// trigger page reload on route change. | |
function handleClick(event) { | |
if (isOnline && window[SERVICE_WORKER_UPDATE_FOUND] === true) { | |
event.preventDefault() | |
window.location.href = props.to | |
} | |
} | |
return ( | |
<Link {...props} onClick={handleClick}> | |
{children} | |
</Link> | |
) | |
} | |
export default NavLink |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment