Skip to content
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

Bridge components aren't always registered #35

Open
joemasilotti opened this issue Oct 8, 2024 · 0 comments
Open

Bridge components aren't always registered #35

joemasilotti opened this issue Oct 8, 2024 · 0 comments

Comments

@joemasilotti
Copy link
Member

After debugging a few new Hotwire Native apps I've noticed a common issue. Hotwire.registerBridgeComponents() must be called before a Navigator instance is created. Otherwise, the bridge components never register for the web view.

The quick fix is to defer creation of the Navigator via a lazy variable, like so in the getting started guide:

import HotwireNative
import UIKit

let rootURL = URL(string: "https://hotwire-native-demo.dev")!

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

-   private let navigator = Navigator()
+   private lazy var navigator = Navigator()

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
+       Hotwire.registerBridgeComponents([...])
        window?.rootViewController = navigator.rootViewController
        navigator.route(rootURL)
    }
}

However, it isn't clear why this has to occur and isn't documented anywhere.

I don't think we can ever get registerBridgeComponents() to work after a URL has been routed. The user agent can't be changed after the request is made and that is what signifies which bridge components are registered. However, having a warning or error if you try to register after a URL has been routed could go a long way in stopping these issues.

I'm also open to other suggestions if folks have ideas!

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

1 participant