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

Introduce refresh visit action and method to refresh the current page #1186

Closed
wants to merge 2 commits into from

Conversation

pfeiffer
Copy link
Contributor

Related issues:

This PR adds a refresh visit action and changes Session#refresh to use the current URL if not provided explicitly.

Add refresh visit action

The motivation behind it is to support more granular behavior when doing refreshes. Today, a 'refresh' is essentially a replace action to the current URL. There's no easy way to know if this action was triggered by a user interaction or by a turbo stream refresh. By adding a granular refresh action, developers can handle refreshes (eg. broadcasted as streams) differently from replace actions (form submissions, drive navigations, ..).

An example of this would be in Turbo iOS, where there's no way to differentiate between a user-triggered replace action (ie. pull-to-refresh) or a programatically triggered refresh (ie. broadcasts) (see eg. Turbo iOS #136 and Turbo iOS #175).

With this change, developers (and library maintainers of eg. Turbo iOS) would able to differentiate between replace actions and refresh actions and handle eg. showing loading indicator behavior differently. As an example we'd like show a more subtle loading indicator on refresh actions than on replace actions.

Make Session#refresh refresh current page unless specified

Secondly, the Turbo.session.refresh(..) method is updated to use the current URL of the page. This allows developers to programatically refresh the current page simply by calling Turbo.session.refresh() without having to explicitly provide the current page's URL.

A common use-case could be refreshing (and morphing) a page when it becomes re-focused as a tab, to always ensure that the content is up to date:

addEventListener("visibilitychange", () => {
  if (document.visibilityState === "visible") {
    Turbo.session.refresh()
  }
})

I think this would also be usable for Turbo iOS and Turbo Android apps with multiple sessions, where other sessions could be refreshed instead of reloaded when a mutating request has been made in a single session. See: hotwired/turbo-ios#150

@pfeiffer pfeiffer force-pushed the refresh-visit-option branch 2 times, most recently from 1ce2478 to b620ba4 Compare February 15, 2024 08:47
@pfeiffer
Copy link
Contributor Author

Just noticed that there's a slightly similar PR approach-wise in #1145 although that PR deals with a different issue (navigating to another URL than the current page with morphing). I think the two are unrelated in that this PR explicitly not allows morphing between two different path names.

@@ -33,7 +33,7 @@ export function registerAdapter(adapter) {
* @param location Location to visit (a URL or path)
* @param options Options to apply
* @param options.action Type of history navigation to apply ("restore",
* "replace" or "advance")
* "replace", "refresh" or "advance")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the refresh action should be reserved for internal use, just as the restore action is.

@pfeiffer
Copy link
Contributor Author

Closing as morphing has landed in Turbo Native without this 🎉

@pfeiffer pfeiffer closed this Feb 26, 2024
@tylerklose
Copy link

@pfeiffer any idea what the best upgrade path is?

For example, if I publish a new version of iOS with the updated Turbo Native, there will be some users with the change and others without the change.

I probably wouldn't want users on the older version of the app to see the previous behavior

@pfeiffer
Copy link
Contributor Author

The Turbo Native adapters are compatible with both Turbolinks and Turbo 8.

Your users using an older version of the app would still be able to do that; they would not get the benefit of refreshes though. You could conditionally subscribe to broadcasts on your website, so that users of older app versions without morph support, would not receive any broadcasted refreshes.

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.

2 participants