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 · 2 comments
Open

Bridge components aren't always registered #35

joemasilotti opened this issue Oct 8, 2024 · 2 comments
Assignees

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!

@olivaresf
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.

What do you think about this #41?

In #41, all Navigators will subscribe to a .didRegisterBridgeComponents notification that registerBridgeComponents() posts when it's called. Once this notification is received, the navigator will initialize the bridge in both root and modal web views and update its user agent. This will not fix any requests that are in-flight or done so we may need to force a refresh, but it should work in theory.

Could you take that for a spin in one of your projects to see if it works for you?

@olivaresf olivaresf self-assigned this Oct 21, 2024
@joemasilotti
Copy link
Member Author

This works great! I swapped out lazy var for a let in the demo app's Navigator and added a bridge component to the index page - worked flawlessly. I added one commit to the PR cleaning up some whitespace.

In-flight requests, as expected, do not correctly register their bridge components. You can replicate with the code below. But I'm OK with not addressing that - what do you think?

import HotwireNative
import UIKit

let rootURL = URL(string: "http://localhost:45678")!

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

    private let navigator = Navigator()

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

final class FormComponent: BridgeComponent {
    override class var name: String { "form" }

    // ...
}

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