Skip to content

Instantly share code, notes, and snippets.

@croxton
Created October 26, 2022 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save croxton/e9c8dc777c9e334c213af280e41fbb8b to your computer and use it in GitHub Desktop.
Save croxton/e9c8dc777c9e334c213af280e41fbb8b to your computer and use it in GitHub Desktop.
htmx push history
//===========================================
// Create a new history entry for the request
//===========================================
var pushUrl = getClosestAttributeValue(elt, "hx-push-url");
var elementIsBoosted = getInternalData(elt).boosted;
if (pushUrl || elementIsBoosted) {
// If hx-push-url is explicitly defined (and not "true" or "false"),
// we already know what the final URL should be.
// For boosted elements the final URL will be the same as the request path.
if (elementIsBoosted || pushUrl === "true" || pushUrl === "false") {
// Fallback to path
pushUrl = path;
}
if (pushUrl === null || finalPathForGet) {
// Otherwise use the current url to create a new history
// entry *without* changing the location url.
// The response will change (replaceState) the final URL
pushUrl = currentPathForHistory;
}
// Create a history entry
pushUrlIntoHistory(pushUrl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment