4

It is a bit of weird situation but we have to structure the two angular app as below.

App1 being the main angular app situated at the root and other angular app is in a subfolder.

All is working well except for the routing in App2 since it is overridden by the routes config in Main app.

I am able to use the command below to build/deploy and appears to be working for App2 (subfolder)

ng build --prod --base-href /subfolder/ --deploy-url /subfolder/

App1 is configured to have unknown routes redirected to a 404 page like below

{ path: '**', redirectTo: '/404' }

The issue with if I refresh the page on App2. it got redirected to the 404 page defined in App1. eg. http://www.example.com/subfolder/route1

How can I tell angular in App1 to ignore anything under subfolder and use its own angular routes in App2?

11
  • We have been trying to achieve this recently. Using a relative base-href didn't work when we navigated in App2 routes. We had to use absolute URL. As for skiping App1 route for a specific route, you could try something like this: stackoverflow.com/a/40421975/4036999 Commented Sep 23, 2021 at 5:41
  • @saravanapriyan Did you guys solve it? or any alternative? Commented Sep 23, 2021 at 5:42
  • Easiest way is to use useHash option for navigation in both apps, if it's not a problem to have hash navigation. Commented Sep 23, 2021 at 5:45
  • 1
    Hoping for a cleaner angular way. Commented Sep 23, 2021 at 5:56
  • 1
    I might have to end up building as App2 as a angular module within App1 Commented Sep 24, 2021 at 4:00

2 Answers 2

1
+50

I'm not sure if this will work or if it is helpful at all. But when using a base href like:

<base href="/subfolder/" />

I had to create a proxy configuration e.g.:

proxy.conf.json

{
  "/subfolder/*": {
    "target": "http://localhost:4200",
    "secure": false,
    "logLevel": "debug",
    "changeOrigin": true,
    "pathRewrite": {
      "^/subfolder": ""
    }
  }
}

And serve it using this proxy configuration:

ng serve --proxy-config proxy.conf.json

In angular.json you can also add proxy configurations:

"build": {
  "builder": "@angular-devkit/build-angular:browser",
  "options": {
    "proxyConfig": "proxy.conf.json"
  }
}
...
"serve": {
  "builder": "@angular-devkit/build-angular:dev-server",
  "options": {
    "proxyConfig": "proxy.conf.json"
  }
}

You can find more information here.

Sign up to request clarification or add additional context in comments.

5 Comments

If this isn't helpful at all, let me know and I'll redact my answer, I just thought I'd share. This was new to me when I ran into it. XD
Interesting. Not sure what proxy config. Will try it out
Reading through angular documentation I don’t think proxy is for this purpose?
Your application isn't going to know it is served on "<base href="/subfolder/" />" by itself though ... I am using it effectively to serve on localhost:4200/subfolder instead of localhost:4200. I think the second application could be on another port and map to the subfolder address. I actually do think this could solve your problem. I would have to ask some1 with more experience to be sure though.
Your exact situation isn't described in the angular documentation, but that doesn't mean it couldn't solve your issue. It's not all that usual to have different applications on the same web address. Either you include it in the application or your application runs elsewhere and you proxy to it. At least that is what I think. I could be wrong. I'll leave my answer for now so someone may add to it, confirm it or rule it out.
0

I had the same problem in one project and handled this issue by using <a href=""></a> instead routerLink for routing between projects.

Another way is using Angular workspace to merge all projects in one.

1 Comment

My issue the 404 routes in app1 overwritten the 404 routes in App2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.