Repository navigation
Conversation
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
There was a problem hiding this comment.
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.
| for (var i = 1; i < segments.length; i++) { | ||
| segments[i] = segments[i].replaceFirst(RegExp('^/+'), ''); | ||
| } |
There was a problem hiding this comment.
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;
}
A child route whose path starts with
/(e.g./settingunder/profile) is nested under its parent at runtime, because go_router joins paths withconcatenatePaths. The generatedlocationusedp.url.joinAllinstead, which drops the parent when a segment is absolute, soSettingRoute().go(context)went to/settinginstead 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
[shared_preferences]///).Footnotes
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