Repository navigation
Commit 17e14c2
authored
[go_router] Fix shell route key collisions and dynamic branch updates (#12549)
This supersedes #11143, which was closed as inactive. It incorporates the review feedback from that PR.
## Problem
When `push()` navigates between sibling or nested regular `ShellRoute` branches, multiple shell pages and navigators can coexist in the widget tree.
This could produce duplicate page or widget keys because:
- `_CustomNavigator` was keyed using `GlobalObjectKey(navigatorKey.hashCode)`, so distinct navigator keys with the same hash were treated as the same key.
- `ShellRouteMatch.pageKey` was derived only from `route.hashCode`.
- Re-entering a `ShellRoute` that was already present in the imperative stack reused its page and navigator identities.
The first implementation in #11143 created fresh page and navigator keys for every `ShellRouteMatch`. Review correctly pointed out that this made navigator identity unstable across route reparses and prevented the configured `ShellRoute.navigatorKey` from consistently identifying its navigator.
`RoutingConfig` updates could also leave inactive loaded `StatefulShellRoute` branches holding matches and navigator widgets built from the old route objects.
## Fix
This version addresses that feedback by:
- preserving the configured navigator key during normal route matching and reparsing;
- making shell page keys identity-aware, so distinct routes remain distinct even when their `hashCode` values collide;
- keying the nested `_CustomNavigator` wrapper with a value-equal `ValueKey` of its navigator key;
- assigning scoped page and navigator keys only when an imperative push would otherwise collide with another live regular `ShellRoute` match;
- deriving that scope from the persistent `ImperativeRouteMatch.pageKey`;
- carrying existing shell matches through recursive imperative push matching, so nested key collisions are detected;
- reparsing loaded inactive `StatefulShellRoute` branches and rebuilding their cached navigator widgets when route objects change;
- scoping saved matches to their shell and resetting branches whose base location was removed;
- identifying reparsed branches by their navigator key, including after nested `pushReplacement()`;
- preserving branch and root observers when rebuilding inactive or preloaded navigators, honoring `notifyRootObserver`;
- leaving the root navigator wrapper and normal configured-key navigation unchanged.
## Tests
The regression test `push to a sibling shell route under the same parent shell route` forces both navigator-key and route-hash collisions.
It covers:
- pushing to a sibling shell route;
- re-entering the first shell route while its original instance is still in the stack;
- preserving navigator and page state across match-list reconstruction and `refresh()`;
- detecting and scoping collisions in nested `ShellRouteMatch` trees;
- popping back through both shell routes;
- navigating with `go()` after the imperative stack is removed.
Additional regressions cover the nested push sequence in [the review comment](#12549 (comment)), inactive loaded branches after a `RoutingConfig` update ([review comment](#12549 (comment))), and the exact flows from flutter/flutter#148712 and flutter/flutter#140586.
Eight additional regression cases cover the follow-up review findings: a removed base route with a surviving push (with and without preload), nested `pushReplacement()`, a removed pushed route, and branch/root observer behavior for both preload and `notifyRootObserver` settings. The observer regression uses pageless `Navigator.push()` so a GoRouter rebuild cannot mask detached observers; it also verifies navigator-state preservation and updated `clipBehavior`.
No screenshots are included because this is a routing/runtime fix.
- Fixes flutter/flutter#148712
- Fixes flutter/flutter#140586
## Validation
- `flutter test --no-pub`: 471 passed
- `flutter analyze --no-pub`: no issues
- `flutter_plugin_tools validate --check-for-missing-changes --packages go_router`: no issues
- `flutter_plugin_tools publish-check --packages go_router`: no issues
- License check: passed
Release note: This package uses batch releases, so the fix is recorded in `pending_changelogs/shell_route_key_collisions.yaml`; `pubspec.yaml` and `CHANGELOG.md` remain unchanged.
## Pre-Review Checklist
[^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.1 parent 975717a commit 17e14c2
9 files changed
Lines changed: 1315 additions & 68 deletions
File tree
- packages/go_router
- lib/src
- pending_changelogs
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
344 | 344 | | |
345 | 345 | | |
346 | 346 | | |
347 | | - | |
| 347 | + | |
348 | 348 | | |
349 | 349 | | |
350 | 350 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
177 | | - | |
| 177 | + | |
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
| |||
336 | 336 | | |
337 | 337 | | |
338 | 338 | | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
339 | 382 | | |
340 | 383 | | |
341 | 384 | | |
| |||
404 | 447 | | |
405 | 448 | | |
406 | 449 | | |
407 | | - | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
408 | 455 | | |
409 | 456 | | |
410 | 457 | | |
411 | 458 | | |
412 | | - | |
413 | | - | |
| 459 | + | |
| 460 | + | |
414 | 461 | | |
415 | 462 | | |
416 | 463 | | |
| |||
616 | 663 | | |
617 | 664 | | |
618 | 665 | | |
619 | | - | |
620 | | - | |
621 | | - | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
622 | 678 | | |
623 | 679 | | |
624 | 680 | | |
| |||
631 | 687 | | |
632 | 688 | | |
633 | 689 | | |
| 690 | + | |
634 | 691 | | |
635 | 692 | | |
636 | 693 | | |
637 | 694 | | |
638 | 695 | | |
639 | | - | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
640 | 704 | | |
641 | 705 | | |
642 | 706 | | |
643 | 707 | | |
644 | 708 | | |
645 | | - | |
646 | | - | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
647 | 712 | | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
648 | 726 | | |
649 | | - | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
650 | 736 | | |
651 | 737 | | |
652 | 738 | | |
| |||
0 commit comments