aurelia / router

A powerful client-side router.
MIT License
120 stars 115 forks source link

Router should get context path when starting up #341

Open jasoncarreira opened 8 years ago

jasoncarreira commented 8 years ago

Rather than having to set the base url and base href the router should pick up the context URL when the app starts and use that as the base for the routes.

EisenbergEffect commented 8 years ago

What is the "context url"? I haven't seen that term before. Where can we derive it from?

jasoncarreira commented 8 years ago

Everything after the domain:

http://www.foo.com/some/context/url

EisenbergEffect commented 8 years ago

And how does that relate to the base tag?

jasoncarreira commented 8 years ago

See gitter... Someone was having a very hard time getting it to correctly route under the context.

If the app was running under /context1 rather than going to /context1/route1 it wanted to take them to /route1

In multi-app sites that could be part of a whole other app.

I haven't set up an Aurelia app to run below the root context but I don't see why the router can't handle that on its own.

Here's what he said he had to do to get it working. In the JSP he had to render this into the HTML:

and then when configuring the router:

var base = document.getElementByTagName(“base”)[0].href.replace(location.protocal + “//“ + location.host, “”); config.map([ { route: [base, base + "firstroute"] ... }, { route: [base + "secondroute"] ... }, ]);

None of this should be necessary.

nikolaj-a commented 8 years ago

I have got a workaround for this, but it's very clunky:

In the index file, add a base tag with the context path (be aware that this is JSP templating, NOT Aurelia templating). Don't forget the trailing slash!

<base href="${pageContext.request.contextPath}/" />

That will render out something like:

<base href="/ctxpath/" />

Or if you're at the root of the website:

<base href="/" />

Then in configureRouter(), do the following.

var base = document.getElementByTagName("base")[0].href.replace(location.protocol + "//" + location.host, "");

config.map([
   { route: [base, base + "firstroute"], ... },
   { route: [base + "secondroute"], ... },
   { route: [base + "thirdroute"], ... }
]);

It would be nice if Aurelia picks up this "base" from the base tag automatically, or we can enable it somehow (config.options.useBaseURL = true). Angular does this out of the box, by the way.

As it is now, all the route hrefs will have the context path stripped away.

(Sorry for the cross-posting)

EisenbergEffect commented 8 years ago

We're happy to address this. It sounds like a good contribution that could come from the community. Is anyone interested in putting together a pull request for this?

jasoncarreira commented 8 years ago

I could take a look if you can point me to where I should start. I haven't messed with the router at all.

EisenbergEffect commented 8 years ago

Start by looking at the history-browser library. It's relatively small and important to this issue.

jasoncarreira commented 8 years ago

Okay, will do

On May 20, 2016, at 2:25 PM, Rob Eisenberg notifications@github.com wrote:

Start by looking at the history-browser library. It's relatively small and important to this issue.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/aurelia/router/issues/341#issuecomment-220682453

thomas-darling commented 8 years ago

Be aware that the <base> tag breaks SVG id's, so this should also work without it: https://github.com/aurelia/router/issues/382