Skip to content

Commit

Permalink
feat(router): Allow onSameUrlNavigation: 'ignore' in navigateByUrl (
Browse files Browse the repository at this point in the history
angular#52265)

There are cases where the application's default behavior is 'reload' and
a certain navigation might want to override this to be `ignore` instead.
This commit allows `onSameUrlNavigation` in the `router.navigateByUrl`
to be `ignore` where it was previously restricted to only `reload`.

PR Close angular#52265
  • Loading branch information
atscott authored and amilamen committed Jan 26, 2024
1 parent 9642246 commit c1d8f19
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion goldens/public-api/router/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ export interface Navigation {

// @public
export interface NavigationBehaviorOptions {
onSameUrlNavigation?: Extract<OnSameUrlNavigation, 'reload'>;
onSameUrlNavigation?: OnSameUrlNavigation;
replaceUrl?: boolean;
skipLocationChange?: boolean;
state?: {
Expand Down
2 changes: 1 addition & 1 deletion packages/router/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ export interface NavigationBehaviorOptions {
* @see {@link OnSameUrlNavigation}
* @see {@link RouterConfigOptions}
*/
onSameUrlNavigation?: Extract<OnSameUrlNavigation, 'reload'>;
onSameUrlNavigation?: OnSameUrlNavigation;

/**
* When true, navigates without pushing a new state into history.
Expand Down
27 changes: 27 additions & 0 deletions packages/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@ describe('Integration', () => {
]);
});

it('should override default onSameUrlNavigation with extras', async () => {
TestBed.configureTestingModule({
providers: [
provideRouter([], withRouterConfig({onSameUrlNavigation: 'reload'})),
]
});
const router = TestBed.inject(Router);
router.resetConfig([
{path: '', component: SimpleCmp},
{path: 'simple', component: SimpleCmp},
]);

const events: (NavigationStart|NavigationEnd)[] = [];
router.events.subscribe(e => onlyNavigationStartAndEnd(e) && events.push(e));

await router.navigateByUrl('/simple');
await router.navigateByUrl('/simple');
expectEvents(events, [
[NavigationStart, '/simple'], [NavigationEnd, '/simple'], [NavigationStart, '/simple'],
[NavigationEnd, '/simple']
]);

events.length = 0;
await router.navigateByUrl('/simple', {onSameUrlNavigation: 'ignore'});
expectEvents(events, []);
});

it('should ignore empty paths in relative links',
fakeAsync(inject([Router], (router: Router) => {
router.resetConfig([{
Expand Down

0 comments on commit c1d8f19

Please sign in to comment.