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.

Revisit SVG data URI

See original GitHub issue

Background

SVG files are treated differently than other images in CRA. (https://create-react-app.dev/docs/adding-images-fonts-and-files)

https://github.com/facebook/create-react-app/issues/1153 disabled automatic conversion of small SVG files into data URIs. The reason for that was incompatibility with SVG sprite systems, where the “fat” SVG document needs to be an external entity (or it could be inlined).

Oddly, the referenced svg-sprite and gulp-svg-sprite projects are not very active, with last releases ~1year ago each. Does that make the sprite technique obsolete? grandfathered? in active use?

I suspect that the <style> limitation (see below), also applies to SVG sprite systems.

It’s odd to have special handling for SVG in CRA, when a sprite system is not bundled by default.

Inline SVG

(https://create-react-app.dev/docs/adding-images-fonts-and-files#adding-svgs) There’s an alternative of using inline SVG XML, with some limitations:

  • namespaces
  • inline <style> elements

The core of the latter is that SVG styles are global, i.e. style in one inlined SVG pollutes all inlined SVG and even HTML. There are projects that attempt to inline the style into the SVG elements, but that’s not always possible, for example, keyframes cannot be inlined. It may be possible to rename all inline styles to unique names for a given project [link? help needed to reference such projects].

SVG is typically designed in variety of 3rd party tools (let’s say design team vs. dev team), thus it’s unrealistic to require users to “make better SVG”.

Proposal.

As it is now, SVG handling is an ugly nit.

Let’s reinstate data URI conversion of SVG files by default. This allows isolation between SVG files. I believe that’s what most users expect. It’s certainly more common for beginners than advanced techniques like sprites.

Let’s optionally provide an escape hatch for sprite system users.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mrhethercommented, Dec 15, 2020

Or a slightly less coupled solution:

const {
  override,
  addWebpackModuleRule
} = require('customize-cra');

module.exports = override(
  addWebpackModuleRule({
    test: /[.]svg$/,
    loader: require.resolve('url-loader'),
    options: {
      name: 'static/media/[name].[hash:8].[ext]'
    }
  })
);

1reaction
heyimalexcommented, Aug 23, 2019

Unless I’m misunderstanding, everything works perfectly, it’s just that small svgs aren’t inlined so there are extra requests? It seems like reverting this would be breaking, since any svg with a fragment identifier would be broken.

That being said, I think we could pretty easily update our use of babel-plugin-named-asset-import to include support for a named Url type for people who explicitly want a non-inlined file, or maybe Inline for the opposite.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Revisiting SVG workflow for performance and progressive ...
Revisiting SVG workflow for performance and progressive development with transparent data URIs · Learn JavaScript the right way. · Feature detect.
Read more >
09: SVG with Data URIs
We use data uris for icons that are repeated. You can have a CSS class that you can apply to everywhere you want...
Read more >
1616 (Remote CSS: Allow data URIs in CSS properties)
I'm using the Remote CSS feature for WordCamp Boston. I'd like to use SVGs for background images, encoded directly into the CSS file...
Read more >
Checkered pattern for loading images, revisited
First, I can't use an image—where's the fun in that? It'd be easy enough to use an SVG data URI, but that's still...
Read more >
SVG Data uri - Visual Studio Marketplace
SVG Data Uri Generator. A simple addon to generate a SVG data uri. For use in CSS background property. preview. screenshot.
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