-
Notifications
You must be signed in to change notification settings - Fork 72
Reduce repeated SVG size with <use> tag #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
👋 thanks for opening this issue, @ryanb. ✨
Do you have a (simplified) example? I would like to understand how the list of items is constructed, the structure of the SVG and the surrounding HTML document if possible. 🤔
I'm not 100% sure if this fits the scope of |
@jamesmartin Sure, something along these lines:
Here the content of the It's possible to reference part of another SVG using an id like this:
This way the SVG content is outside of the repeating content. This is similar to the first attempt taken by GitHub mentioned in this blog post. Looks like they had cross-domain issue preventing them from doing this, but many don't have that issue. Perhaps it's not worth the hassle of referencing external SVG icons since most of them are small. Just an idea. :) |
@ryanb thanks for the examples, that does seem like a useful feature, especially if there is no cross-domain problem in your use-case. Based on your second example, which is no doubt simplified for clarity, how do you imagine the <ul>
<%= (inline_svg_tag "handle.svg", with_use_tag: 100.times) do |item| %>
<li><svg><use href="#handle-icon" /></svg> <%= item %></li>
<% end %>
</ul> This assumes the caller would assemble their own slim SVGs in the block, with the relevant |
@jamesmartin good question. In my example I included the svg on the same page but I think it would be better done through a separate URL which could join multiple svgs together, perhaps join everything within a directory similar to how a sprite library works.
Behind the scenes this would generate an
Then you could do
The I understand if this is outside the scope of Update: Looks like there are a number of solutions already out there to help with this. |
@ryanb thanks for the extra detail, I understand what you're getting at now.
|
I'm interested in this also, although I see the API as an "inline" solution (not assets pipeline). Here's what I'm thinking.
module Helpers
def inline_svg_tag(filename, transform_params={})
with_asset_finder(InlineSvg.configuration.asset_finder) do
- render_inline_svg(filename, transform_params)
+ cache_inline_svg(filename, transform_params)
end
end
+ def cache_inline_svg(*args)
+ @svg_files ||= {}
+ @svg_files[args] ||= render_inline_svg(*args)
+ end
...
This would certainly speed up pages with lots of SVG files. |
FYI, others are interested in this also. see: https://twitter.com/_swanson/status/1336783159460028421 |
If I have a list of a hundred items which all reference the same
inline_svg_tag
image, the content is duplicated for each item. It's possible to reduce the generated HTML by inlining the SVG once and referencing it in each item using the <use> tag.Is this something that fits in the scope of
inline_svg
? I understand if not, but it seems like a nice feature.The text was updated successfully, but these errors were encountered: