From 71f008f9069c731bbf9b762da272398984c2c63e Mon Sep 17 00:00:00 2001 From: ggradnig Date: Sun, 17 May 2020 15:16:08 +0200 Subject: [PATCH] fix(router): remove parenthesis for primary outlet segment after removing auxiliary outlet segment (#24656) (#37163) For URLs that use auxiliary route outlets in the second or following path segments, when removing the auxiliary route segment, parenthesis remain for the primary outlet segment. This causes the following error when trying to reload an URL: "Cannot match any route". The commit adds a check for this scenario, serializing the URL as "a/b" instead of "a/(b)". PR Close #24656 PR Close #37163 --- packages/router/src/url_tree.ts | 5 +++++ packages/router/test/create_url_tree.spec.ts | 6 ++++++ packages/router/test/integration.spec.ts | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/router/src/url_tree.ts b/packages/router/src/url_tree.ts index 010e7556ea1..7dd79e97c93 100644 --- a/packages/router/src/url_tree.ts +++ b/packages/router/src/url_tree.ts @@ -341,6 +341,11 @@ function serializeSegment(segment: UrlSegmentGroup, root: boolean): string { return [`${k}:${serializeSegment(v, false)}`]; }); + // use no parenthesis if the only child is a primary outlet route + if (Object.keys(segment.children).length === 1 && segment.children[PRIMARY_OUTLET] != null) { + return `${serializePaths(segment)}/${children[0]}`; + } + return `${serializePaths(segment)}/(${children.join('//')})`; } } diff --git a/packages/router/test/create_url_tree.spec.ts b/packages/router/test/create_url_tree.spec.ts index 36cd73c7899..0fc4a19232b 100644 --- a/packages/router/test/create_url_tree.spec.ts +++ b/packages/router/test/create_url_tree.spec.ts @@ -128,6 +128,12 @@ describe('createUrlTree', () => { expect(serializer.serialize(t)).toEqual('/a'); }); + it('should support removing parenthesis for primary segment on second path element', () => { + const p = serializer.parse('/a/(b//right:c)'); + const t = createRoot(p, ['a', {outlets: {right: null}}]); + expect(serializer.serialize(t)).toEqual('/a/b'); + }); + it('should update matrix parameters', () => { const p = serializer.parse('/a;pp=11'); const t = createRoot(p, ['/a', {pp: 22, dd: 33}]); diff --git a/packages/router/test/integration.spec.ts b/packages/router/test/integration.spec.ts index 7ffe1381880..09e9d43f7d4 100644 --- a/packages/router/test/integration.spec.ts +++ b/packages/router/test/integration.spec.ts @@ -3351,7 +3351,7 @@ describe('Integration', () => { advance(fixture); expect(log.map((a: any) => a.path)).toEqual(['b']); - expect(location.path()).toEqual('/two-outlets/(a)'); + expect(location.path()).toEqual('/two-outlets/a'); }))); it('works with a nested route',