Skip to content

Instantly share code, notes, and snippets.

@firedev
Last active March 4, 2024 21:07
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 firedev/cedf0ea3153e790498da5c72b33fcd76 to your computer and use it in GitHub Desktop.
Save firedev/cedf0ea3153e790498da5c72b33fcd76 to your computer and use it in GitHub Desktop.
Turbo Stream Actions
// <turbo-stream action="reload" target="frame-id"></turbo-stream>
StreamActions.reload = function () {
document.getElementById(this.getAttribute("target"))?.reload()
}
// Following the naming conventions
// https://github.com/marcoroth/turbo_power-rails#browser-history-actions
//
// <turbo-stream action="replace_state" url="URL"></turbo-stream>
window.addEventListener("turbo:load", () => {
document.addEventListener("turbo:before-stream-render", (event) => {
if (event.target.action === "replace_state") {
const url = event.target.attributes.url.value;
history.replaceState(null, null, url);
event.preventDefault();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment