Skip to content

fix(upgrade): need to take base uri into account in setUpLocationSync - #20244

Closed
observstream wants to merge 5 commits into
angular:masterfrom
observstream:base-href-duplication
Closed

observstream wants to merge 5 commits into
angular:masterfrom
observstream:base-href-duplication

Conversation

@observstream

Copy link
Copy Markdown
Contributor

The url.pathname passes the base uri as part of the url instead of removing it

Fixes #20061

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] angular.io application / infrastructure changes
[ ] Other... Please describe:

What is the current behavior?

I am modifying the setUpLocationSync in packages/router/upgrade/src/upgrade.ts to take the base tag into account when navigating by url

Issue Number: 20061

What is the new behavior?

The base tag is removed from the pathname before the router navigates to the specified url. This stops an infinite loop from occurring when navigating between Angular and AngularJS.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

@observstream

Copy link
Copy Markdown
Contributor Author

I had one question. This function isn't being tested anywhere in Angular that I can find. I am not sure if I should make an entire test file and write new tests for all of the functionality.

@zbuhler

zbuhler commented Nov 8, 2017

Copy link
Copy Markdown

The logic on this is pretty sound but there is a problem with the commit as is. If pulled in as is you will still get the infinite redirect.

Until the url.href is set (like it is on url.href = next;) url.protocol and url.host will not be set properly (":" and "" respectively). The logic for setting the baseHref most likely needs to be put in the $locationChangeStart AFTER the url.href = next;.

@observstream

Copy link
Copy Markdown
Contributor Author

I have submitted a fix for the issues listed. Please recheck to see if this fixes this bug.

@gkalpak gkalpak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for looking into this 👍
I left a couple of comments. And yes, we should definitely have tests for this kind of stuff 😕

Ideally, we should have both unit and integration tests. But either is better than nothing 😁
Feel free to add a new spec file with tests  ❤️

Comment thread packages/router/upgrade/src/upgrade.ts Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation change causes linting to fail.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at this! I just got back from a trip and will push a fix for this over the weekend.

Comment thread packages/router/upgrade/src/upgrade.ts Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the general direction (stripping baseHref from the URL) is correct, but I think it would be better to use something like Location#normalize() for that. This ensures that there is no unnecessary duplication (of code and runtime overhead) and provides a more robust implementation (for examples it takes APP_BASE_HREF into account if provided).

WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a much better solution. I will make those changes and write unit tests for them. They should be up either this weekend or early next week.

@gkalpak

gkalpak commented Dec 15, 2017

Copy link
Copy Markdown
Member

@jahumes, any update on this? Will you find time to work on this in the near future or should someone else take it on?

@observstream

Copy link
Copy Markdown
Contributor Author

I have done what you have requested with the change and that helped greatly make the code much smaller and normalized.

I have been having a hard time with the writing of tests (which is why this has taken so long to push). I seem to be unable to get my tests to run at all. When I create a new file (packages/router/test/upgrade.spec.ts), none of the tests I create in there are run during ./test.sh node. Am I required to run the build process after I make my changes before my tests start running?

@gkalpak

gkalpak commented Jan 23, 2018

Copy link
Copy Markdown
Member

I think the tests inside packages/router are run with ./test.sh router (or ./test.sh browser, but ./test.sh router should suffice).

If you need ngUpgrade files in your tests, you will also need to update router/karma.conf.js to include them (but you might not need them at all).

