derschiw
derschiw

Reputation: 350

Turbo and content_for - Rails

I'm trying to load a javascript only on a certain page. For that I'm using <%= yield(:head) %> in the <head> and the following on the page:

<% content_for :head do %>
  <%= javascript_import_module_tag "foo" %>
<% end %>

This works when clicking on a link to the page the first time. However when further I navigate anywhere and then go back again, Turbo conserves the head part (<script type="module">import "foo"</script>). Thus this script is not executed anymore.

Is there a way to tell turbo, that it shouldn't 'cache' this?

Upvotes: 0

Views: 605

Answers (1)

Abdul Wahed
Abdul Wahed

Reputation: 317

ran into same problem. had to use document.addEventListener("turbo:load", function() {} ); to load the js

foo.js

document.addEventListener("turbo:load", function() {
  console.log("foo.js loaded");
  // more js codes
});

Upvotes: 0

Related Questions