-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(router-store): add routerState config option #1847
Conversation
07bd0cb
to
d7a486e
Compare
Updated the whole PR with feedback given. I'm not quite happy on the docs part... feedback is welcome 😃 |
Preview docs changes for d7a486e at https://previews.ngrx.io/pr1847-d7a486e/ |
|
||
### RouterState.Minimal | ||
|
||
`RouterState.Minimal` will use the `MinimalRouterStateSerializer` serializer to serialize Angular router event. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`RouterState.Minimal` will use the `MinimalRouterStateSerializer` serializer to serialize Angular router event. | |
`RouterState.Minimal` will use the `MinimalRouterStateSerializer` serializer to serialize the Angular Router's `RouterState` and `RouterEvent`. |
|
||
The metadata on the action consists of the navigation id and the url. | ||
|
||
```ts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
```ts | |
```json |
|
||
When this property is set to `RouterState.Full`, `@ngrx/router-store` will use the `DefaultRouterStateSerializer` serializer to serialize the Angular router event. | ||
|
||
The metadata on the action will contain the Angular router event, e.g. NavigationStart` and `RoutesRecognized`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The metadata on the action will contain the Angular router event, e.g. NavigationStart` and `RoutesRecognized`. | |
The metadata on the action will contain the Angular router event, e.g. `NavigationStart` and `RoutesRecognized`. |
@@ -5,12 +5,14 @@ interface StoreRouterConfig { | |||
stateKey?: string | Selector<any, RouterReducerState<T>>; | |||
serializer?: new (...args: any[]) => RouterStateSerializer; | |||
navigationActionTiming?: NavigationActionTiming; | |||
state?: RouterState; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
state?: RouterState; | |
routerState?: RouterState; |
/** | ||
* Decides which router serializer should be used, if there is none provided, and the metadata on the dispatched @ngrx/router-store action payload. | ||
* Set to `Full` to use the `DefaultRouterStateSerializer` and to set the angular router events as payload. | ||
* Set to `Minimal` to use the `MinimalRouterStateSerializer` and to set a minimal router event with the nagivation id and url as payload. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Set to `Minimal` to use the `MinimalRouterStateSerializer` and to set a minimal router event with the nagivation id and url as payload. | |
* Set to `Minimal` to use the `MinimalRouterStateSerializer` and to set a minimal router event with the navigation id and url as payload. |
}); | ||
|
||
describe('Minimal', () => { | ||
it('should dispatch the navidation id with url', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it('should dispatch the navidation id with url', async () => { | |
it('should dispatch the navigation id with url', async () => { |
Thanks Brandon! |
Preview docs changes for 78137b3 at https://previews.ngrx.io/pr1847-78137b3/ |
import { BaseRouterStoreState, RouterStateSerializer } from './shared'; | ||
|
||
export interface MinimalRouteSnapshot { | ||
params: RouterStateSnapshot['root']['params']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should all these be the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No they should not 😐
@@ -88,3 +90,52 @@ StoreRouterConnectingModule.forRoot({ | |||
navigationActionTiming: NavigationActionTiming.PostActivation, | |||
}); | |||
</code-example> | |||
|
|||
## routerState |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## routerState | |
## Router State Snapshot |
params: RouterStateSnapshot['root']['params']; | ||
url: RouterStateSnapshot['root']['params']; | ||
queryParams: RouterStateSnapshot['root']['params']; | ||
data: RouterStateSnapshot['root']['params']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data: RouterStateSnapshot['root']['params']; | |
data: RouterStateSnapshot['root']['data']; |
Preview docs changes for 7c50710 at https://previews.ngrx.io/pr1847-7c50710/ |
url: string; | ||
} | ||
|
||
export class MinimalRouterStateSerializer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After taking a second look at this, I think this should match what the custom serializer does in the docs. If we just give them the root snapshot, the params will be empty, the data will be empty, only leaving the url and global queryParams.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would make more sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pushed these changes. One caveat is that users won't be able to access params from higher up. Another implementation I've encountered a few times is to loop over all of the children and pluck the params from each child and build up an object with all of the params.
Preview docs changes for 93266b0 at https://previews.ngrx.io/pr1847-93266b0/ |
delete snapshot.queryParamMap; | ||
delete snapshot.component; | ||
|
||
// properties that do not exist on the minimal serializer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this OK to leave out?
I did this because these are always undefined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep
import { BaseRouterStoreState, RouterStateSerializer } from './shared'; | ||
|
||
export interface MinimalActivatedRouteSnapshot { | ||
routeConfig: ActivatedRouteSnapshot['routeConfig']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way to type this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than to use its actual type of Route
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I tried to just use ActivatedRouteSnapshot
and override some properties but I'm not sure if this is possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure either
Preview docs changes for 2f23e4c at https://previews.ngrx.io/pr1847-2f23e4c/ |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
By default, the router event exists on the router-store action e.g.
NavigationStart
andRoutesRecognized
. The problem is that these events are not serializable.Closes #1834
What is the new behavior?
This changes introduces a new config parameter,
routerState
which has two options :RouterState.Full
andRouterState.Minimal
. If set toFull
, the current behavior remains. When set toMinimal
, a new router serializerMinimalRouterStateSerializer
will be used to serialize the router event, it will also only use the navigation id and the url from the router event to append to the@ngrx/router-store
action.If a serializer is already provided, it will continue to use the custom serializer.
Does this PR introduce a breaking change?
Other information