If you find any difficulties (or if you don't have time to work on this atm and want someone else to take it over), just let me know 😉

@ngbot ngbot Bot added this to the Backlog milestone Jan 23, 2018
@ocombe ocombe removed this from the Backlog milestone Jan 23, 2018
@observstream

observstream commented Jan 24, 2018 •

Copy link
Copy Markdown
Contributor Author

Thanks @gkalpak! I will look at this and if I can't get to it by next week, I will let you know.

@mafredri

Copy link
Copy Markdown

It would be awesome to get this merged, I've just spent two days hunting down this bug :-/. Unfortunately I only found this issue after I traced the bug and solved the issue.

Anything I can do to help so that others can benefit?

@gkalpak

gkalpak commented Feb 25, 2018

Copy link
Copy Markdown
Member

@mafredi, currently this PR is missing tests. If @jahumes doesn't have time to work on this atm, perhaps you could give it a shot (adding tests)?

@observstream

observstream commented Feb 27, 2018 •

Copy link
Copy Markdown
Contributor Author

@mafredri @gkalpak I have finally had the time to figure out how create a new test file. All my issues were around including a new module (@angular/upgrade/static) in my test file. I have it working and should be sending a PR today.

@observstream

Copy link
Copy Markdown
Contributor Author

@gkalpak Do these tests work? Is there anything else you would like me to do?

@gkalpak gkalpak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor questions/suggestions. Looks pretty good overall 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change really necessary? AFAICT, @angular/upgrade/static is not used in @angular/router/testing, is it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a mistake from all the testing I had to do to figure out how to get the tests to run successfully

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change really necessary? AFAICT, @angular/upgrade/static is not used in @angular/router/testing, is it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the above issue.

Comment thread packages/router/karma.conf.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? What is requesting stuff from upgrade/static/src/...?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test file upgrade.spec.ts requests this. I will review with the below request and see if I can get it to work.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reviewing this, this is required because the spec file needs the UpgradeModule for the test bed.
See line 31 of upgrade.spec.ts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UpgradeModule is imported from @angular/upgrade/static. Why isn't dist/all/@angular/upgrade/static/*.js enough?

@observstream observstream Feb 28, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, without both of these lines, Karma can't find all of necessary files. In karma-test-shim.js, we are linking the module @angular/upgrade/static to the index.js files which includes file from the src directory. (See here)

If I remove either one of them, I get the following error:
ERROR: Error{__zone_symbol__currentTask: ZoneTask{_zone: Zone{_properties: ..., _parent: ..., _name: ..., _zoneDelegate: ...}, runCount: 0, _zoneDelegates: null, _state: 'notScheduled', type: 'microTask', source: 'Promise.then', data: undefined, scheduleFn: undefined, cancelFn: null, callback: function () { ... }, invoke: function () { ... }}}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the new tests run under bazel test //... ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping! we ought to make sure any added tests are being run by circleci

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'll make sure tests are run and then mark for merge.

Comment thread packages/router/test/upgrade.spec.ts Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file should be in packages/router/upgrade/test/.... From what I see in other similar packages, (e.g. packages/animations/browser/), test/ should be next to the corresponding src/.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which means, you should also update this list.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I will make this change.

Comment thread packages/router/test/upgrade.spec.ts Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is redundant (since this is implicitly verified by the test below).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will remove it.

Comment thread packages/router/test/upgrade.spec.ts Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Since these three vars ($injector, injector, callback) are not necessary for most tests, I think it would make the tests cleaner if you removed them from here (and in beforeEach()) and get them only in the tests that actually use them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy change to make.

Comment thread packages/router/test/upgrade.spec.ts Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Since callback is only needed it one test, this could be simplified as:

get: jasmine.createSpy('$injector.get').and.returnValue({'$on': () => undefined})

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Easy change to make.

Comment thread packages/router/test/upgrade.spec.ts Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems redundant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you talking about spying on the injector.get? I am doing this primarily to isolate the test from any side effects. I will try removing it to see if I can get away with it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing should be calling injector.get (and even if it did, we get a new injector for each test anyway).

Comment thread packages/router/test/upgrade.spec.ts Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two assertions are redundant imo. For example, you can implicitly test this, by changing the $injector.get spy to:

$injector.get.and.callFake((name: string) => (name === '$rootScope') && $rootScope)

(Keeping the tests focused on what matters most, makes them easier to follow.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made this change.

@observstream

Copy link
Copy Markdown
Contributor Author

@gkalpak Thanks for the feedback! I will revisit and send you responses and fixes shortly.

@observstream

Copy link
Copy Markdown
Contributor Author

@gkalpak I have made the changes you have requested. Please review and let me know if there is anything I have missed or still need to change.

Comment thread karma-js.conf.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is wrong. We do want files from .../@angular/upgrade/test.
It should be .../@angular/router/upgrade/test/**. Or maybe you could change the above line to .../@angular/router/**/test/**.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So sorry! I will make this change.

Comment thread packages/router/karma.conf.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UpgradeModule is imported from @angular/upgrade/static. Why isn't dist/all/@angular/upgrade/static/*.js enough?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this. $injector.get is already a spy 😃

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I had to modify line 31 above to not use a fat arrow function and that allowed me to remove it.

Comment thread packages/router/test/upgrade.spec.ts Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing should be calling injector.get (and even if it did, we get a new injector for each test anyway).

@gkalpak gkalpak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for the quick turnaround, @jahumes 👍
Almost there 😃

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I would simplify this test like this (but it is totally optional 😁):

const $rootScope = jasmine.createSpyObj('$rootScope', ['$on']);
upgradeModule.$injector.get.and.returnValue($rootScope);
LocationMock.normalize.and.returnValue(pathname);

setUpLocationSync(upgradeModule);

const url = 'https://google.com';
const pathname = '/custom/route';
const query = '?query=1&query2=3';
const hash = '#new/hash';
const callback = $rootScope.$on.calls.argsFor(0)[1];

callback({}, url + pathname + query + hash, '');

expect(LocationMock.normalize).toHaveBeenCalledTimes(1);
expect(LocationMock.normalize).toHaveBeenCalledWith(pathname);

expect(RouterMock.navigateByUrl).toHaveBeenCalledTimes(1);
expect(RouterMock.navigateByUrl).toHaveBeenCalledWith(pathname + query + hash);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I am always happy to see new ways to do things. I wasn't aware about the argsFor functionality. That makes it much easier.

@observstream

Copy link
Copy Markdown
Contributor Author

@gkalpak I have pushed another set of small changes based on your advice.

@mafredri

mafredri commented Mar 1, 2018 •

Copy link
Copy Markdown

Thanks for your efforts @jahumes and @gkalpak, excited to see this moving along.

My co-worker just noticed that this does not work in IE 11. The problem lies in using a.href and relying on the output.

The url.pathname does not have a leading slash, which prevents Location from normalizing the URL:

  • Chrome: /base/user/1
  • IE 11: base/user/1

In IE, base does not match /base which prevents the normalization.

I'm thinking something like:

let path = url.pathname;
if (!path.startsWith('/')) {
  path = `/${path}`;
}
path = location.normalize(path);

Would that cause any other problems, can we assume there always should be a leading slash? This fix works for us though.

@alexeagle

Copy link
Copy Markdown
Contributor

@gregmagolan could you help them green this up?

@gkalpak
gkalpak force-pushed the base-href-duplication branch from a8986c1 to 2f07677 Compare August 6, 2018 10:14
@gkalpak
gkalpak force-pushed the base-href-duplication branch from 2f07677 to d2af25f Compare August 6, 2018 10:20
@gkalpak

gkalpak commented Aug 6, 2018 •

Copy link
Copy Markdown
Member

It should be green now. We just need another bazel approval (basically for d2af25), @alexeagle 🙏

@gkalpak gkalpak assigned alexeagle and unassigned jasonaden Aug 6, 2018
@gkalpak
gkalpak requested a review from alexeagle August 6, 2018 14:17
@alexeagle alexeagle added cla: yes and removed cla: no labels Aug 6, 2018
@googlebot

Copy link
Copy Markdown

A Googler has manually verified that the CLAs look good.

(Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.)

@alexeagle alexeagle removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Aug 6, 2018
@gkalpak gkalpak added the action: merge The PR is ready for merge by the caretaker label Aug 6, 2018
@kara kara closed this in ba1e25f Aug 6, 2018
kara pushed a commit that referenced this pull request Aug 6, 2018
)

Normalize the full URL (including the base uri) before passing it to
`router.navigateByUrl()`.

Fixes #20061

PR Close #20244
@observstream
observstream deleted the base-href-duplication branch August 6, 2018 19:13
@angular-automatic-lock-bot

Copy link
Copy Markdown

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 Sep 13, 2019
adhambaouz pushed a commit to adhambaouz/angular that referenced this pull request Nov 20, 2026
…ular#20244)

Normalize the full URL (including the base uri) before passing it to
`router.navigateByUrl()`.

Fixes angular#20061

PR Close angular#20244
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: router cla: yes effort1: hours freq1: low target: patch This PR is targeted for the next patch release type: bug/fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Changing baseHref in Angular along with the Upgrade Module to work with AngularJS router causes infinite redirect