Skip to content

Commit 930ada9

Browse files
committed
fix(@angular/ssr): decode route segments when building and matching route tree
Updates the `getPathSegments` method in `RouteTree` to decode each path segment using `decodeURIComponent` after splitting the route by `/`. This ensures that encoded characters in URL segments (such as spaces or encoded slashes) are correctly interpreted when matching incoming requests against the route tree. This prevents issues where encoded segments would fail to match their corresponding route definitions or wildcard patterns in the tree. Also reverts experimental changes in `url.ts` and `router.ts` regarding matrix parameter handling, as the simple change in `route-tree.ts` is sufficient to resolve the issue and passes all tests. Adds a test in `router_spec.ts` to verify that a URL with an encoded parameter containing spaces and slashes (`Bob%20%2F%20Roberts`) correctly matches a wildcard route (`/user/*`). Fixes #33044 (cherry picked from commit 53b9623)
1 parent 0dc8a44 commit 930ada9

3 files changed

Lines changed: 11 additions & 2 deletions

File tree

packages/angular/ssr/src/routes/route-tree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class RouteTree<AdditionalMetadata extends Record<string, unknown> = {}>
207207
* @returns An array of path segments.
208208
*/
209209
private getPathSegments(route: string): string[] {
210-
return route.split('/').filter(Boolean);
210+
return route.split('/').filter(Boolean).map(decodeURIComponent);
211211
}
212212

213213
/**

packages/angular/ssr/src/routes/router.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ export class ServerRouter {
8787
// A request to `http://www.example.com/page/index.html` will render the Angular route corresponding to `http://www.example.com/page`.
8888
let { pathname } = stripIndexHtmlFromURL(url);
8989
pathname = stripMatrixParams(pathname);
90-
pathname = decodeURIComponent(pathname);
9190

9291
return this.routeTree.match(pathname);
9392
}

packages/angular/ssr/test/routes/router_spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,15 @@ describe('ServerRouter', () => {
127127
renderMode: RenderMode.Server,
128128
});
129129
});
130+
131+
it('should handle encoded params', () => {
132+
const encodedUserMetadata = router.match(
133+
new URL('http://localhost/user/Bob%20%2F%20Roberts'),
134+
);
135+
expect(encodedUserMetadata).toEqual({
136+
route: '/user/*',
137+
renderMode: RenderMode.Server,
138+
});
139+
});
130140
});
131141
});

0 commit comments

Comments
 (0)