csells / go_router

The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
https://gorouter.dev
441 stars 96 forks source link

allow control of browser's history stack #199

Closed noga-dev closed 2 years ago

noga-dev commented 2 years ago

Opening a separate issue, per @lulupointu's suggestion here: https://github.com/csells/go_router/issues/196#issuecomment-981360706

Go Router should have the ability to track nav history (kind of like Routemaster) and allow us to programmatically go back and forth and maybe also manipulate the stack.

lulupointu commented 2 years ago

The issue with history tracking/modifying is that this is quite limited in flutter web due to intrinsic limitation imposed by web browser. Here are what web browsers allow:

By blindly I mean: You can change to change the current history index by a certain amount (positive or negative) but you don't know whether this is possible, nor if modifying the current history index by the said amount would keep the user in your app.

If you take into account the fact that:

  1. This is the only thing a web browser allows
  2. An app can be restarted from scratch on flutter web (by reloading the current page in the browser), making it impossible to track the current history entry

You end up with not much to implement.

However, if we consider other platform, anything can be done (since the app cannot be restarted). The question when implementing this feature should be how to deal with the discrepancies between web and non-web.

nullrocket commented 2 years ago

The web behavior makes me nuts trying to find a sensible way to handle browser forward/backward buttons so I just wrap everything in Router.neglect so that I can still get the URL to change and use deep links. I then maintain my own navigation history where it makes sense. Yeah it breaks the forwards / backwards buttons in the browser but I have found that to be less brittle and frustrating for users than trying to fight native browser behavior and losing.

So related to the original question, probably the only way to handle the discrepancies in platforms it to allow an opt out of browser history and substitute your own history stack.

esDotDev commented 2 years ago

I do think the tracking portion of this at least would be quite nice to have. Sometimes you make decisions based on the current state of the history stack, currently this requires adding a listener to the router and storing this List<String> history yourself.

csells commented 2 years ago

I can imagine exposing a history stack but I won't be exposing a way to handle the browser's history stack from go_router anytime soon.