Skip to content

Conversation

@jayohms
Copy link
Contributor

@jayohms jayohms commented Aug 21, 2025

This fixes: #136

Let's say you have 2 screens:

  • Fragment A (web screen)
  • Fragment B (native screen)

Previously, when you navigated from A -> B -> (back to) A, the library would perform a "synthetic" restore visit to the A screen, since the underlying session's WebView still resided at location A (B is native, so it didn't alter the WebView location).

This approach prevented A from recreating it's View state for bridge components on the screen. When A is no longer visible, it's View is destroyed by the system and only B is visible on screen. Additionally, the synthetic restore approach meant that we never cached snapshots when leaving A and visiting a native screen.

So, to fix this: I updated the functionality to:

  • Cache snapshots whenever leaving a web screen and visiting a native screen

This wasn't sufficient, though, reattaching the WebView to the already currently rendered location does not trigger Stimulus to re-connect its controllers. This makes sense, since the page DOM hasn't actually changed. But, it's a problem in the native library since we need to rebuild the view state of the attached bridge components on the screen — and the way to do this is to see the connect() callback in bridge component controllers on the web.

To work around this, the native library now dispatches a native:restore event that the web bridge library can listen to in already connected bridge component controllers and manually call its connect() method.

This is the corresponding PR to allow native apps to see bridge components reconnect after a synthetic restore visit:
hotwired/hotwire-native-bridge#9

…caching a snapshot when leavin the web screen.
…e web bridge library can be notified when this occurs
Copy link
Collaborator

@mbarta mbarta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work 👏

Copy link

@jhutarek jhutarek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 👍

…s does not always play nicely with same-page "restore" visits
@jayohms jayohms merged commit 8ba14ae into main Aug 28, 2025
1 check passed
@jayohms jayohms deleted the fix-same-page-restore branch August 28, 2025 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Stimulus connect() not called when navigating back from native fragment

4 participants