Skip to content

feat: jaspr_router: let a route confirm before it is left #972

Open

Description

@BiAksoy

Description

A form page with unsaved edits should be able to ask "Discard your changes?" when the person navigates away inside the app: through a Link, a push or replace from code, or the browser's Back and Forward buttons. Closing the tab or a full page load is covered by beforeunload, which an app can handle itself.

In #201 the conclusion was that redirect covers route guards, and for entering a route (a sign-in check, say) it does. Leaving a route is the part it cannot express. I tried to build the confirmation on the top-level redirect (asking, then returning the current location to stay) and ran into four things:

  1. The redirect also runs when a Link is hovered (fix: jaspr_router runs redirect when a Link is hovered #971), so the question appears on mouseover.

  2. The redirect only sees the target location and cannot tell a push from a Back or Forward. RouteState carries no navigation kind and no origin.

  3. Declining a push leaves a duplicate history entry. _update pushes whenever updateHistory is true, and the location it pushes is the redirected one, the page we stayed on:

    Future<void> _update(String location, {Object? extra, bool updateHistory = true, bool replace = false}) {
    return _matchRoute(location, extra: extra).then((match) {
    if (!mounted) return;
    setState(() {
    _matchList = match;
    if (updateHistory || location != match.uri.toString()) {
    final historyUrl = _normalizeUrl(match.uri.toString());
    if (!replace) {
    PlatformRouter.instance.history.push(historyUrl, title: match.title, data: match.extra);
    } else {
    PlatformRouter.instance.history.replace(historyUrl, title: match.title, data: match.extra);
    }
    }
    });
    });

    In a browser test, after a declined push('/list') from /edit, history.length went up by one and Back stayed on /edit. The page itself is kept (a counter in it kept its value).

  4. Declining Back or Forward overwrites a history entry. When popstate fires, the browser is already on the entry it moved to. The router handles it with updateHistory: false, replace: true:

    _routeChangeDispose = PlatformRouter.instance.history.init(
    context,
    onChangeState: (state, {url}) {
    _update(url ?? context.url, extra: state, updateHistory: false, replace: true);
    },
    );

    When the redirect returns a different location, _update calls history.replace with it, which writes the page we stayed on into the entry the browser moved to. With the history /, /list, /edit and the redirect sending /list back to /edit, a browser test showed: after Back the address is /edit and history.length is unchanged; with the redirect no longer declining, the next Back goes to /, and Forward twice shows /edit both times. The /list entry now holds /edit, so /list is lost from the history.

The app now works around this with a capturing click listener that runs ahead of Link, a flag that skips the question while a mouseover is being handled, and its own popstate listener that pushes the page's address back after a declined Back. That depends on listener registration order and on router internals, which is why I would rather ask for a hook.

What I have in mind is a callback on Route that runs before the route is left and can cancel the navigation, along the lines of go_router's onExit (FutureOr<bool> Function(BuildContext, GoRouterState)):

https://github.com/flutter/packages/blob/f895ad58a1fd8527e08b7b536b984e49ea8ddb20/packages/go_router/lib/src/route.dart#L415-L461

For a declined push or replace the router would do nothing. That part alone would already let the app drop its click listener and the mouseover flag.

A declined Back or Forward is the harder case and could be decided separately: the browser has already moved, so the router has to move it back to the entry it came from instead of replacing the entry it landed on. One way is to keep an index in the history state, so the router knows how far the browser moved and can undo it with history.go. I have not tried this, and I may be missing a simpler way.

Additional Context

Seen with jaspr_router 0.9.0 (jaspr 0.23.5). The behaviour in points 3 and 4 is the same on main (413ad06); I checked it there with browser tests (dart2js, headless Chrome 156). I can share the test file. If a hook like this is welcome, I would be happy to work on it once the shape is agreed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions