angularangular-cliangular14

Serving an Angular app from a sub-path with ng serve


I am trying to serve an Angular 14 app via ng serve, but I need it to be served in a sub-path, like for example:

http://localhost:4200/mysubpath

Now, I managed to partially make it work by adding this property to angular.json:

enter image description here

And now the base url in the browser address bar is correct, but the problem is that, as you can see from the following screenshot, the Angular asset files and resources are still being served from the root path:

enter image description here

How can I make it so that the entire Angular application, including assets, is served from a sub-path? I've found various questions similar to this online, but all the answers seem to be deprecated, for example a lot of people suggest passing the --base-href option to ng serve, but that argument seem to be invalid for modern versions of Angular, the ng cli rejects it.


Solution

  • After further testing, it seems the baseHref setting is not enough. You also need to invoke the ng serve command line with the following option:

    ng serve --serve-path /mysubpath/
    

    This seems to work, although it is breaking something in my hosting pipeline but I will research that issue separately.