angular-ui / ui-router

The de-facto solution to flexible routing with nested views in AngularJS
http://ui-router.github.io/
MIT License
13.56k stars 3.01k forks source link

ui-sref generate wrong URLs after upgrading to 1.0.19 #3728

Closed gserg closed 4 years ago

gserg commented 5 years ago

This issue tracker is for Bug Reports and Feature Requests only.

Please direct requests for help to StackOverflow. See http://bit.ly/UIR-SOF for details.

This is a (check one box):

My version of UI-Router is: 1.0.19

Bug Report

Current Behavior:

I have two links in menu:

<a ui-sref="stateOne({param: 'param11'})">One</a>
<a ui-sref="stateTwo({param: 'param22'})">Two</a>

html5Mode is enabled, <base> is omitted

$locationProvider.html5Mode({
  enabled: true,
  requireBase: false
});

When I open http://localhost/ URLs for all links are calculated correctly:

<a href="http://localhost/state1/param11">One</a>
<a href="http://localhost/state2/param22">Two</a>

But when I open direct URL http://localhost/state1/param11 the links are broken:

<a href="http://localhost/state1/state1/param11">One</a>
<a href="http://localhost/state1/state2/param22">Two</a>

Expected Behavior:

All links have correct URLs when open direct URL of some state

<a href="http://localhost/state1/param11">One</a>
<a href="http://localhost/state2/param22">Two</a>

Link to Plunker that reproduces the issue:

http://plnkr.co/edit/f10MMFiS6G6lxvsboPR3?p=preview

NB! Because this test requires to change URL manually, you need to download code from plunker and run as simple SPA.

There is my config for nginx server:

server {
    listen 80;
    server_name localhost;

    location / {
        root /path/to/demp/app;
        index index.html;
        try_files $uri $uri/ /index.html;
    }
}

(also src="_main.js" must be replaced by src="/_main.js" in index.html)

With ui-router v1.0.18 everything works fine :)

PS maybe this issue is related to https://github.com/angular-ui/ui-router/commit/cd426e5 fix

apitts commented 5 years ago

Confirming the same issue here and that it is resolved by reverting to v1.0.18.

tclift commented 5 years ago

https://github.com/ui-router/core/commit/23742e3 was released with 1.0.20, fixing a regression in 1.0.19 that sounds like the same thing here, but did not fix this issue.

Serving a page from /foo with no base element (but in "HTML5 mode" with an effective base of /) will result in a ui-sref to a state with path /bar going to /foo/bar (instead of the expected /bar).

jamessharp commented 5 years ago

We've seen this too

This is the line that is the culprit: https://github.com/angular-ui/ui-router/commit/cd426e53d879fda9bcde9efa4062fdef4fc0a34a#diff-6978271205a73d1a084ce370967b7b7bR74.

baseHref() {
    return this._baseHref || (this._baseHref = this.$browser.baseHref() || this.$window.location.pathname);
  }

In our case we have a base tag <base href="" />, so the empty string triggers the || test to continue, but I can easily imagine something falsey returned if the base tag isn't there too.

jkytomaki commented 5 years ago

Same issue here. We did not have any baseHref defined and upgarding to 10.0.19/10.0.20 caused a random state's path element to be prepended to the URLs returned by $state.href(). Adding a <base href="/" /> to the html seems to have fixed it for us.

Zadvornyi commented 5 years ago

I have the same problem with with 1.0.20

goliney commented 5 years ago

Adding a <base href="/" /> to the html seems to have fixed it for us.

Worked for me, thanks @jkytomaki !

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

This does not mean that the issue is invalid. Valid issues may be reopened.

Thank you for your contributions.