Comments (17)
And yes, all the required svgs get added to the DOM, even when they are not in use.
This is a concern for me, because it is adding unused elements to the DOM. The projects I work on try to avoid dumping unused data to the DOM to keep the DOM/templates clean and lightweight. Also, in certain situations, SVG's can come with security risks, so in a paranoid world: it's best to limit SVG's to only those you require.
I think that many of the ideas presented by @wmertens are really great, and I think providing a react-specific loader option is a cool idea, or breaking the react-ready component out to it's own repo.
from svg-sprite-loader.
@wmertens you want something like this - https://github.com/webpack/style-loader#reference-counted-api?
from svg-sprite-loader.
Yes I suppose that can be used for it.
I just realized that #32 and that refcount API can be implemented in a backwards compatible way by not returning a string id, but an object that returns the id for .toString()
and .valueOf()
, and also has .use()
, .unuse()
and .viewBox
…
from svg-sprite-loader.
Ok, I'm starting to need this, so I'd like your thoughts on the proper approach. This is what I think needs to be done:
- Create
<Sprites/>
React component that returns the<svg/>
with all the sprites (instead of usingdocument.write
). Add a query flaguseSprites: true
to the loader to enable it. - Create
<Sprite id={...}/>
React component that makes a<svg><use .../></svg>
.- It could have a
noSvg
attribute to just return the<use/>
so it can be used inside<svg/>
.
- It could have a
- Add refcounting API, and change the above components to use it. The output of the loader should be an object with:
.toString()
: returns sprite id.use()
,.unuse()
: to be called from the<Sprite/>
component'sconstructor
and...WillUnmount
.Sprite
: pre-bound<Sprite/>
component
So example usage would be
import {Sprite, Sprites, initSprites} from 'svg-sprite-loader'
import star from './star.svg'
…
const App = () => {
initSprites() // only needed for server side rendering
return (<div>
<Sprite id={star}/><Sprite id={star}/><Sprite id={star}/>
<Sprites/>
</div>)
}
or with the pre-bound Sprite:
import {Sprites, initSprites} from 'svg-sprite-loader'
import {Sprite: Star} from './star.svg'
…
const App = () => {
initSprites() // only needed for server side rendering
return (<div>
<Star/><Star/><Star/>
<Sprites/>
</div>)
}
Thoughts? I realize this is only for React, so maybe the components should be in a separate repo?
from svg-sprite-loader.
With the loader-returns-object approach, the <symbol/>
source can be part of that object, so that it can be passed to <Sprites/>
via .use()
.
That way, any implementation is possible, and the loader is only responsible for creating the symbol from the .svg file.
from svg-sprite-loader.
Continuing the thinking: Make a react-sprite-loader
which exports <Sprites/>
and initSprites()
apart from being a webpack loader, and which expects objects with id
and symbol
(the symbol source as a string) as input.
Tell webpack to chain it with svg-sprite-loader, something like:
{
test: /icons\/.*\.svg/,
loaders: ['react-sprite', 'svg-sprite?object&loadOnly'],
}
and in the code
import {Sprites, initSprites} from 'react-sprite-loader'
import Star from './icons/star.svg'
const Component = (props) => (
<div>
<Star/><Star/><Star/>
</div>
)
const App = (props) => {
initSprites() // only needed for server side rendering
return (
<div>
<Component/>
<Sprites/>
</div>
)
}
So then svg-sprite-loader would only need to return plain objects with id
and symbol
(and viewBox
). The id
could be a hash of the symbol, which would cause de-duplication.
from svg-sprite-loader.
I'd like to @princed answered your question, because I am not good in React. But I fully agree with suggestion to return object (or class instance) from sprite-loader.
from svg-sprite-loader.
Actually we use a bit different approach like described in the very end of the README, so icon component doesn't have to require all the icons.
I don't think we should have something react specific. We'd better have common SSR solution.
And we could use counting API to remove inactive defs from DOM, however it could lead to more complex code due our workarounds.
from svg-sprite-loader.
@princed how does that work? All the icons are required statically, no? And then they all get added to the DOM, even when they are not in use?
from svg-sprite-loader.
from svg-sprite-loader.
@wmertens Yes, all the svgs are required statically in components that use icon component.
And yes, all the required svgs get added to the DOM, even when they are not in use.
Why does it actually concern you?
from svg-sprite-loader.
And we could use counting API to remove inactive defs from DOM, however it could lead to more complex code due our workarounds.
Does this style-loader-like solution seem appropriate to you?
from svg-sprite-loader.
it is adding unused elements to the DOM
Just don't import unnecessary SVGs in your code
from svg-sprite-loader.
And we could use counting API to remove inactive defs from DOM, however it could lead to more complex code due our workarounds.
Does this style-loader-like solution seem appropriate to you?
@wmertens @radiovisual I've just wanted to be sure that it's way to go for you.
Because I'd rather keep this loader framework-agnostic despite we use React for our products too.
from svg-sprite-loader.
@princed , I am all for an option that keeps things framework agnostic, but lets you easily drop into a React environment when needed. Sounds good to me, so I like @wmerten's react-style-loader
idea.
from svg-sprite-loader.
I'm just curious why some may want to deal with this stuff manually although no one does the same with styles from style-loader.
from svg-sprite-loader.
New loader version adds ability to specify custom runtime generator. Please check default generator and custom generator implementation example.
from svg-sprite-loader.
Related Issues (20)
- Webpack 5 cache=filesystem HOT 2
- Performance consumption HOT 1
- del
- NPM recommending svg-sprite-loader 2.0.3 as a vulnerability fix?
- Exception emitted for every svg
- How can I add new attributes to the parent SVG?
- Is there a way to set role, focusable and other attributes to root svg sprite tag without extracting?
- Regular Expression Denial of Service in postcss (6.0.11) HOT 2
- Unable to modify spriteFilename if loader rule has resourceQuery while using SpriteLoaderPlugin HOT 1
- SvgSpriteLoader is trying to guess you are using webpack 4 or 5 HOT 1
- Node16 npm audit moderate HOT 1
- SVG created is blank HOT 1
- The plugin doesn-t see rules if config in oneOf HOT 2
- Abnormal width and height
- Dependency on loader-utils 1.1.0 is security concern, CRITICAL vulnerabilities
- Module Warning (from ./node_modules/svg-sprite-loader/lib/loader.js): svg-sprite-loader exception. Some loaders will be applied after svg-sprite-loader in extract mode
- sprite.svg load every time when change showing icon HOT 1
- svg-sprite-loader not working well with encore webpack
- Webpack deprecation warning
- Esbuild alternative HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svg-sprite-loader.