Skip to content

Reloading a page doesn't trigger Stimulus disconnect() #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
joemasilotti opened this issue Dec 22, 2024 · 2 comments
Open

Reloading a page doesn't trigger Stimulus disconnect() #65

joemasilotti opened this issue Dec 22, 2024 · 2 comments

Comments

@joemasilotti
Copy link
Member

Reloading a page, either via pull-to-refresh or by calling navigator.reload(), does not trigger disconnect() on any connected Stimulus controllers.

To reproduce, apply the following diff to the demo app server:

diff --git a/public/javascript/controllers/menu_controller.js b/public/javascript/controllers/menu_controller.js
index 11520b3..e59c8a5 100644
--- a/public/javascript/controllers/menu_controller.js
+++ b/public/javascript/controllers/menu_controller.js
@@ -3,6 +3,16 @@ import { Controller } from "@hotwired/stimulus"
 export default class extends Controller {
   static targets = [ "dialog", "item", "result" ]

+  connect() {
+    super.connect()
+    alert("connect")
+  }
+
+  disconnect() {
+    super.disconnect()
+    alert("disconnect")
+  }
+
   show() {
     this.dialogTarget.style.display = "block"
   }

When you tap "Menu example" you'll see an alert with "connect". When you tap the back button you'll see "disconnect". But if you tap "Menu example" then pull-to-refresh, you only receive "connect" again. You never receive "disconnect".

This can cause issues if a page adds a native bar button item only for signed in users. If the user signs out and the page is reloaded, there's no hook to remove the button because disconnect() is never called.

@joemasilotti
Copy link
Member Author

Turns out refreshing a page in a web view won't call disconnect() either. So this isn't actually a Hotwire Native issue. But I'm curious as to why this occurs.

@jspooner
Copy link

jspooner commented May 6, 2025

I was looking at the lifecycle hooks in HotwriteWebViewController and how BridgeDelegate relays these messages to the JavaScript component. Are you suggesting there is similar code that broadcasts messages to normal Stimilus Controllers?

I was trying to use the disconnect hook to stop Audio from playing in the view but that method is not called when navigating to another Native Tab Item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants