Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HistoryLocation doesn't use baseURL correctly #5334

Closed
jamesarosen opened this issue Aug 6, 2014 · 17 comments
Closed

HistoryLocation doesn't use baseURL correctly #5334

jamesarosen opened this issue Aug 6, 2014 · 17 comments

Comments

@jamesarosen
Copy link

Ember.HistoryLocation sets baseURL to jQuery('base').attr('href') || ''. That means that baseURL is something like https://mysite.com/root/.

That's totally reasonable, except HistoryLocation#getURL uses baseURL as if it were just a path. That is, it calls (more or less) location.pathname.replace(baseURL, ''). location.pathname will never include the protocol and domain, so that replace will never do anything.

I believe the correct thing to do would be to extract rootURL from baseURL on initialization:

rootURL: null,

init: function() {
  var location = get(this, 'location') || window.location;
  set(this, 'location', location);

  var baseURL = get(this, 'baseURL') || jQuery('base').attr('href') || '';
  set(this, 'baseURL', baseURL);

  var origin = location.origin ||
               "%@//%@%@".fmt(location.protocol, location.hostname, (location.port ? ':' + location.port : ''));
  var rootURL = get(this, 'rootURL') ||
                baseURL.replace(origin, '');
  set(this, 'rootURL', rootURL);
},

Then, only use rootURL in getURL:

getURL: function() {
  var location = get(this, 'location'),
      path = location.pathname,
      baseURL = get(this, 'baseURL');

  baseURL = baseURL.replace(/\/$/, '');
  var url = path.replace(rootURL, '');

  return url;
},
@jamesarosen
Copy link
Author

Ah, I think I see the confusion. HistoryLocation is tested with a baseURL of /base/. That's certainly a valid value for the href attribute of the <base> tag.

But it's perfectly sensible for a page to use <base href="https://example.com/base/" />.

And if you ask for $('base').prop('href') (instead of 'attr') or for window.document.baseURI, you always get a full URL with protocol, host, and (if supplied), port.

For example, on this page:

$('<base>').attr('href', '/agent/').prop('href'); // "https://github.com/agent/"

@wagenet wagenet added the bug label Aug 6, 2014
@jamesarosen
Copy link
Author

rootURL is also used as a path in formatURL:

formatURL: function(url) {
  // ...
  return baseURL + rootURL + url;
},

That suggests that this might be better called rootPath or, alternatively, that we calculate a single baseURI and throw out rootURL and baseURL altogether.

@jayphelps
Copy link
Contributor

FWIW I agree that rootURL should be renamed rootPath as it's used as such all over ember.

@jamesotron Have you started patching this baseURL issue somewhere? Don't wanna double-effort.

@jamesarosen
Copy link
Author

@jayphelps I suspect you meant jamesarosen, not jamesotron.

I haven't started any code yet. Here are some ideas:

  1. eliminate rootURL and baseURL; replace them both with a single baseURI (matching document.baseURI)
  2. eliminate rootURL; change baseURL to be a full URL, not just a path
  3. change rootURL to rootPath; continue to append it to baseURL
  4. change rootURL to rootPath; change baseURL to origin (matching window.location.origin); extract both from document.baseURI by default

In any of those cases, we can add deprecation warnings to the existing properties and have them modify the new properties as needed. Basically: the transition isn't hard once we know where we're going.

@jayphelps
Copy link
Contributor

@jamesarosen WHOOPS! I did indeed mean you.

Sorry @jamesotron. Carry on.

@jimsynz
Copy link
Contributor

jimsynz commented Aug 13, 2014

ha! will do.

@jayphelps
Copy link
Contributor

@jamesarosen I'm neck deep in the location classes and router again so I'm gonna try to squash this issue. Any additional thoughts or revelations since we last discussed or should I plow ahead?

@jamesarosen
Copy link
Author

We experimented with a number of options. I think my (4) above is closest to what we ended up with.

IE doesn't have document.baseURI, so you will still have to use $('base').first().attr('href') or something to get it.

@wagenet
Copy link
Member

wagenet commented Nov 1, 2014

@jayphelps status?

@rwjblue
Copy link
Member

rwjblue commented Aug 9, 2015

@jayphelps / @jamesarosen - Is this still an issue? Any chance one of you would try whipping up a PR?

@jayphelps
Copy link
Contributor

@rwjblue I don't have the cycles to dig in right now 😞

@jamesarosen
Copy link
Author

Yes, I can take this on. Our next sprint starts next Monday. I can try to slate it in there.

@jamesarosen
Copy link
Author

OK, so my plan is

AutoLocation

  • add rootPath
  • change rootURL to return rootPath and emit a deprecation warning
  • change the rest to use rootPath instead of rootURL

HistoryLocation

  • keep baseURL, but use .prop('.href') so it's always a full URL
  • add rootPath
  • change rootURL to return rootPath and emit a deprecation warning
  • change the rest to use rootPath instead of rootURL

Tests

Not quite sure yet.

@sandstrom
Copy link
Contributor

@jamesarosen this would be a great addition, happy when I saw a fix is in the works! ⛵

@locks
Copy link
Contributor

locks commented Apr 19, 2016

@jamesarosen anything happening here?
cc @rwjblue

@jamesarosen
Copy link
Author

I had a PR, but it lingered and was closed. I stopped working on the app that this was a problem for about a year and a half ago.

@locks
Copy link
Contributor

locks commented Apr 22, 2016

@jamesarosen thanks for the work! I'm closing this issue, sorry the PR didn't get merged at the time.

@locks locks closed this as completed Apr 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants