angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Incorrect JS paths when ng build -prod is deployed in a subfolder

Open jarben opened this issue 10 years ago • 43 comments

Please provide us with the following information:

  1. OS?
    Windows 10
  1. Versions: angular-cli: 1.0.0-beta.5 node: 4.4.5 os: win32 x64
  2. Repro steps:
  • Fresh app created by "ng new datamatic"
  • Then build "ng build prod"
  • /dist folder deployed to http://datamatic.co/angular2-test/
  1. The log given by the failure: Browser log when accessing http://datamatic.co/angular2-test/: image

Files are loaded relative to the domain, not the folder

jarben avatar Jun 14 '16 14:06 jarben

When you're deploying to non-root path within a domain, you'll need to manually update the <base href="/"> tag in your dist/index.html.

In this case, you will need to update to <base href="/angular2-test/"> I believe.

filipesilva avatar Jun 17 '16 00:06 filipesilva

Thanks for your answer @filipesilva , that fixes the issue! Sorry I didn't notice the base tag.

jarben avatar Jun 17 '16 08:06 jarben

Don't be sorry, this is a common scenario and not so easy to identify. Next time someone has the same problem they will find your issue :)

filipesilva avatar Jun 17 '16 15:06 filipesilva

It worked for me to deploy in my production server, but after change, 'ng serve' doesn't load application anymore either in http://localhost:4200/myfolder or in http://localhost:4200 paths.

Is there any way of making this change automatically depending on prod/dev execution mode?

Thanks in advance!

JavierFuentes avatar Oct 22 '16 07:10 JavierFuentes

@JavierFuentes it doesn't make sense to use --base-href on ng serve the test server will serve in a root path.

filipesilva avatar Oct 27 '16 16:10 filipesilva

@filipesilva You give me a clue... ng build -prod --base-href \"/mysubfolder/\" works in my server without change original <base href="/"> in my index.html for continue developing.

You can view the result here: http://foobar.cf/calculator

Thanks a lot!

JavierFuentes avatar Oct 27 '16 17:10 JavierFuentes

@filipesilva I think @JavierFuentes idea of hosting the app while development within another subdirectory makes sense in a specific context. See my new issue #3010 therefore. I'd appreciate your opinion on it.

manuelfink avatar Nov 03 '16 23:11 manuelfink

Hi ,

I have a similar issue. I have deployed Angular 2 inside War file in tomcat. I have changed base href to "/AngularSpring" in index.html.

http://localhost:8080/AngularSpring/---- . Application URL.

The application is loading fine but resources which are inside asset folder gives 404. http://localhost:8080/assets/env.json 404. Following line is giving issue at runtime this.getConfigFile('./assets/env.json'); Is there any way which automatically appends ./AngularSpring before the './assets/env.json as inside war file asset folder in inside /AngularSpring.

please help I have N number of the solution but none of them worked. I don't want to change the path in the file manually.

chandresh87 avatar Mar 23 '17 22:03 chandresh87

I am having same issue, localhost:8080/ --works but localhost/myfolder -- doesnt work tried ng build -prod --base-href /myfolder /\ but doesnt help, Any light please? Appreciate your response in advance. Frank

JagFrank avatar Mar 24 '17 10:03 JagFrank

try ng build --prod --aot=false -bh /myfolder -d /myfolder

chandresh87 avatar Mar 24 '17 10:03 chandresh87

@@Thanks Chandresh! Above command suggested by Chandresh87 , changes the base ref in index.html to base href="/myfolder" also tried ng build -prod --base-href /myfolder does the same, but no joy Any reason why I am unable to access from contecxt path localhost:8090/myfolder -- doesnt work Where as localhost:8090 does work, PS: I am using tomcat8 ~(apache-tomcat-8.0.27) incase it helps or makes difference Thanks

JagFrank avatar Mar 24 '17 10:03 JagFrank

are you setting welcome file list as index.html in web.xml ?

chandresh87 avatar Mar 24 '17 11:03 chandresh87

Thanks, that worked, but the Spring security is blocking it, tried adding .antMatchers("/**").permitAll() still it doesnt allow angular to load the root page

JagFrank avatar Mar 28 '17 13:03 JagFrank

don't give the root to pass through the spring controller. map your java controller to somethings else like rest-dispatcher /JagFrank/ So the url for java controller will be localhost:8080/context/JagFrank/

chandresh87 avatar Mar 28 '17 13:03 chandresh87

@filipesilva , Hi, I also had the same issue and I fixed it by manually changing the base root for my server. It works fine unless or until I refresh or reload the page..

Its showing me the 404 error. How to solve this issue?

Any other way for solution? Please share.

Thanks

devsubhajit avatar May 18 '17 06:05 devsubhajit

@shaadh you need to configure your server to fallback to index.html, see https://angular.io/docs/ts/latest/guide/deployment.html#!#fallback for details.

filipesilva avatar May 18 '17 09:05 filipesilva

Having the same issue as @chandresh87 - ng build --prod --base-href "/mypath/" allows the app to load, but still 'assets' are unable to load because they are looked for in root. But --aot=false and -d didn't help me. What's the best practice here? surely I'm not expected to rewrite all urls to match the production setup.

itaysk avatar Jun 04 '17 13:06 itaysk

@itaysk you'll have to set --public-url "/mypath/" as well for that case AFAIK.

filipesilva avatar Jun 12 '17 09:06 filipesilva

@filipesilva there's no --public-url flag in ng build.. what did you mean?

itaysk avatar Jun 12 '17 17:06 itaysk

@itaysk apologies, it's --deploy-url.

filipesilva avatar Jun 21 '17 08:06 filipesilva