# Controller initialized twice

**URL:** https://discuss.hotwired.dev/t/controller-initialized-twice/273
**Category:** General
**Created:** [May 1, 2018, 9:38am UTC](https://discuss.hotwired.dev/t/controller-initialized-twice/273 "2018-05-01T09:38:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mrmartineau](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/mrmartineau/32/149_2.png) [@mrmartineau](https://discuss.hotwired.dev/u/mrmartineau)
#### Post date: [May 1, 2018, 9:38am UTC](https://discuss.hotwired.dev/t/controller-initialized-twice/273/1 "2018-05-01T09:38:50Z")

</div>

For some reason, my controller is being initialized twice.

At the moment, there is only one item on the page that uses this controller, but if I log when `initialize` is run, then I see two messages.

What could be causing this?

The reason for my concern is that some HTML is modified so if the controller initializes more than once, the markup is modified multiple times…

Any ideas how to prevent this from happening?

Also, I have seen a similar topic here but I am not using Turbolinks so that is not the issue.

---

<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: [May 4, 2018, 1:37am UTC](https://discuss.hotwired.dev/t/controller-initialized-twice/273/2 "2018-05-04T01:37:32Z")

</div>

The only time I’ve seen this happen is in conjunction with Turbolinks (as you mentioned) when it was set up incorrectly.

Can you try to provide a minimal example showing the issue?

---

<div class="post-metadata">

### Author: ![mrmartineau](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/mrmartineau/32/149_2.png) [@mrmartineau](https://discuss.hotwired.dev/u/mrmartineau)
#### Post date: [May 15, 2018, 8:37pm UTC](https://discuss.hotwired.dev/t/controller-initialized-twice/273/3 "2018-05-15T20:37:37Z")

</div>

Thanks Scott, I will see if I can put a simple demo together for you (it shouldn’t be too much trouble).

---

<div class="post-metadata">

### Author: ![javan](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/javan/32/594_2.png) [@javan](https://discuss.hotwired.dev/u/javan)
#### Post date: [May 22, 2018, 9:35pm UTC](https://discuss.hotwired.dev/t/controller-initialized-twice/273/4 "2018-05-22T21:35:40Z")

</div>

See [https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/3:](https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/3:)

> [@Controller initialized twice when visiting from a Turbolinks page](https://discuss.hotwired.dev/t/controller-initialized-twice-when-visiting-from-a-turbolinks-page/17/3):
>
> 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.
