Skip to content

Commit b203e4c

Browse files
atscottdylhunn
authored andcommitted
fix(router): create correct URL relative to path with empty child (#49691)
The previous fix for squashing empty children didn't quite work when the existing route had segments. The result would be that the segments from the existing route were dropped from the final URL. PR Close #49691
1 parent a218ef5 commit b203e4c

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

packages/router/src/create_url_tree.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,9 @@ function updateSegmentGroupChildren(
339339
const outlets = getOutlets(commands);
340340
const children: {[key: string]: UrlSegmentGroup} = {};
341341
// If the set of commands does not apply anything to the primary outlet and the child segment is
342-
// an empty path primary segment on its own, we want to skip applying the commands at this
343-
// level. Imagine the following config:
342+
// an empty path primary segment on its own, we want to apply the commands to the empty child
343+
// path rather than here. The outcome is that the empty primary child is effectively removed
344+
// from the final output UrlTree. Imagine the following config:
344345
//
345346
// {path: '', children: [{path: '**', outlet: 'popup'}]}.
346347
//
@@ -361,8 +362,9 @@ function updateSegmentGroupChildren(
361362
if (!outlets[PRIMARY_OUTLET] && segmentGroup.children[PRIMARY_OUTLET] &&
362363
segmentGroup.numberOfChildren === 1 &&
363364
segmentGroup.children[PRIMARY_OUTLET].segments.length === 0) {
364-
return updateSegmentGroupChildren(
365-
segmentGroup.children[PRIMARY_OUTLET], startIndex, commands);
365+
const childrenOfEmptyChild =
366+
updateSegmentGroupChildren(segmentGroup.children[PRIMARY_OUTLET], startIndex, commands);
367+
return new UrlSegmentGroup(segmentGroup.segments, childrenOfEmptyChild.children);
366368
}
367369

368370
Object.entries(outlets).forEach(([outlet, commands]) => {

packages/router/test/create_url_tree.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,32 @@ describe('createUrlTree', async () => {
206206
expect(serializer.serialize(t)).toEqual('/parent/child(rootSecondary:rootPopup)');
207207
});
208208

209+
it('works with named children of empty path primary, relative to non-empty parent',
210+
async () => {
211+
router.resetConfig([{
212+
path: 'case',
213+
component: class {},
214+
children: [
215+
{
216+
path: '',
217+
component: class {},
218+
children: [
219+
{path: 'foo', outlet: 'foo', children: []},
220+
],
221+
},
222+
]
223+
}]);
224+
await router.navigateByUrl('/case');
225+
expect(router.url).toEqual('/case');
226+
expect(router
227+
.createUrlTree(
228+
[{outlets: {'foo': ['foo']}}],
229+
// relative to the 'case' route
230+
{relativeTo: router.routerState.root.firstChild})
231+
.toString())
232+
.toEqual('/case/(foo:foo)');
233+
});
234+
209235
describe('absolute navigations', () => {
210236
it('with and pathless root', async () => {
211237
router.resetConfig([

0 commit comments

Comments
 (0)