Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using Turbolinks 5 events are not bound after using the browser Back button #6

Open
Mitcheljager opened this issue May 30, 2021 · 1 comment

Comments

@Mitcheljager
Copy link

I have a component that's simply a button that console.logs when clicked:

// application.js
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"

Rails.start()
Turbolinks.start()

import WebpackerSvelte from "webpacker-svelte"
import Button from "../src/components/form/Button.svelte"

WebpackerSvelte.setup({ Button })
// src/components/form/Button.svelte
<a on:click={ () => console.log("clicky") }>Click me</a>

This works as you would expect. Now when I navigate away from this page and then press the back button in the browser the button still renders (as it was cached by Turbolinks) but the click event no longer fires.

Disabling caching in Turbolinks fixes the issue, but caching would be nice to keep.

@Mitcheljager
Copy link
Author

Mitcheljager commented Jun 1, 2021

I've managed to fix it myself by emptying the element before caching.

document.addEventListener("turbolinks:before-cache", function() {
  const svelteComponents = document.querySelectorAll("[data-svelte-component]")
  svelteComponents.forEach(element => element.innerHTML = null)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant