0

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?

derschiw
  • 120
  • 5

1 Answers1

0

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
});
Abdul Wahed
  • 317
  • 3
  • 12
  • I tried the same, but I didn't solve my problem, since the file stays after loading. This is not what I wanted. But thanks for your answer and sorry for my late response!! – derschiw Oct 18 '22 at 09:12