Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Last active August 26, 2023 15:43
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/309b93c366372e5d08f567b987a521ce to your computer and use it in GitHub Desktop.
Save kellenmace/309b93c366372e5d08f567b987a521ce to your computer and use it in GitHub Desktop.
// 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