react icon indicating copy to clipboard operation
react copied to clipboard

UISref links work incorrectly with <base> and hashLocationPlugin

Open bisubus opened this issue 7 years ago • 14 comments

For <base href="/"> tag and this router setup that is used on /foo/ page:

const rootState = { name: 'root',
	url: '/',
	component: Root
};

const barState = {
	name: 'bar',
	url: '/bar',
	component: Bar
};

<UIRouter plugins={[hashLocationPlugin]} states={[rootState, barState]}>
	<div>
		<UISref to="bar"><a>bar</a></UISref>
		<UIView/>
	</div>
</UIRouter>

bar link works properly on click (goes to bar state and results in http://localhost:3000/foo/#/bar location) but the link itself is #/bar and works like http://localhost:3000/#/bar when opened in new window (because of how anchors work with <base>).

Currently UISref uses StateService href to generate a link. Since StateService is unaware of <base>, it's understandable why it generates an anchor that doesn't work as expected. Also produces a wrong link when using absolute:

stateService.href('bar', {}), // '#/bar'
stateService.href('bar', {}, { absolute: true }), // 'http://localhost:3000/#/bar'

bisubus avatar Mar 28 '18 17:03 bisubus

I'm not sure how to reproduce this, as I don't understand if you are serving the app under localhost:3000/foo. From the http://localhost:3000/foo/#/bar url it looks like it.

UI-Router is handling the routing in the only after the # portion since you are using the hashLocationPlugin.

Could you set up a Codesandbox to reproduce the error?

elboman avatar Apr 26 '18 14:04 elboman

Yes, the application is served from localhost:3000/foo/. UISref link is http://localhost:3000/#/barif` was specified.

It seems that Codesandbox will have problems serving anything but /index.html

Here is a repo, https://github.com/bisubus/react-uirouter-demo

bisubus avatar Apr 26 '18 19:04 bisubus

I need to experiment a bit and see what's wrong.

Out of curiosity: since you serving from /foo, why isn't base <base href="/foo">?

elboman avatar Apr 28 '18 11:04 elboman

This is expected behaviour for anchors because they respect , but from hash router point of view it's incorrect. In my case this is non-SPA, and all resources are relative to /, wasn't able to rewrite all links to absolute.

bisubus avatar Apr 28 '18 21:04 bisubus

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.

stale[bot] avatar Dec 27 '19 03:12 stale[bot]

AFAIK, the issue is applicable to latest Router version. Does the demo needs to be updated to it?

bisubus avatar Dec 30 '19 12:12 bisubus

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!

stale[bot] avatar Sep 25 '20 23:09 stale[bot]

The issue is still relevant, bot.

bisubus avatar Sep 26 '20 06:09 bisubus

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!

stale[bot] avatar Mar 26 '21 10:03 stale[bot]

The issue is still relevant, bot.

bisubus avatar Mar 26 '21 13:03 bisubus

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!

stale[bot] avatar Sep 22 '21 13:09 stale[bot]

The issue is still relevant, bot.

bisubus avatar Sep 22 '21 19:09 bisubus

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!

stale[bot] avatar Apr 16 '22 01:04 stale[bot]

The issue is still relevant, bot.

bisubus avatar Apr 16 '22 20:04 bisubus

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!

stale[bot] avatar Oct 16 '22 14:10 stale[bot]

This issue has been automatically closed.

stale[bot] avatar Jan 07 '23 20:01 stale[bot]