intercom / ember-href-to

:link: A lightweight alternative to {{link-to}}
Apache License 2.0
190 stars 42 forks source link

BaseURL other than / makes troubles #19

Open tschoartschi opened 9 years ago

tschoartschi commented 9 years ago

@GavinJoyce we host our ember app in a subdirectory called /app, when I use Ember-Href-To the links does not work, because Ember-Href-To resolves the links to /app/app. We are using Ember-CLI and our environment.js looks as follows:

module.exports = function (environment) {
    var ENV = {
        environment: environment,
        baseURL: '/app/'
        // ...
        // ...
    }
    return ENV;
}

I solved it by making my own helper based on Ember-Href-To. The helper looks as follows:

import Ember from 'ember';
import { hrefTo } from 'ember-href-to/helpers/href-to';

export function hrefToX(params/*, hash*/) {
    var url = hrefTo(params);
    var baseURL = APPENV.baseURL.replace(/\/$/, ''); // APPENV is globally available... jeah this is a relict from our old code from ember-cli 0.0.39
    url = url.replace(baseURL, '');
    return url;
}

export default Ember.Helper.helper(hrefToX);

It works but if I hover the links built with hrefToX the links dont contain the /app/. This does not happen with link-to. Is my solution the right way to go or is there a better way to do it?

Thanks a lot

GavinJoyce commented 9 years ago

We use the rootURL property on the Router, href-to is designed to work with this:

var Router = Ember.Router.extend({
  rootURL: '/a'
});

Perhaps one of these mechanisms is a legacy way of setting the root URL?

tschoartschi commented 8 years ago

@GavinJoyce at first of all, thanks for your effort and ember-href-to.

I'm not sure if we are using a legacy way of setting the root URL. I just started a new ember-cli project yesterday (with ember cli version 1.13.13) and in the environment.js there is still an entry for baseURL. If I add the rootURL to the router I have to remove the baseURL from the environment.js, I'm not totally sure if this is fine or if I break something from ember-cli if I remove the baseURL from the environment.js. Do you have any insights on this?

GavinJoyce commented 8 years ago

Perhaps you could create a simple ember twiddle which demonstrates the problem? This would allow me to make a suggestion or a fix much faster

tschoartschi commented 8 years ago

@GavinJoyce okay, I'll try to figure it out how I can setup an Ember CLI project with an environment.js as an ember twiddle. At the first glance I didn't see how to do this. I'll check it out later in more depth.

tschoartschi commented 8 years ago

I tried to reproduce the issue but it is not possible. Maybe I have to try harder. I'll let you know when I have a sample which reproduces the bug. In the meanwhile thanks a lot for your effort

nanuxbe commented 8 years ago

I can confirm that I have the same issue using baseURL.

I don't think baseURL is legacy as it is still the "official" way of ember-cli guides

rootURL doesn't seem to be part of the "Routing Introduction" section of Ember Guides since v2.0.0 though but might be somewhere else.

nanuxbe commented 8 years ago

I also have to add that this does only happen with sub-routes:

And finally {{link-to}} doesn't have the same problem (if it can be of any help)

I don't think it's possible to create an ember-cli twiddle for this but I'll try to find some time this weekend to create a small app which reproduces the problem

GavinJoyce commented 8 years ago

Thanks, a sample application which demonstrates the problem will allow me to address the issue much more rapidly

tschoartschi commented 8 years ago

@GavinJoyce: It was quite hard to reproduce the bug on a fresh ember-cli project. The problem arises when you add a fallback route to the router. What we have in our project is: this.route('error404', {path: '/*path'}); and this seems to cause problems in conjunction with setting a base url in environment.js.

I created a github repo because I don't know how to outline this problem in an ember-fiddle. Hope this is ok?

Basically you will see a simple ember-cli app which has 3 pages, index/home, about and contact. Then there is the navigation once with embers link-to helper, once with ember-href-to and once with the workaround which I posted when I opened the issue. But my workaround does not produce the same values for href as embers link-to. When you hover the links you will notice that the link-to links still have the base url in it and the links from my workaround does not.

Hope this helps finding the problem. I'm not quite sure if it is a bug with ember-href-to or if it is a problem how the router is used?

GavinJoyce commented 8 years ago

@tschoartschi thanks for the repo.

I can see that the rendered URLs are correct:

image

It looks like _getNormalisedRootUrl will need to be updated to handle this case: https://github.com/intercom/ember-href-to/blob/master/addon/helpers/href-to.js#L6

tschoartschi commented 8 years ago

I cross-checked it and I think you are right. The _getNormalisedRootUrl seems to cause the troubles. Do you know how to solve it? I could assist you if you tell me the way to go :wink:

rwjblue commented 8 years ago

baseURL is going to die 🔥 🔥 🔥 🔥 🔥 🔥 . See https://github.com/ember-cli/ember-cli/pull/5792 and a blog post we are working on in https://github.com/emberjs/website/pull/2569.

nathanhammond commented 6 years ago

baseURL is dead as of 3.0. Nominate for closure.