Skip to content

Commit dbd50be

Browse files
committed
fix(router): Do not intercept reload events with Navigation integration
This commit prevents the Router from intercepting reload navigations in the navigate event listener. This would convert hard page reloads to SPA navigations. fixes #66746
1 parent fb05fc8 commit dbd50be

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

packages/router/src/statemanager/navigation_state_manager.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ export class NavigationStateManager extends StateManager {
239239
// initiated outside the router, we do need to replace it with a router-triggered navigation
240240
// to add the router-specific state.
241241
(navigationEvent &&
242-
(navigationEvent.navigationType === 'traverse' ||
243-
navigationEvent.navigationType === 'reload') &&
242+
navigationEvent.navigationType === 'traverse' &&
244243
this.eventAndRouterDestinationsMatch(navigationEvent, transition))
245244
) {
246245
return;
@@ -388,7 +387,10 @@ export class NavigationStateManager extends StateManager {
388387
private handleNavigate(event: NavigateEvent) {
389388
// If the event cannot be intercepted (e.g., cross-origin, or some browser-internal
390389
// navigations), let the browser handle it.
391-
if (!event.canIntercept) {
390+
// We also do not convert reload navigation events to SPA navigations. Intercepting
391+
// would prevent the generally expected hard refresh. If an application wants special
392+
// handling for reloads, they can implement it in their own `navigate` event listener.
393+
if (!event.canIntercept || event.navigationType === 'reload') {
392394
return;
393395
}
394396

packages/router/test/with_platform_navigation.spec.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {TestBed} from '@angular/core/testing';
10-
import {NavigationStart, provideRouter, Router} from '../src';
10+
import {NavigationStart, provideRouter, Event, Router} from '../src';
1111
import {withExperimentalPlatformNavigation, withRouterConfig} from '../src/provide_router';
1212
import {withBody} from '@angular/private/testing';
1313
import {
@@ -261,5 +261,29 @@ if (typeof window !== 'undefined' && 'navigation' in window) {
261261
expect(router.url).toBe('/somewhere');
262262
}),
263263
);
264+
265+
it('should not convert reload events to SPA navigations', async () => {
266+
const navigation = TestBed.inject(PlatformNavigation);
267+
navigation.addEventListener(
268+
'navigate',
269+
(e: NavigateEvent) => {
270+
e.intercept({
271+
handler: () => new Promise((_, reject) => setTimeout(reject, 2)),
272+
});
273+
},
274+
{once: true},
275+
);
276+
const routerEvents: Event[] = [];
277+
router.events.subscribe((e) => routerEvents.push(e));
278+
router.resetConfig([
279+
{
280+
path: '**',
281+
children: [],
282+
},
283+
]);
284+
navigation.reload();
285+
await timeout(3);
286+
expect(routerEvents).toEqual([]);
287+
});
264288
});
265289
}

0 commit comments

Comments
 (0)