Solvedangular cli CSS images with relative path not included in production build
✔️Accepted Answer
This seems to be related with AoT.
Workarounds for now:
- use
ng build --prod --no-aot
- use an absolute path for the image (starting with
/
)
Other Answers:
I can verify this happens:
touch ./src/assets/img.svg
(using svg here because it always loads as a file regardless of size)- add
h1 { background-image: url(../assets/img.svg) }
to./src/app/app.component.css
ng build
- verify
./dist/img.svg
exists and it's referenced in./dist/main.bundle.js
ng build --prod
- verify
./dist/img.[hash].svg
does NOT exist but it's referenced in./dist/main.[hash].bundle.js
Will investigate.
In my case, cause I use base-href './'
the workaround is as follows:
- use
ng build --prod --no-aot --base-href ./
- use a relative path to the images folder (i.e.
../assets/images/img.*
)
Hey guys! I'm new to angular and I'm facing the same problem in my app where the local version works perfectly great whereas the github deployment breaks all the URLs for images. My images are placed in src/assets/xyz.jpg directory and I have tried changing the base href as well as my image url and still the image appears to be broken.
I'm using angular 6. Would be really thankful if I get my issue sorted out.
Thanks in advance.
No news on this issue? I am seeing the same thing on Angular 6. Building on Unix seems to be broken, but strangely, I never have the issue when building on Windows.
Oddly enough, when I reference assets in this fashion:
url("/assets/myimage.jpg")
instead of
url("../../../assets/myimage.jpg")
it works fine locally, and during my unix production build.
OS?
Versions.
Repro steps.
The log given by the failure.
Mention any other details that might be useful.