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): enabling MinimalRouterStateSerializer by default #2326

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions modules/router-store/spec/router_store_module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('Router Store Module', () => {
});

describe('routerState', () => {
function setup(routerState: RouterState, serializer?: any) {
function setup(routerState?: RouterState, serializer?: any) {
createTestModule({
reducers: {},
config: {
Expand Down Expand Up @@ -172,7 +172,17 @@ describe('Router Store Module', () => {
await router.navigateByUrl('/');
});

it('should use the default router serializer', () => {
it('should use the minimal router serializer by default', () => {
const { serializer } = setup();
expect(serializer).toEqual(new MinimalRouterStateSerializer());
});

it('should use the minimal router serializer if minimal state option is passed in', () => {
const { serializer } = setup(RouterState.Minimal);
expect(serializer).toEqual(new MinimalRouterStateSerializer());
});

it('should use the default router serializer if full state option is passed in', () => {
const { serializer } = setup(RouterState.Full);
expect(serializer).toEqual(new DefaultRouterStateSerializer());
});
Expand Down
14 changes: 7 additions & 7 deletions modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function _createRouterConfig(
): StoreRouterConfig {
return {
stateKey: DEFAULT_ROUTER_FEATURENAME,
serializer: DefaultRouterStateSerializer,
serializer: MinimalRouterStateSerializer,
navigationActionTiming: NavigationActionTiming.PreActivation,
...config,
};
Expand Down Expand Up @@ -168,9 +168,9 @@ export class StoreRouterConnectingModule {
provide: RouterStateSerializer,
useClass: config.serializer
? config.serializer
: config.routerState === RouterState.Minimal
? MinimalRouterStateSerializer
: DefaultRouterStateSerializer,
: config.routerState === RouterState.Full
? DefaultRouterStateSerializer
: MinimalRouterStateSerializer,
},
],
};
Expand Down Expand Up @@ -328,9 +328,9 @@ export class StoreRouterConnectingModule {
routerState: this.routerState,
...payload,
event:
this.config.routerState === RouterState.Minimal
? { id: payload.event.id, url: payload.event.url }
: payload.event,
this.config.routerState === RouterState.Full
? payload.event
: { id: payload.event.id, url: payload.event.url },
},
});
} finally {
Expand Down
9 changes: 2 additions & 7 deletions projects/example-app/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import { ROOT_REDUCERS, metaReducers } from '@example-app/reducers';

import { CoreModule } from '@example-app/core';
import { AppRoutingModule } from '@example-app/app-routing.module';
import {
UserEffects,
RouterEffects
} from '@example-app/core/effects';
import { UserEffects, RouterEffects } from '@example-app/core/effects';
import { AppComponent } from '@example-app/core/containers';

@NgModule({
Expand Down Expand Up @@ -50,9 +47,7 @@ import { AppComponent } from '@example-app/core/containers';
/**
* @ngrx/router-store keeps router state up-to-date in the store.
*/
StoreRouterConnectingModule.forRoot({
routerState: RouterState.Minimal,
}),
StoreRouterConnectingModule.forRoot(),

/**
* Store devtools instrument the store retaining past versions of state
Expand Down
19 changes: 10 additions & 9 deletions projects/ngrx.io/content/guide/router-store/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ interface StoreRouterConfig {

## Default Router State Serializer

If no router state serializer is provided through the [configuration](#configuration-options) of router store, the `DefaultRouterStateSerializer` is used. This router state serializer, serializes the URL together with the [ActivatedRouteSnapshot](https://angular.io/api/router/ActivatedRouteSnapshot) from [Angular Router](https://angular.io/guide/router). The latter is serialized recursively, but only with the possibility to traverse the route downward since `root` and `parent` parameters are set to `undefined`.
`DefaultRouterStateSerializer` router state serializer, serializes the URL together with the [ActivatedRouteSnapshot](https://angular.io/api/router/ActivatedRouteSnapshot) from [Angular Router](https://angular.io/guide/router). The latter is serialized recursively, but only with the possibility to traverse the route downward since `root` and `parent` parameters are set to `undefined`.

<div class="alert is-important">

The `DefaultRouterStateSerializer` cannot be used when [serializability runtime checks](guide/store/configuration/runtime-checks) are enabled. If you want to use runtime checks to enforce serializability of your state and actions, you can configure `RouterStoreModule` to use the `MinimalRouterStateSerializer` or implement a custom router state serializer.
This also applies to Ivy with immutability runtime checks.
The `DefaultRouterStateSerializer` cannot be used when [serializability runtime checks](guide/store/configuration/runtime-checks) are enabled.
With runtime checks enabled `MinimalRouterStateSerializer` serializer is used by default in `RouterStoreModule` when no other serializer is provided. This also applies to Ivy with immutability runtime checks.
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
With runtime checks enabled `MinimalRouterStateSerializer` serializer is used by default in `RouterStoreModule` when no other serializer is provided. This also applies to Ivy with immutability runtime checks.
With serializability runtime checks enabled, the `MinimalRouterStateSerializer` serializer **must** be used. This also applies to Ivy with immutability runtime checks.


</div>

Expand All @@ -41,14 +41,14 @@ import { Params, RouterStateSnapshot } from '@angular/router';
import { RouterStateSerializer } from '@ngrx/router-store';

export interface RouterStateUrl {
url: string;
params: Params;
queryParams: Params;
url: string;
Copy link
Member

Choose a reason for hiding this comment

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

Seems like something weird has happened here with the formation, could you revert this change please?

params: Params;
queryParams: Params;
}

export class CustomSerializer implements RouterStateSerializer&lt;RouterStateUrl&gt; {
serialize(routerState: RouterStateSnapshot): RouterStateUrl {
let route = routerState.root;
serialize(routerState: RouterStateSnapshot): RouterStateUrl {
Copy link
Member

Choose a reason for hiding this comment

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

Same here

let route = routerState.root;

while (route.firstChild) {
route = route.firstChild;
Expand All @@ -63,7 +63,8 @@ export class CustomSerializer implements RouterStateSerializer&lt;RouterStateUrl
// Only return an object including the URL, params and query params
// instead of the entire snapshot
return { url, params, queryParams };
}

}
Copy link
Member

Choose a reason for hiding this comment

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

And here

}
</code-example>

Expand Down
2 changes: 1 addition & 1 deletion projects/ngrx.io/content/guide/router-store/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { AppComponent } from './app.component';
RouterModule.forRoot([
// routes
]),
// Connects RouterModule with StoreModule
// Connects RouterModule with StoreModule, uses MinimalRouterStateSerializer by default
StoreRouterConnectingModule.forRoot(),
],
bootstrap: [AppComponent],
Expand Down