Skip to content

'history.state' object is getting lost when navigate using back/forward buttons #28108

Closed

Description

@alecvolo

🐞 bug report

Affected Package

The issue is caused by package @angular/router....

Is this a regression?

No

Description

I think there is a small bug - the `history.state` object is getting lost when navigate using back/forward buttons.

The fix should be pretty simple- set the restored state into the NavigationExtras parameter in the call of scheduleNavigation in Router.setUpLocationChangeListener() method :

        // Navigations coming from Angular router have a navigationId state property. When this
        // exists, restore the state.
        const state = change.state && change.state.navigationId ? change.state : null;
     setTimeout(
            () => { this.scheduleNavigation(rawUrlTree, source, state, {replaceUrl: true, state }); }, 0);

🔬 Minimal Reproduction

https://stackblitz.com/edit/angular-bupuzn created by @fjauer - just use back/forward after pressing Test button

🔥 Exception or Error





🌍 Your Environment

Angular Version:


Angular CLI: 7.2.1
Node: 10.15.0
OS: win32 x64
Angular: 7.2.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.12.1
@angular-devkit/build-angular     0.12.1
@angular-devkit/build-optimizer   0.12.1
@angular-devkit/build-webpack     0.12.1
@angular-devkit/core              7.2.1
@angular-devkit/schematics        7.2.1
@angular/cdk                      7.2.1
@angular/cli                      7.2.1
@angular/flex-layout              7.0.0-beta.23
@angular/material                 7.2.1
@ngtools/webpack                  7.2.1
@schematics/angular               7.2.1
@schematics/update                0.12.1
rxjs                              6.0.0
typescript                        3.2.2
webpack                           4.23.1

Anything else relevant?

Activity

  1. alecvolo commented on Jan 13, 2019

    @alecvolo
    ContributorAuthor

    The workaround is to catch NavigationStart event and update CurrentNavigation object

    export class AppComponent  {
      constructor(private _router: Router) {
        this._router.events.pipe(
            filter(e =>e instanceof NavigationStart),
            filter((e: NavigationStart)=>e.navigationTrigger == "popstate")
        )
          .subscribe((x: NavigationStart) => {
            this._router.getCurrentNavigation().extras.state={...x.restoredState, navigationId:x.id } ;
          });
      }
      }

    Here is the StackBlitz example https://stackblitz.com/edit/angular-xyrmg1 (Should be run in new window)

  2. added this to the needsTriage milestone on Jan 13, 2019
  3. jasonaden commented on Jan 15, 2019

    @jasonaden
    Contributor

    Thanks for the issue. Is this something you think you can create a PR for? I would like to get it fixed ASAP, but it will be a little while until this can be pulled in.

  4. modified the milestones: needsTriage, Backlog on Jan 15, 2019
  5. added
    open for contributions
    on Jan 15, 2019
  6. alecvolo commented on Jan 19, 2019

    @alecvolo
    ContributorAuthor

    I've created PR #28176

  7. 10 remaining items

  8. angular-automatic-lock-bot commented on Oct 30, 2020

    @angular-automatic-lock-bot

    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.

  9. locked and limited conversation to collaborators on Oct 30, 2020
  10. added a commit that references this issue on Nov 11, 2022
    ea1365d
  11. added 2 commits that reference this issue on Nov 21, 2022
    fa5528f
    1976e37
  12. added a commit that references this issue on Feb 1, 2023
    0b51f65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: routerfreq3: highopen for contributionsrouter: navigation pipestate: confirmedtype: bug/fix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions