Skip to content

[go_router] Keep cached branch locations consistent after root navigation - #13735

Open
AyatAliev wants to merge 9 commits into
flutter:mainfrom
AyatAliev:fix/go-router-194209-scoped-branch-location
Open

AyatAliev wants to merge 9 commits into
flutter:mainfrom
AyatAliev:fix/go-router-194209-scoped-branch-location

Conversation

@AyatAliev

@AyatAliev AyatAliev commented Nov 20, 2026 •

Copy link
Copy Markdown

This continues #13202, which GitHub closed after I accidentally deleted its head fork. It has the same commits plus the ones that address the latest review there; the earlier discussion is in #13202.

When a StatefulShellRoute branch redirects to a child on the root navigator, returning to the branch with goBranch() can restore matches that exclude the child while their URI still points to it. The restore parser then keeps that inconsistent cached list, so the redirect page does not appear on re-entry.

Find the first match outside the current shell through its ancestor shells and remove that match and the following suffix with RouteMatchList.remove(). This removes obsolete path parameters and retains query parameters, fragments, extra, and branch imperative pages.

RouteMatchList.remove() now takes the new URI path from the encoded matchedLocation of the last remaining declarative match instead of rebuilding it from decoded path parameters, so encoded delimiters such as %2F survive both branch scoping and a plain pop(). It also removes redirect-only routes left at the end after the removal: at the end of a preceding ShellRouteMatch, and before a ShellRouteMatch that becomes empty. This fixes the redirect-only assertion failures on pop() and goBranch() for a root route nested under a redirect-only route in a branch. In that setup, and for a shell route nested under a redirect-only route, pushReplacement() and replace() also no longer leave the redirect-only route in the location. The pop(), pushReplacement() and replace() failures also exist on main, independently of branch restoration.

Branch navigators are still built from the unscoped match list, so a branch navigator is also rebuilt when the URI it was built from changes, or when the top match it was built from is gone, such as a root route that was pushed before restartAndRestore or a RoutingConfig update and then popped. This keeps GoRouterState.of correct in branch pages after popping a root route or returning with goBranch(). Pushing a route onto the root navigator changes neither, so branch pages are still not rebuilt in that case.

Fixes flutter/flutter#194209.

Adds fifteen widget tests in test/branch_restoration_uri_test.dart covering repeated root redirects, cached URI/path parameters, encoded path delimiters on branch scoping and on pop(), imperative stacks, scoping through both enclosing and root navigators, restartAndRestore, GoRouterState.of in branch pages, including after popping a root route pushed before restartAndRestore or a RoutingConfig update, branch rebuilds on push, and redirect-only parents. For a shell route nested under a redirect-only route, adds a RouteMatchList.remove unit test in test/match_test.dart and a pushReplacement test in test/go_router_test.dart. This package uses batched releases, so instead of a version bump the change adds pending_changelogs/fix_cached_branch_uri.yaml, which requests a patch release.

Validation

On this branch merged with main at 76015c1493 (go_router 18.1.4), Flutter 3.44.9 / Dart 3.12.2:

  • 12 of the 15 tests in test/branch_restoration_uri_test.dart, the new test/match_test.dart test and the new test/go_router_test.dart test fail on unmodified main. The other 3 guard behavior that already works there. All pass with this change.
  • flutter test --no-pub --reporter failures-only: 499 passed.
  • flutter test --no-pub --reporter failures-only in example/: 17 passed.
  • flutter test --no-pub --platform=chrome test/branch_restoration_uri_test.dart test/match_test.dart test/matching_test.dart: 26 passed.
  • flutter analyze --no-pub --fatal-infos: no issues.
  • dart format --output=none --set-exit-if-changed on the changed Dart files: no changes required.
  • flutter_plugin_tools license-check, format, analyze, validate --check-for-missing-changes and publish-check for go_router: no issues.

Pre-Review Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2

Scoping a branch location removes the URI of routes on ancestor navigators,
but the branch Navigator is still built from the unscoped match list. Popping
such a route left the scoped location unchanged, so the Navigator was not
rebuilt and GoRouterState.of in branch pages kept the URI, path parameters and
top route of the removed route. Returning to the branch with goBranch had the
same result.

Rebuild the Navigator when the URI it was built from changes. Pushing a route
does not change that URI, so branch pages are still not rebuilt in that case.

Removing a route on an ancestor navigator also left a redirect-only parent
nested in the preceding ShellRouteMatch, which failed the redirect-only
assertions after pop and goBranch. Remove trailing redirect-only routes from
that shell as well.
- Remove redirect-only routes before a shell route that remove() empties.
- Build the path in remove() from the encoded matchedLocation so pop()
  keeps encoded delimiters such as %2F.
