Bruce Kellerman
Bruce Kellerman

Reputation: 181

Making base url case insensitive with angular 2 router

I have an application that does not get deployed at the root of our site, it lives at site.com/TRAP_beta and a component route defined like this { path: '', component: TrapFormComponent }. I set the base url tag as <base href="/TRAP_beta/">.

When I navigate to site.com/TRAP_beta all is good with the world and the application works as intended, but if I navigate to site.com/trap_beta or any other cased version of the url, then router cannot find the path and it breaks.

How do I go about making the urls case insensitive to angular 2?

Upvotes: 2

Views: 1989

Answers (2)

Bruce Kellerman
Bruce Kellerman

Reputation: 181

I ended up dynamically making the base element in a script tag that runs before anything else gets loaded.

    var path = location.pathname.substr(1).match(/[a-z_0-1]*/i)[0];
    var base = document.createElement('base');
    base.href = '/' + location.pathname.substr(1).match(/[a-z_0-1]*/i)[0] + '/';
    document.head.appendChild(base);

This seems to be resolving my issue and allowing me to continue to use router.navigate over navigateByUrl.

Upvotes: 1

Rahal Danthanarayana
Rahal Danthanarayana

Reputation: 748

Try to use router.navigateByUrl('/'+'nextValue'); instead of router.navigate(['nextValue']);.Because the instruction will be get based upon the URL provided.

Upvotes: 0

Related Questions