Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy an angular app so that it works under any path?

this is probably a newbie question, but I couldn't find any answers to it here or on angular documentation. Let's say I have a simple Angular application (the hero editor from the angular.io tutorial for instance) that I want to distribute without yet knowing under which path it will run on my webserver. It could be under:

  • http://host.domain/foo/myapp/
  • http://host.domain/foo/bar/myapp/
  • etc... you get the idea

Put differently, I'd need the generated sources (the index.html and its angular associated js files) to be fully working under any path (I guess that's why relative path are for...) currently my generated sources only work if I put them at the root dir my server, because of the

<base href="/"> 

npm deploy has generated in the index.html probably...I tried naively to change it to

<base href="./">

in order that my browser interpret the path of the included JS files as relative to the index.html. With this, my browser correcly finds the JS files, however the angular app is no longer working (blank page...).
Any idea of what should be the proper "angular-way" of doing that? Thanks for your help in advance!

like image 383
xaxa Avatar asked Jun 20 '17 22:06

xaxa


People also ask

How is angular application deployed?

Basic deployment to a remote serverlink For the simplest deployment, create a production build and copy the output directory to a web server. Copy everything within the output folder ( dist/project-name/ by default) to a folder on the server. Configure the server to redirect requests for missing files to index.html .

Where can I host my angular app?

Because these files are static, you can host them on any web server capable of serving files; such as Node. js , Java, . NET, or any backend such as Firebase, Google Cloud, or App Engine.


1 Answers

Found a solution that's working for me and doesn't look too ugly:

ng build --base-href "./"
like image 163
xaxa Avatar answered Oct 27 '22 08:10

xaxa