Skip to content

Instantly share code, notes, and snippets.

@tk-o
Last active August 31, 2022 05:47
Show Gist options
  • Save tk-o/84644e3ab008d44670d94925bd9a6ede to your computer and use it in GitHub Desktop.
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)
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 });
}
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