question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

src binding not finding svg file

See original GitHub issue

Hello there, I’m not sure if this is an issue with the loader or merely a lack of my understanding. It appears that when I attempt to dynamically load an SVG using the v-bind:src approach on the image tag, the loader cannot find it.

Loading SVG from src path

<img svg-inline :src="`../../static/images/icon-${game.name}.svg`" />

If I write it in normally src="../../static/images/icon-basketball.svg" it works.

Loading SVG from build path I’ve also tried this:

<img svg-inline :src="`/images/icon-${game.name}.svg`" />

But it ends up just loading up an image tag with the SVG as it’s src instead of inlining the SVG.

Any ideas?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:32 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
oliverfindlcommented, Jul 17, 2019

Hello,

I will try to explain it one more time as simple as possible.

This Webpack loader only parses static files. That means, it takes all your *.vue files and tries to locate all *.svg sources in image tags in those files. It doesn’t perform any Vue specific logic, because that happens later in browser (or in SSR process).

As for using @ alias, that’s Webpack stuff. It has nothing to do with Vue.

If you have any more questions, feel free to ask.

Thanks

1reaction
oliverfindlcommented, Oct 4, 2019

Hello,

while it is true, that majority of use cases are based on displaying svg image is based on dynamic rules, Webpack runs before any Vue logic is able to execute (excluding SSR, which is minority of use cases). Therefore there is no simple way to know, which svg files should Webpack include into bundle. You can read my last message in this issue for more details and possible solutions.

I’m currently working on Vue plugin, which will target this issue, of course with cost of performance.

For your concerns about usefulness of this loader, currently you have more approaches you can use to inline your svg files:

  • using Webpack require function together with v-html directive, which will increase your build time and bundle size (demo here, read readme first).
  • using different Webpack loader (e.g.: vue-svg-loader), which require to manually import svg files and register them as components before you can use them. This can get pretty messy, if you are using lot of svgs.
  • using Vue plugin (e.g.: vue-simple-svg). I don’t have any experience with it. I’m working on something similar (based on this loader), but more advanced with more features.
  • manually copying svgs into your code, which is very hard to maintain, when you are making changes to svg, that is copied into multiple components.

At time I was writing this plugin, I had to solve situation, where I had to show one of 3 possible svg images about 100+ times (was user configurable). So at that time, it was pretty straightforward to me to do v-if, v-else-if and v-else together with inline sprites. Time showed me, that people want use this loader different way I designed it. Therefore I’m working on Vue plugin version of this loader as I stated above.

If you have any more questions, feel free to ask.

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Img src for svg file doesn't work if property binding is used in ...
I think if you are receiving data asynchronous then your binding should be work. When the value is received, AsyncPipe signals the component ......
Read more >
Using SVG and Vue.js: A complete guide - LogRocket Blog
This is a complete guide to using SVG with Vue, including practical examples and best practices to help you along the way.
Read more >
Property binding - Angular
To bind the src property of an <img> element to a component's property, place src in square brackets followed by an equal sign...
Read more >
How to Embed, Inline, and Reference SVG Files in Svelte
A guide on using SVG files with Svelte, and how to embed, inline-directly, or reference them, with or without rollup plugins.
Read more >
xlink:href - SVG: Scalable Vector Graphics - MDN Web Docs
The target element must be part of the current SVG document fragment. ... If the xlink:href attribute is not provided, the target element ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found