Skip to content

Commit 7917a27

Browse files
vsavkinbrandonroberts
authored andcommitted
fix(RouterStore): change the default serializer to work around cycles in RouterStateSnapshot
1 parent a0f45ff commit 7917a27

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed
Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
1-
import { InjectionToken } from '@angular/core';
2-
import { RouterStateSnapshot } from '@angular/router';
1+
import { ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
32

43
export abstract class RouterStateSerializer<T> {
54
abstract serialize(routerState: RouterStateSnapshot): T;
65
}
76

7+
export interface SerializedRouterStateSnapshot {
8+
root: ActivatedRouteSnapshot;
9+
url: string;
10+
}
11+
812
export class DefaultRouterStateSerializer
9-
implements RouterStateSerializer<RouterStateSnapshot> {
10-
serialize(routerState: RouterStateSnapshot) {
11-
return routerState;
13+
implements RouterStateSerializer<SerializedRouterStateSnapshot> {
14+
serialize(routerState: RouterStateSnapshot): SerializedRouterStateSnapshot {
15+
return {
16+
root: this.serializeRoute(routerState.root),
17+
url: routerState.url,
18+
};
19+
}
20+
21+
private serializeRoute(
22+
route: ActivatedRouteSnapshot
23+
): ActivatedRouteSnapshot {
24+
const children = route.children.map(c => this.serializeRoute(c));
25+
return {
26+
params: route.params,
27+
paramMap: route.paramMap,
28+
data: route.data,
29+
url: route.url,
30+
outlet: route.outlet,
31+
routeConfig: {
32+
component: route.routeConfig ? route.routeConfig.component : undefined,
33+
},
34+
queryParams: route.queryParams,
35+
queryParamMap: route.queryParamMap,
36+
fragment: route.fragment,
37+
component: (route.routeConfig
38+
? route.routeConfig.component
39+
: undefined) as any,
40+
root: undefined as any,
41+
parent: undefined as any,
42+
firstChild: children[0],
43+
pathFromRoot: undefined as any,
44+
children,
45+
};
1246
}
1347
}

0 commit comments

Comments
 (0)