- Rebuild a branch navigator when the top match it was built from is gone.
- Use the standard license header in the new test file.
@github-actions github-actions Bot added p: go_router P2 Important issues not at the top of the work list labels Nov 20, 2026
@github-actions
github-actions Bot marked this pull request as draft November 20, 2026 11:01
@github-actions github-actions Bot added the waiting for response The Flutter team cannot make further progress on this PR until the author responds label Nov 20, 2026
@github-actions

Copy link
Copy Markdown

Hi @AyatAliev, thank you for your contribution!

This pull request has been converted to a draft because it does not appear to follow our pull request template:

  • Unchecked checklist items (1 of 11): All items in the Pre-Review Checklist must be completed and checked off (- [x]) before the PR can be reviewed:
    • - [ ] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools.

How to fix this

  1. Click the ... menu in the top-right corner of your PR description above and select Edit.
  2. Address the issues listed above. Make sure every item in the Pre-Review Checklist is present without modifications and checked off (- [x]).
  3. Save your changes, then click the "Ready for review" button at the bottom of the PR.
Click here for the raw Markdown of the Pre-Review Checklist to copy and paste
## Pre-Review Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
- [ ] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools.
- [ ] I read the [Tree Hygiene] page, which explains my responsibilities.
- [ ] I read and followed the [relevant style guides] and ran [the auto-formatter].
- [ ] I signed the [CLA].
- [ ] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]`
- [ ] I [linked to at least one issue that this PR fixes] in the description above.
- [ ] I followed [the version and CHANGELOG instructions], using [semantic versioning] and the [repository CHANGELOG style], or I have commented below to indicate which documented exception this PR falls under[^1].
- [ ] I updated/added any relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or I have commented below to indicate which [test exemption] this PR falls under[^1].
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

[^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.

<!-- Links -->
[Contributor Guide]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md
[AI contribution guidelines]: https://github.com/flutter/flutter/blob/master/CONTRIBUTING.ai.md#pull-requests
[Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md
[relevant style guides]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style
[the auto-formatter]: https://github.com/flutter/packages/blob/main/script/tool/README.md#format-code
[CLA]: https://cla.developers.google.com/
[Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
[linked to at least one issue that this PR fixes]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview
[the version and CHANGELOG instructions]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version-and-changelog-updates
[semantic versioning]: https://dart.dev/tools/pub/versioning#semantic-versions
[repository CHANGELOG style]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style
[test exemption]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request resolves several routing and state restoration issues in go_router. It updates RouteMatchList to preserve encoded path delimiters and refactors the removal of trailing redirect-only routes, including those nested within shell routes. In StatefulNavigationShellState, the scoping of match lists is updated to correctly identify matches after the shell route, and _StatefulShellBranchState is enhanced to track the match list from which the navigator was built to detect when a navigator is outdated. Additionally, regression tests and changelog entries are added. The review feedback suggests resolving a potential issue where _removeTrailingRedirectOnlyMatches could return an invalid empty ShellRouteMatch, and correcting an invalid import of package:material_ui/material_ui.dart in the test file.

Comment thread packages/go_router/lib/src/match.dart Resolved
@AyatAliev
AyatAliev marked this pull request as ready for review November 20, 2026 11:10

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request fixes several issues in go_router related to StatefulShellRoute branch restoration, location metadata, and redirect-only route cleanup. Specifically, it updates RouteMatchList to preserve encoded path delimiters, refactors trailing redirect-only route removal, and improves navigator rebuilding logic in StatefulNavigationShellState by tracking the match list from which the navigator was built. Review feedback highlights an incorrect material import in the new test file and identifies a logic bug in _removeTrailingRedirectOnlyMatches where empty shell routes are not fully cleaned up.

Comment thread packages/go_router/lib/src/match.dart Resolved
@github-actions github-actions Bot removed the waiting for response The Flutter team cannot make further progress on this PR until the author responds label Nov 20, 2026
The shared test router now uses GoRouter.routingConfig so a test can
replace its routes while a root route is pushed.
@AyatAliev

AyatAliev commented Nov 20, 2026 •

Copy link
Copy Markdown
Author

@Piinks this continues #13202, which closed after I accidentally deleted my fork. All the comments from your last review are addressed here, and I replied in each thread there

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

Labels

p: go_router P2 Important issues not at the top of the work list

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go_router] Root-navigator redirect page is omitted when restoring a StatefulShellRoute branch

1 participant