Skip to content

Commit dd58c4b

Browse files
atscottkirjs
authored andcommitted
refactor(common): Add token to indicate whether precommit handler is supported
This commit adds a token that indicates whether the precommitHandler feature is supported by the navigation api. https://developer.mozilla.org/en-US/docs/Web/API/NavigateEvent/intercept#precommithandler
1 parent 91dc91b commit dd58c4b

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

packages/common/src/navigation/platform_navigation.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {
1010
Injectable,
11+
InjectionToken,
1112
ɵNavigateEvent as NavigateEvent,
1213
ɵNavigation as Navigation,
1314
ɵNavigationCurrentEntryChangeEvent as NavigationCurrentEntryChangeEvent,
@@ -20,6 +21,19 @@ import {
2021
ɵNavigationUpdateCurrentEntryOptions as NavigationUpdateCurrentEntryOptions,
2122
} from '@angular/core';
2223

24+
/**
25+
* @see https://developer.mozilla.org/en-US/docs/Web/API/NavigationPrecommitController
26+
* @see https://developer.mozilla.org/en-US/docs/Web/API/NavigateEvent/intercept#precommithandler
27+
*/
28+
export const PRECOMMIT_HANDLER_SUPPORTED = new InjectionToken<boolean>('', {
29+
factory: () => {
30+
return (
31+
typeof window !== 'undefined' &&
32+
typeof (window as any).NavigationPrecommitController !== 'undefined'
33+
);
34+
},
35+
});
36+
2337
/**
2438
* This class wraps the platform Navigation API which allows server-specific and test
2539
* implementations.

packages/common/src/private_export.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export {
1212
setRootDomAdapter as ɵsetRootDomAdapter,
1313
} from './dom_adapter';
1414
export {NavigationAdapterForLocation as ɵNavigationAdapterForLocation} from './location/navigation_adapter_for_location';
15+
export {PRECOMMIT_HANDLER_SUPPORTED as ɵPRECOMMIT_HANDLER_SUPPORTED} from './navigation/platform_navigation';

packages/common/testing/src/navigation/provide_fake_platform_navigation.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {DOCUMENT, PlatformLocation, PlatformNavigation} from '../../../index';
1010
import {inject, InjectionToken, Provider} from '@angular/core';
11+
import {PRECOMMIT_HANDLER_SUPPORTED} from '../../../src/navigation/platform_navigation';
1112

1213
import {
1314
FakeNavigationPlatformLocation,
@@ -38,5 +39,6 @@ export function provideFakePlatformNavigation(): Provider[] {
3839
useFactory: () => inject(FAKE_NAVIGATION),
3940
},
4041
{provide: PlatformLocation, useClass: FakeNavigationPlatformLocation},
42+
{provide: PRECOMMIT_HANDLER_SUPPORTED, useValue: true},
4143
];
4244
}

packages/router/src/statemanager/navigation_state_manager.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import {
1414
Injectable,
1515
} from '@angular/core';
1616

17-
import {PlatformLocation, PlatformNavigation} from '@angular/common';
17+
import {
18+
PlatformLocation,
19+
PlatformNavigation,
20+
ɵPRECOMMIT_HANDLER_SUPPORTED as PRECOMMIT_HANDLER_SUPPORTED,
21+
} from '@angular/common';
1822
import {StateManager} from './state_manager';
1923
import {RestoredState, Navigation as RouterNavigation} from '../navigation_transition';
2024
import {
@@ -59,6 +63,7 @@ export class NavigationStateManager extends StateManager {
5963
/** The root URL of the Angular application, considering the base href. */
6064
private readonly appRootURL = new URL(this.location.prepareExternalUrl?.('/') ?? '/', this.base)
6165
.href;
66+
private readonly precommitHandlerSupported = inject(PRECOMMIT_HANDLER_SUPPORTED);
6267
/**
6368
* The `NavigationHistoryEntry` from the Navigation API that corresponds to the last successfully
6469
* activated router state. This is crucial for restoring the browser state if an ongoing navigation

0 commit comments

Comments
 (0)