Skip to content

[go_router_builder] Nest absolute child paths under their parent - #13718

Open
yeegz wants to merge 2 commits into
flutter:mainfrom
yeegz:fix/156382-builder-absolute-child-path
Open

yeegz wants to merge 2 commits into
flutter:mainfrom
yeegz:fix/156382-builder-absolute-child-path

Conversation

@yeegz

@yeegz yeegz commented Nov 19, 2026

Copy link
Copy Markdown

A child route whose path starts with / (e.g. /setting under /profile) is nested under its parent at runtime, because go_router joins paths with concatenatePaths. The generated location used p.url.joinAll instead, which drops the parent when a segment is absolute, so SettingRoute().go(context) went to /setting instead of /profile/setting.

This strips the leading / from child segments before joining, so only that case changes. Relative routes and trailing slashes come out exactly as before, and every existing golden still passes. The same joined path is used by the duplicate-path check, so that now sees the real path too.

Added test_inputs/absolute_child_path.dart. Without the change its location is '/setting'; with it, '/profile/setting'.

Fixes flutter/flutter#156382

Pre-Review Checklist

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

A child route whose path starts with `/` is nested under its parent at
runtime (go_router's concatenatePaths), but the generated `location`
used p.url.joinAll, which drops the parent for an absolute segment.
Strip the leading `/` from child segments before joining.

Fixes flutter/flutter#156382
@github-actions github-actions Bot added p: go_router_builder P2 Important issues not at the top of the work list labels Nov 19, 2026

@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 updates go_router_builder to correctly nest child routes whose paths start with / under their parent route when generating the location property, matching runtime behavior. A test case has been added to verify this change. Feedback suggests optimizing the path segment processing in route_config.dart by replacing the RegExp instantiation inside the loop with startsWith and substring to improve efficiency.

Comment on lines +226 to +228
for (var i = 1; i < segments.length; i++) {
segments[i] = segments[i].replaceFirst(RegExp('^/+'), '');
}

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.

medium

Instantiating a RegExp inside a loop compiles the regular expression on every iteration, which is inefficient. Since we only need to strip leading slashes, we can achieve the same result more efficiently without regular expressions by using startsWith and substring in a loop.

    for (var i = 1; i < segments.length; i++) {
      var segment = segments[i];
      while (segment.startsWith('/')) {
        segment = segment.substring(1);
      }
      segments[i] = segment;
    }

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

Labels

p: go_router_builder 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_builder]: Since go_router: 14.2.9, generated locations are different for route paths with or without slash

1 participant