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

@filipesilva adding --deploy-url /static/ does add /static/ before all *.budled.js files but it doesn't do so with assets. How to solve this problem?

pyaf avatar Sep 29 '17 14:09 pyaf

@rishabhiitbhu it will add it for assets referenced in css, but not for images referenced in html. The tracking issue for that is https://github.com/angular/angular-cli/issues/6666, but discussion on it can be found in https://github.com/angular/angular-cli/issues/4517.

filipesilva avatar Oct 02 '17 10:10 filipesilva

Hey, I am trying to host a static page on github pages. using ng build --prod --base-href "https://kartik1607.github.io/angularHeader/" ngh

All the js and css load up fine but assets give 404.

Angular tried to find assets at https://kartik1607.github.io/assets/wallpaper.jpg instead of https://kartik1607.github.io/angularHeader/assets/wallpaper.jpg

Kartik1607 avatar Oct 28 '17 13:10 Kartik1607

Solved the issue using ng build --prod --base-href "https://kartik1607.github.io/angularHeader/" -d "/angularHeader" command.

Kartik1607 avatar Oct 28 '17 13:10 Kartik1607

Updating the base href works but when I refresh the page, I get 404

simonatorw avatar Nov 06 '17 05:11 simonatorw

@simonatorw Are you getting 404 on bundles generated or on assets? Are you hosting your application inside some sub directory? eg: In my case subdirectory was "/angularHeader"

Kartik1607 avatar Nov 07 '17 15:11 Kartik1607

@filipesilva I am facing similar issue, while running ng build command it is deleting dist folder.

Angular CLI: 1.6.6 Node: 6.11.0 OS: win32 x64 Angular: 5.2.5 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router

@angular/cli: 1.6.6 @angular-devkit/build-optimizer: 0.0.42 @angular-devkit/core: 0.0.29 @angular-devkit/schematics: 0.0.52 @ngtools/json-schema: 1.1.0 @ngtools/webpack: 1.9.6 @schematics/angular: 0.1.17 typescript: 2.5.3 webpack: 3.10.0

the error is...

chunk {inline} inline.bundle.js, inline.bundle.js.map (inline) 5.83 kB [entry] [rendered] chunk {main} main.bundle.js, main.bundle.js.map (main) 303 bytes [initial] [rendered] chunk {polyfills} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 323 bytes [initial] [rendered] chunk {styles} styles.bundle.js, styles.bundle.js.map (styles) 11.4 kB [initial] [rendered]

ERROR in TypeError: Cannot read property 'loadChildren' of undefined at _collectLoadChildren (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler\bundles\compiler.umd.js:29034:20) at listLazyRoutes (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler\bundles\compiler.umd.js:29009:49) at visitLazyRoute (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler\bundles\compiler.umd.js:31176:47) at AotCompiler.listLazyRoutes (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler\bundles\compiler.umd.js:31144:20) at AngularCompilerProgram.listLazyRoutes (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler-cli\src\transformers\program.js:156:30) at Function.NgTools_InternalApi_NG_2.listLazyRoutes (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@angular\compiler-cli\src\ngtools_api.js:44:36) at AngularCompilerPlugin._getLazyRoutesFromNgtools (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@ngtools\webpack\src\angular_compiler_plugin.js:248:66) at Promise.resolve.then.then (C:\Users\ANIMESH\Desktop\notes\ngApp\node_modules@ngtools\webpack\src\angular_compiler_plugin.js:565:50) PS C:\Users\ANIMESH\Desktop\notes\ngApp>

animeshsingh04 avatar Feb 20 '18 14:02 animeshsingh04

developers typically (at least I do for convenience and just to have a tomcat server running with ability to go there and launch any project of convenience from anywhere from home office or mobile phone) develop a project off of tomcat root here is the path C:\TOMCAT\WEBAPPS\ROOT\ngapp

angular-cli bundles and builds out to C:\TOMCAT\WEBAPPS\ROOT\ngapp\dist thats where index.html and all the bundles are located

furthermore an assets dir lives under there too
C:\TOMCAT\WEBAPPS\ROOT\ngappt\dist\assets

I run my tomcat on 8080 http://localhost:8080/ngapp

app runs but paths to assets and bundles are frigged setting <base href=""> results in weird paths to bundles and assets

