6

All my svg's work perfectly in dev mode, as soon as I run `ng build --prod --aot" they are not loading. They all fallback to the png image. I can't figure out how to fix it. I have been on github already, and tried

  • absolute paths
  • relative paths
  • binding vs. interpolation

I'm using angular 4.4.3

and angular/cli 1.5.4

html:

<img
    class="chain"
    src="../assets/svgs/LinkWardenChain.svg"
    alt="Linkwarden chain graphic"
    onerror="this.src='../assets/images/LinkWardenChain.png'"
>

another example:

<img
    class="chain"
    [src]="ethernet1"
    alt="ethernet connected graphic"
>

*I cannot upgrade to angular 5 at the moment, due to 3rd party package support

SOLUTION

Thanks to Mathew for the answer, adding the file paths to the cli assets folder:

"assets": [
        "assets",
        "favicon.ico",
        "./src/assets/svgs/eth-100.svg",
        "./src/assets/svgs/eth-1000.svg",
        ...etc...
      ],
FussinHussin
  • 1,718
  • 4
  • 19
  • 39
  • 1
    Have you tried adding the svg files to the "assets" section in the CLI configuration file? – Reactgular Nov 28 '17 at 00:39
  • they are in the assets folder – FussinHussin Nov 28 '17 at 14:23
  • 2
    You have to manually add them as entries to the "assets" section in the CLI configuration file. Webpack might be moving the images to the bundles and rewriting the URLs. SVG files might not be moved but the URLs are rewritten. I think this might be known issue with that version of Angular. Keep in mind that this is a webpack problem. – Reactgular Nov 28 '17 at 14:30
  • 1
    hmm, alright, i'll give that a shot. it doesn't make sense to me, because the .png files work great, and they are not listed as assets in the cli file – FussinHussin Nov 28 '17 at 14:36
  • thanks @MathewFoscarini ! – FussinHussin Nov 28 '17 at 15:31
  • This didn't work for me unfortunately. What did end up worked for me was changing hosting providers from Google Cloud Storage Buckets to AWS S3 Bucket for my Angular app. And this solution didn't require adding the SVG paths to the assets array in Angular.json. – Gabe Karkanis Nov 03 '22 at 16:58

1 Answers1

0

I had shared svg icons stored in the apps/shared/assets/icons/*. For angular 14 this worked:

in angular.json:

"assets":[
...
    {
      "glob": "**/*",
      "input": "apps/shared/assets/",
      "output": "assets"
    }
]

the path to the icon in html:

src="assets/icons/connection.svg"

and added svg mime type on the server:

mimetype.assign = (
  ".html" => "text/html",
  ".txt" => "text/plain",
  ".jpg" => "image/jpeg",
  ".png" => "image/png",
  ".js" => "text/javascript",
  ".css" => "text/css",
  ".svg" => "image/svg+xml"
)
niio
  • 326
  • 3
  • 15