# Controller initialized twice when visiting from a Turbolinks page

**URL:** <https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17>\
**Category:** General\
**Created:** [February 2, 2018, 1:09am UTC](https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17 "2018-02-02T01:09:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![scottharvey](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/scottharvey/32/10_2.png) [@scottharvey](https://discuss.hotwired.dev/u/scottharvey)\
**Post date:** [February 2, 2018, 1:09am UTC](https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/1 "2018-02-02T01:09:02Z")

</div>

When using Turbolinks and I visit a page that has a Stimulus controller the controller’s `initialize` function is called twice. Here’s a gift showing it happening

 ![screen](https://canada1.discourse-cdn.com/flex027/uploads/stimulusjs/original/1X/9ad2d17f71cf696f4214bc3c6d3b036312f40658.gif)

Here is the example Rails app I used to create that gif. [https://github.com/scottharvey/stimulus\_asset\_pipeline](https://github.com/scottharvey/stimulus_asset_pipeline)

I’m not sure if it’s an issue or not but something unusual I notice while debugging something else.

---

<div class="post-metadata">

**Author:** ![pelted](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/pelted/32/12_2.png) [@pelted](https://discuss.hotwired.dev/u/pelted)\
**Post date:** [February 2, 2018, 1:14am UTC](https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/2 "2018-02-02T01:14:51Z")

</div>

I had something similar happen but it was because I was sending an HTML fragment with the data-controller attribute from the server. This was causing it to connect again when that fragment was added to the DOM.

---

<div class="post-metadata">

**Author:** ![sam](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/sam/32/1091_2.png) [@sam](https://discuss.hotwired.dev/u/sam)\
**Post date:** [February 2, 2018, 1:20am UTC](https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/3 "2018-02-02T01:20:30Z")

</div>

I believe everything is working as expected here. You’re seeing [Turbolinks’ preview cache](https://github.com/turbolinks/turbolinks#understanding-caching) 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.

---

<div class="post-metadata">

**Author:** ![scottharvey](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/scottharvey/32/10_2.png) [@scottharvey](https://discuss.hotwired.dev/u/scottharvey)\
**Post date:** [February 2, 2018, 1:30am UTC](https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/4 "2018-02-02T01:30:06Z")

</div>

Thanks @sam, that totally makes sense. Everything seems to be working as you described.

 ![screen](https://canada1.discourse-cdn.com/flex027/uploads/stimulusjs/original/1X/6e77e0c4164b8dd803b557000862279b5f28029e.gif)

---

<div class="post-metadata">

**Author:** ![aliahmed922](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/aliahmed922/32/616_2.png) [@aliahmed922](https://discuss.hotwired.dev/u/aliahmed922)\
**Post date:** [September 20, 2019, 2:10am UTC](https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/5 "2019-09-20T02:10:43Z")

</div>

Double 👍👍 for @sam

---

<div class="post-metadata">

**Author:** ![asfour75](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/asfour75/32/2552_2.png) [@asfour75](https://discuss.hotwired.dev/u/asfour75)\
**Post date:** [February 17, 2022, 9:07pm UTC](https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/6 "2022-02-17T21:07:54Z")

</div>

I’m using The latest Stimulus with Rails 7 …  
Now I’m observing this behavior only whenever I do hard refresh to the page … then I try to active an action, it goes twice !!

---

<div class="post-metadata">

**Author:** ![Iccr](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/iccr/32/3735_2.png) [@Iccr](https://discuss.hotwired.dev/u/Iccr)\
**Post date:** [May 1, 2024, 5:13am UTC](https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/7 "2024-05-01T05:13:41Z")

</div>

I am facing same in rails 7. did you find an work around for it. I am stuck at it.

---

<div class="post-metadata">

**Author:** ![VanPaitin](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/vanpaitin/32/4080_2.png) [@VanPaitin](https://discuss.hotwired.dev/u/VanPaitin)\
**Post date:** [April 10, 2025, 2:12am UTC](https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/8 "2025-04-10T02:12:45Z")

</div>

when calling:

```auto
<%= javascript_importmap_tags %>

```

just make sure to disable shims. For some reason, the shims is importing the controllers twice.  
It should look something like this:

```auto
<%= javascript_importmap_tags shim: false %>

```