I am seeing (depending on what we set href to) paths ahead or after resulting in inaccessible paths to bundles and assets.

currently my app runs but cannot retrieve assets no matter what I try

currently

if I set <base href="/ngapp/dist"> my bundles are not found 404

if I set <base href="/ngapp/dist/"> my bundkles are found and app runs but cannot find assets with 404 on all assets

???

MEANSTACKDIGITAL avatar Mar 21 '18 05:03 MEANSTACKDIGITAL

Saved my day.

wangki avatar Apr 27 '18 05:04 wangki

Is there a way to avoid setting the base-href in the ng build? I'm working with a project and the war file can be renamed to whatever. So I don't know what is the real base href. It will be so helpful to avoid it and still load the fonts, because there is no way to know the project name. Thanks

nikuelias avatar May 02 '18 03:05 nikuelias

@filipesilva you're brighter then a brain pie! Thank you so much - Was troubleshooting my Node Express + Angular6 app and for the life of me couldn't figure out why /app references were all routing to my 404 handler. base-href was it! 💯

erikyuzwa avatar May 10 '18 14:05 erikyuzwa

Github!! Code Savers thanks @filipesilva

courageDeveloper avatar Jul 02 '18 13:07 courageDeveloper

@filipesilva your solution saved me. Thanks a lot!!

Ishapanda25 avatar Jul 17 '18 10:07 Ishapanda25

Am getting the same issue of following assets/img/brands/max.png 502 (Bad Gateway)

all the assets folder taking root path. Other files are working as expected. Please help me out.

Versions are: Angular CLI: 6.0.8 Node: 8.11.3 OS: darwin x64 Angular: 6.1.1 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router, service-worker

Package Version

@angular-devkit/architect 0.6.8 @angular-devkit/build-angular 0.6.8 @angular-devkit/build-optimizer 0.6.8 @angular-devkit/core 0.6.8 @angular-devkit/schematics 0.6.8 @angular/.DS_Store @angular/cdk 6.4.2 @angular/cli 6.0.8 @angular/material 6.4.2 @angular/pwa 0.6.8 @ngtools/webpack 6.0.8 @schematics/angular 0.6.8 @schematics/update 0.6.8 rxjs 6.2.2 typescript 2.7.2 webpack 4.8.3

html5ravi avatar Aug 20 '18 10:08 html5ravi

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.

yeah its working but when i refresh a page then through page not found error

sureshkumarcse avatar Jan 31 '19 07:01 sureshkumarcse

For anyone still having issues with assets, you should reference your images etc using a relative path e.g "./assets/image-1.png". This resolved my issue.

tyronboyd avatar Feb 06 '19 22:02 tyronboyd

I can confirm what @tyronboyd said. There are three ways to get this done with both assets and all other files i tried.

Nr 1: Change index.html default <base href="/"> to <base href="./">

Nr 2: You can run: ng build --prod --base-href ./

Nr 3: You can change your build process, found in angular.json, to automate this by adding: "baseHref": "./" To your production configuration.

Obviously you can use fixed paths as well with either of these, but adding this single dot to the default uses relative paths, which I would guess is the default behavior one would expect. No idea why absolute paths are still a thing in something as advanced as Angular, but oh well. :-)

MarcoHeumann avatar Mar 11 '19 01:03 MarcoHeumann

@MarcoHeumann had a similar issue fixed it by changing the index.html default to

was wondering how does the absolute and relative path referencing make the difference. Can you help understand ? Appreciate your help in advance !

nikithaperuka89 avatar Mar 20 '19 19:03 nikithaperuka89

To understand this, I urge you to just look at the error when you don't do the change. It will show whatever file you tried to access in a wrong folder. The base Path seems to be defined "somewhere else" as well and unless you overwrite it, it seems to make zero sense (at least to me).

Basically the absolute path is wrong, like all the time, while making it relative gives you full control over your structure, while also working as expected. We have come to expect relative paths everywhere and that is for a reason, but a reason Angular does not seem to follow.

MarcoHeumann avatar Mar 20 '19 19:03 MarcoHeumann

Hello, I tried all the solutions but to no avail. My Angular and Backend application both are packaged in one jar. So, is that the reason it is not working or something else.?

amitvchaudhary avatar Apr 11 '19 11:04 amitvchaudhary