Controller initialized twice when visiting from a Turbolinks page

I believe everything is working as expected here. You’re seeing Turbolinks’ preview cache in action:

[D]uring standard navigation (via Application Visits), Turbolinks will immediately restore the page from cache and display it as a preview while simultaneously loading a fresh copy from the network. This gives the illusion of instantaneous page loads for frequently accessed locations.

The first controller is initialized when Turbolinks renders the preview. Then the preview <body> is replaced by fresh page content loaded over HTTP, which initializes a second controller.

Try these two things to confirm:

  1. Log in connect() and disconnect() instead of initialize(). You should see a corresponding call to disconnect() for the first controller before the second controller connects.
  2. Log the value of document.documentElement.hasAttribute("data-turbolinks-preview"). You should see true when Turbolinks is showing the preview.
2 Likes