Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaceUrl not preserved in redirect #53503

Closed
stephenbunch opened this issue Dec 11, 2023 · 4 comments
Closed

replaceUrl not preserved in redirect #53503

stephenbunch opened this issue Dec 11, 2023 · 4 comments
Labels
area: router breaking changes P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@stephenbunch
Copy link

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

Yes

Description

If I call router.navigate with replaceUrl: true, and if I have a CanActivateFn that returns a UrlTree, the resulting redirect loses the replaceUrl setting.

The relevant code is here:

// The URL is already updated at this point if we have 'eager' URL
// updates or if the navigation was triggered by the browser (back
// button, URL bar, etc). We want to replace that item in history
// if the navigation is rejected.
replaceUrl: this.urlUpdateStrategy === 'eager' ||
isBrowserTriggeredNavigation(currentTransition.source)

I can fix the issue by using replaceUrl setting if it's defined:

const extras = {
  // ...
  replaceUrl: currentTransition.extras.replaceUrl ?? (this.urlUpdateStrategy === 'eager' ||
  isBrowserTriggeredNavigation(currentTransition.source))
};

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-ih775l?file=src%2Fmain.ts

Please provide the exception or error you saw

In the demo app I provided, when you click "Replace: Baz -> Bar", you should see "replace: /bar" in the history, but instead you just see "/bar".

Please provide the environment you discovered this bug in (run ng version)

"dependencies": {
  "rxjs": "7.8.1",
  "tslib": "2.6.2",
  "zone.js": "0.14.2",
  "@angular/core": "17.0.6",
  "@angular/forms": "17.0.6",
  "@angular/common": "17.0.6",
  "@angular/router": "17.0.6",
  "@angular/compiler": "17.0.6",
  "@angular/animations": "17.0.6",
  "@angular/platform-browser": "17.0.6"
},

Anything else?

No response

@atscott
Copy link
Contributor

atscott commented Dec 11, 2023

Indeed, this is also true of all the other properties in NavigationBehaviorOptions. Based on the comment here #27148 (comment), it seems like this was considered but not done. This was before my time as the router caretaker. I noticed this when adding the info and browserUrl properties and decided the properties should be carried over by default.

Changing this behavior would need to land in a major version. We would also need to do some preliminary tests to determine how breaking it is and decide if the change is worth making based on the results.

@ngbot ngbot bot modified the milestone: needsTriage Dec 11, 2023
@atscott atscott added the P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent label Dec 11, 2023
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Dec 11, 2023
@atscott
Copy link
Contributor

atscott commented Dec 20, 2023

I ran tests inside google and found that updating replaceUrl value to be carried over for the redirect passed all tests. This change should be easy to land for v18.

@atscott atscott modified the milestones: Backlog, v18-candidates Dec 20, 2023
blathers16 added a commit to blathers16/angular that referenced this issue Jan 23, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503
@blathers16
Copy link
Contributor

Hi, @atscott I have pushed a pull request for this, please let me know if it needs any changes.

blathers16 added a commit to blathers16/angular that referenced this issue Feb 3, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503
blathers16 added a commit to blathers16/angular that referenced this issue Feb 3, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503
blathers16 added a commit to blathers16/angular that referenced this issue Feb 3, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503
blathers16 added a commit to blathers16/angular that referenced this issue Feb 7, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503
blathers16 added a commit to blathers16/angular that referenced this issue Feb 21, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503
blathers16 added a commit to blathers16/angular that referenced this issue Feb 21, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503
blathers16 added a commit to blathers16/angular that referenced this issue Feb 24, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503
blathers16 added a commit to blathers16/angular that referenced this issue Feb 24, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503
blathers16 added a commit to blathers16/angular that referenced this issue Feb 24, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503
atscott pushed a commit to blathers16/angular that referenced this issue Mar 26, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503

BREAKING CHANGE: When a a guard returns a `UrlTree` as a redirect, the
redirecting navigation will now use `replaceUrl` if the initial
navigation was also using the `replaceUrl` option. If this is not
desirable, the redirect can configure new `NavigationBehaviorOptions` by
returning a `RedirectCommand` with the desired options instead of `UrlTree`.
atscott pushed a commit to blathers16/angular that referenced this issue Mar 26, 2024
…ivate

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503

BREAKING CHANGE: When a a guard returns a `UrlTree` as a redirect, the
redirecting navigation will now use `replaceUrl` if the initial
navigation was also using the `replaceUrl` option. If this is not
desirable, the redirect can configure new `NavigationBehaviorOptions` by
returning a `RedirectCommand` with the desired options instead of `UrlTree`.
ilirbeqirii pushed a commit to ilirbeqirii/angular that referenced this issue Apr 6, 2024
…ivate (angular#54042)

This commit will fix the issue of the setting of NavigationExtras.replaceUrl being lost when
returning a urlTree from a CanActivateFn.

Fixes angular#53503

BREAKING CHANGE: When a a guard returns a `UrlTree` as a redirect, the
redirecting navigation will now use `replaceUrl` if the initial
navigation was also using the `replaceUrl` option. If this is not
desirable, the redirect can configure new `NavigationBehaviorOptions` by
returning a `RedirectCommand` with the desired options instead of `UrlTree`.

PR Close angular#54042
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: router breaking changes P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants