Skip to content

Instantly share code, notes, and snippets.

@fabe
Created March 27, 2019 16:47
Show Gist options
  • Save fabe/f247cb654d185b34ae16efa857864b67 to your computer and use it in GitHub Desktop.
Save fabe/f247cb654d185b34ae16efa857864b67 to your computer and use it in GitHub Desktop.
const getUserConfirmation = (location, callback) => {
const [newUrl, action] = location.split('|');
// Check if user wants to navigate to the same page.
// If so, we don't want to trigger the page transitions.
// We have to check the `action`, because the pathnames
// are the same when going back in history 🤷‍
const currentUrl = `${window.location.pathname}${window.location.search}`;
if (newUrl === currentUrl && action === 'PUSH') {
callback(false);
return;
}
callback(true);
};
let history = createHistory({ getUserConfirmation });
// `block` must return a string to conform.
// We send both the pathname and action to `getUserConfirmation`.
history.block((location, action) => {
return `${location.pathname}${location.search}|${action}`;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment