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?

ove
  • 3,092
  • 6
  • 34
  • 51

2 Answers2

1

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.

H3AR7B3A7
  • 4,366
  • 2
  • 14
  • 37
  • 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 – H3AR7B3A7 Sep 25 '21 at 11:35
  • Interesting. Not sure what proxy config. Will try it out – ove Sep 25 '21 at 13:32
  • Reading through angular documentation I don’t think proxy is for this purpose? – ove Sep 25 '21 at 15:35
  • Your application isn't going to know it is served on "" by itself though ... I am using it effectively to serve on http://localhost:4200/subfolder instead of http://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. – H3AR7B3A7 Sep 25 '21 at 16:52
  • 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. – H3AR7B3A7 Sep 25 '21 at 17:04
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.

kian
  • 1,449
  • 2
  • 13
  • 21