Skip to content

Commit

Permalink
refactor(router): Update internal INITIAL_NAVIGATION token to use an …
Browse files Browse the repository at this point in the history
…enum (#46925)

This updates the internal use of INITIAL_NAVIGATION to do two things:

1. Explicitly provide `Enabled` as the default for the token factory
2. Use an enum instead of a string to reduce bundle size

PR Close #46925
  • Loading branch information
atscott authored and Pawel Kozlowski committed Jul 22, 2022
1 parent 55f485b commit f5d764d
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions packages/router/src/router_module.ts
Expand Up @@ -221,8 +221,7 @@ export function getBootstrapListener() {
const router = injector.get(Router);
const bootstrapDone = injector.get(BOOTSTRAP_DONE);

// Default case
if (injector.get(INITIAL_NAVIGATION, null, InjectFlags.Optional) === null) {
if (injector.get(INITIAL_NAVIGATION) === InitialNavigation.EnabledNonBlocking) {
router.initialNavigation();
}

Expand Down Expand Up @@ -274,7 +273,7 @@ const BOOTSTRAP_DONE =

function provideEnabledBlockingInitialNavigation(): Provider {
return [
{provide: INITIAL_NAVIGATION, useValue: 'enabledBlocking'},
{provide: INITIAL_NAVIGATION, useValue: InitialNavigation.EnabledBlocking},
{
provide: APP_INITIALIZER,
multi: true,
Expand Down Expand Up @@ -351,8 +350,32 @@ function provideEnabledBlockingInitialNavigation(): Provider {
];
}

const INITIAL_NAVIGATION =
new InjectionToken<'disabled'|'enabledBlocking'>(NG_DEV_MODE ? 'initial navigation' : '');
/**
* This and the INITIAL_NAVIGATION token are used internally only. The public API side of this is
* configured through the `ExtraOptions`.
*
* When set to `EnabledBlocking`, the initial navigation starts before the root
* component is created. The bootstrap is blocked until the initial navigation is complete. This
* value is required for [server-side rendering](guide/universal) to work.
*
* When set to `EnabledNonBlocking`, the initial navigation starts after the root component has been
* created. The bootstrap is not blocked on the completion of the initial navigation.
*
* When set to `Disabled`, the initial navigation is not performed. The location listener is set up
* before the root component gets created. Use if there is a reason to have more control over when
* the router starts its initial navigation due to some complex initialization logic.
*
* @see ExtraOptions
*/
const enum InitialNavigation {
EnabledBlocking,
EnabledNonBlocking,
Disabled,
}

const INITIAL_NAVIGATION = new InjectionToken<InitialNavigation>(
NG_DEV_MODE ? 'initial navigation' : '',
{providedIn: 'root', factory: () => InitialNavigation.EnabledNonBlocking});

function provideDisabledInitialNavigation(): Provider[] {
return [
Expand All @@ -366,7 +389,7 @@ function provideDisabledInitialNavigation(): Provider[] {
};
}
},
{provide: INITIAL_NAVIGATION, useValue: 'disabled'}
{provide: INITIAL_NAVIGATION, useValue: InitialNavigation.Disabled}
];
}

Expand Down

0 comments on commit f5d764d

Please sign in to comment.