-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(RouterStore): Add serializer for router state snapshot (#188)
This adds a serializer that can be customized for returning the router state snapshot. By default, the entire RouterStateSnapshot is returned. Documentation has been updated with example usage. ```ts import { StoreModule } from '@ngrx/store'; import { StoreRouterConnectingModule, routerReducer, RouterStateSerializer } from '@ngrx/router-store'; export interface RouterStateUrl { url: string; } export class CustomSerializer implements RouterStateSerializer<RouterStateUrl> { serialize(routerState: RouterStateSnapshot): RouterStateUrl { const url = routerState ? routerState.url : ''; // Only return an object including the URL // instead of the entire snapshot return { url }; } } @NgModule({ imports: [ StoreModule.forRoot({ routerReducer: routerReducer }), RouterModule.forRoot([ // routes ]), StoreRouterConnectingModule ], providers: [ { provide: RouterStateSerializer, useClass: CustomSerializer } ] }) export class AppModule { } ``` Closes #97, #104, #237
- Loading branch information
1 parent
a4609e5
commit 0fc1bcc
Showing
9 changed files
with
191 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# API | ||
|
||
## Custom Router State Serializer | ||
|
||
During each navigation cycle, a `RouterNavigationAction` is dispatched with a snapshot of the state in its payload, the `RouterStateSnapshot`. The `RouterStateSnapshot` is a large complex structure, containing many pieces of information about the current state and what's rendered by the router. This can cause performance | ||
issues when used with the Store Devtools. In most cases, you may only need a piece of information from the `RouterStateSnapshot`. In order to pair down the `RouterStateSnapshot` provided during navigation, you provide a custom serializer for the snapshot to only return what you need to be added to the payload and store. | ||
|
||
To use the time-traveling debugging in the Devtools, you must return an object containing the `url` when using the `routerReducer`. | ||
|
||
```ts | ||
import { StoreModule } from '@ngrx/store'; | ||
import { | ||
StoreRouterConnectingModule, | ||
routerReducer, | ||
RouterStateSerializer, | ||
RouterStateSnapshotType | ||
} from '@ngrx/router-store'; | ||
|
||
export interface RouterStateUrl { | ||
url: string; | ||
} | ||
|
||
export class CustomSerializer implements RouterStateSerializer<RouterStateUrl> { | ||
serialize(routerState: RouterStateSnapshot): RouterStateUrl { | ||
const { url } = routerState; | ||
|
||
// Only return an object including the URL | ||
// instead of the entire snapshot | ||
return { url }; | ||
} | ||
} | ||
|
||
@NgModule({ | ||
imports: [ | ||
StoreModule.forRoot({ routerReducer: routerReducer }), | ||
RouterModule.forRoot([ | ||
// routes | ||
]), | ||
StoreRouterConnectingModule | ||
], | ||
providers: [ | ||
{ provide: RouterStateSerializer, useClass: CustomSerializer } | ||
] | ||
}) | ||
export class AppModule { } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { RouterStateSerializer } from '@ngrx/router-store'; | ||
import { RouterStateSnapshot } from '@angular/router'; | ||
|
||
/** | ||
* The RouterStateSerializer takes the current RouterStateSnapshot | ||
* and returns any pertinent information needed. The snapshot contains | ||
* all information about the state of the router at the given point in time. | ||
* The entire snapshot is complex and not always needed. In this case, you only | ||
* need the URL from the snapshot in the store. Other items could be | ||
* returned such as route parameters, query parameters and static route data. | ||
*/ | ||
|
||
export interface RouterStateUrl { | ||
url: string; | ||
} | ||
|
||
export class CustomRouterStateSerializer | ||
implements RouterStateSerializer<RouterStateUrl> { | ||
serialize(routerState: RouterStateSnapshot): RouterStateUrl { | ||
const { url } = routerState; | ||
|
||
return { url }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
import { RouterStateSnapshot } from '@angular/router'; | ||
|
||
export abstract class RouterStateSerializer<T> { | ||
abstract serialize(routerState: RouterStateSnapshot): T; | ||
} | ||
|
||
export class DefaultRouterStateSerializer | ||
implements RouterStateSerializer<RouterStateSnapshot> { | ||
serialize(routerState: RouterStateSnapshot) { | ||
return routerState; | ||
} | ||
} |