Last active
August 31, 2022 05:47
-
-
Save tk-o/84644e3ab008d44670d94925bd9a6ede to your computer and use it in GitHub Desktop.
Programmatic location force reload — [live demo](https://codesandbox.io/s/programmatic-location-force-reload-0ol5gd)
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
const ATTR_FORCE_RELOAD = "forceReload"; | |
export function cleanUpFroceReloadSideEffects() { | |
const nextUrl = new URL(window.location.href); | |
nextUrl.searchParams.delete(ATTR_FORCE_RELOAD); | |
console.log(nextUrl); | |
window.history.replaceState(null, null, nextUrl); | |
} | |
export function forceReload(targetUrl = window.location.href) { | |
const nextLocation = new URL(targetUrl); | |
nextLocation.searchParams.set(ATTR_FORCE_RELOAD, Date.now()); | |
window.location.assign(nextLocation); | |
} | |
export function setup() { | |
cleanUpFroceReloadSideEffects(); | |
Object.assign(window.location, { forceReload }); | |
} |
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
import { setup as forceReloadSetup } from "./force-reload"; | |
forceReloadSetup(); | |
// now just call | |
window.location.forceReload(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment