Skip to content

Bug: Child Routers on Root Components with path '/' must throw pushstate error #5502

Closed

Description

@AlexBachmann

If you have your RootRouter in your app.ts configured like this, where the path '/' redirects to a child router:


@RouteConfig([
  {path: '/...', loader: () => System.import('./components/index/index').then(m => m.IndexComponent), as: 'Index'}
])

and your Child Component (in this case the IndexComponent) configured like this:


@RouteConfig([
  {path: '/', loader: () => System.import('./components/home/index').then(m => m.HomeComponent), as: 'Home'},
  {path: '/listing', loader: () => System.import('./components/listing/index').then(m => m.ListingComponent), as: 'Listing'},
  {path: '/detail', loader: () => System.import('./components/detail/index').then(m => m.DetailComponent), as: 'Detail'}
])

Then your links in the IndexComponent's template


Home
    Listing
    Detail

will always throw the following error:


EXCEPTION: SecurityError: Failed to execute 'pushState' on 'History': A history state object with URL 'http://listing/' cannot be created in a document with origin

This is because of the double slashes in front of the href-link the angular2 router translates the route to:

Why is it doing that?
In the RootRouter.commit method, it automatically prefixes a slash to the emitPath, because it assumes that the emitPath cannot possible already have an leading '/':


if (emitPath.length > 0) {
      emitPath = '/' + emitPath;
}

However, when the path to the ChildComponent is only '/', this is actually possible.
emitPath is defined as:


var emitPath = stringifyInstructionPath(instruction);

and the stringifyInstructionPath method is defined as:


export function stringifyInstructionPath(instruction: Instruction): string {
  return instruction.component.urlPath + stringifyAux(instruction) +
         stringifyPrimaryPrefixed(instruction.child);
}

where in our use case instruction.component.urlPath is empty, stringifyAux(instruction) is empty and stringifyPrimaryPrefixed has a leading '/'

I propose to fix this bug, by checking in the RootRouter.commit method, whether the emitPath has a leading slash, and only if this is not the case, it adds another one.

Activity

  1. brandonroberts commented on Dec 2, 2015

    @brandonroberts
    Contributor

    See if this helps #5218

  2. oubushixb commented on Dec 16, 2015

    @oubushixb

    same question, replace routeConfig '/...' to '/anything/...' it will work

  3. matteosuppo commented on Dec 31, 2015

    @matteosuppo
    Contributor

    I created a repository that can reproduce the bug: https://github.com/matteosuppo/angular2-routing-issue

    I tried to create a test case in the angular source but I couldn't make it fail.

  4. chrisgreeff commented on Jan 7, 2016

    @chrisgreeff

    +1

  5. user414 commented on Jan 17, 2016

    @user414

    I can still reproduce this in the beta1, but solve it by using @oubushixb suggestion with a redirect

      { path: "/", as: "Main", redirectTo: ["/MainScreenApp"], useAsDefault: true},
      { path: "/main/...",      as: "MainScreenApp",      component: MainScreenBootstrapComponent },
    
  6. AlexBachmann commented on Jan 27, 2016

    @AlexBachmann
    Author

    I reproduced this bug in this Plunk
    http://plnkr.co/edit/OjXZK9

    You will not get errors thrown displayed in the console while running in Plunkr so here is the exact same code on our servers
    http://test.loop-one.de/

    The error still exists

  7. angular-automatic-lock-bot commented on Sep 8, 2019

    @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.

  8. locked and limited conversation to collaborators on Sep 8, 2019
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions