Skip to content
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

Merged
merged 5 commits into from
May 20, 2019

Conversation

timdeschryver
Copy link
Member

@timdeschryver timdeschryver commented May 9, 2019

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

By default, the router event exists on the router-store action e.g. NavigationStart and RoutesRecognized. 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 and RouterState.Minimal. If set to Full, the current behavior remains. When set to Minimal, a new router serializer MinimalRouterStateSerializer 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?

[ ] Yes
[x] No

Other information

@timdeschryver timdeschryver force-pushed the pr/router-navigation-id branch from 07bd0cb to d7a486e Compare May 10, 2019 20:29
@timdeschryver timdeschryver changed the title feat(router-store): add onlyEventNavigationId config property feat(router-store): add routerState config option May 10, 2019
@timdeschryver
Copy link
Member Author

timdeschryver commented May 10, 2019

Updated the whole PR with feedback given. I'm not quite happy on the docs part... feedback is welcome 😃

@ngrxbot
Copy link
Collaborator

ngrxbot commented May 10, 2019

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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 () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('should dispatch the navidation id with url', async () => {
it('should dispatch the navigation id with url', async () => {

@timdeschryver
Copy link
Member Author

Thanks Brandon!

@ngrxbot
Copy link
Collaborator

ngrxbot commented May 10, 2019

Preview docs changes for 78137b3 at https://previews.ngrx.io/pr1847-78137b3/

import { BaseRouterStoreState, RouterStateSerializer } from './shared';

export interface MinimalRouteSnapshot {
params: RouterStateSnapshot['root']['params'];
Copy link
Member

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?

Copy link
Member Author

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## routerState
## Router State Snapshot

params: RouterStateSnapshot['root']['params'];
url: RouterStateSnapshot['root']['params'];
queryParams: RouterStateSnapshot['root']['params'];
data: RouterStateSnapshot['root']['params'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data: RouterStateSnapshot['root']['params'];
data: RouterStateSnapshot['root']['data'];

@ngrxbot
Copy link
Collaborator

ngrxbot commented May 14, 2019

Preview docs changes for 7c50710 at https://previews.ngrx.io/pr1847-7c50710/

url: string;
}

export class MinimalRouterStateSerializer
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member Author

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.

@ngrxbot
Copy link
Collaborator

ngrxbot commented May 14, 2019

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
Copy link
Member Author

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

Copy link
Member

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'];
Copy link
Member Author

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?

Copy link
Member

@brandonroberts brandonroberts May 16, 2019

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?

Copy link
Member Author

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.

Copy link
Member

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

@ngrxbot
Copy link
Collaborator

ngrxbot commented May 16, 2019

Preview docs changes for 2f23e4c at https://previews.ngrx.io/pr1847-2f23e4c/

@brandonroberts brandonroberts merged commit d874cfc into master May 20, 2019
@brandonroberts brandonroberts deleted the pr/router-navigation-id branch May 20, 2019 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

strictActionSerializability does not work with RouterModule (and custom serializer)
4 participants