router icon indicating copy to clipboard operation
router copied to clipboard

Router should get context path when starting up

Open jasoncarreira opened this issue 8 years ago • 10 comments

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.

jasoncarreira avatar May 20 '16 17:05 jasoncarreira

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

EisenbergEffect avatar May 20 '16 17:05 EisenbergEffect

Everything after the domain:

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

jasoncarreira avatar May 20 '16 17:05 jasoncarreira

And how does that relate to the base tag?

EisenbergEffect avatar May 20 '16 17:05 EisenbergEffect

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.

jasoncarreira avatar May 20 '16 17:05 jasoncarreira

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)

nikolaj-a avatar May 20 '16 17:05 nikolaj-a

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?

EisenbergEffect avatar May 20 '16 17:05 EisenbergEffect

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

jasoncarreira avatar May 20 '16 18:05 jasoncarreira

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

EisenbergEffect avatar May 20 '16 18:05 EisenbergEffect

Okay, will do

On May 20, 2016, at 2:25 PM, Rob Eisenberg [email protected] 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

jasoncarreira avatar May 20 '16 18:05 jasoncarreira

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

thomas-darling avatar Jul 30 '16 23:07 thomas-darling