Skip to content

Commit

Permalink
feat(router): enabling MinimalRouterStateSerializer by default (#2326)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

The MinimalRouterStateSerializer is enabled by default.

BEFORE:

If no router state serializer is provided through the configuration of router store, the DefaultRouterStateSerializer is used.

AFTER: 

If no router state serializer is provided through the configuration of router store, the MinimalRouterStateSerializer is used.

Closes #2225
  • Loading branch information
evgenyfedorenko authored Feb 5, 2020
1 parent d7fdf7f commit ba37ad8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
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
4 changes: 1 addition & 3 deletions projects/example-app/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,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
6 changes: 3 additions & 3 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 serializability runtime checks enabled, the `MinimalRouterStateSerializer` serializer **must** be used. This also applies to Ivy with immutability runtime checks.

</div>

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

0 comments on commit ba37ad8

Please sign in to comment.