-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
How to require generated sprite in JS file #147
Comments
You probably don't want to
All approaches have pros and cons and the "best" option depends on your setup. If you have any further questions feel free to comment ✌🏼 |
@cascornelissen Sorry for pinging on an old closed issue. Didn't want to create a new issue for asking this question. <template>
<svg>
<use :xlink:href="'/svg/sprite.svg#' + icon"/>
</svg>
</template> Which directly references the generated file at the public path ( Do you know anyway I can solve this issue ? You mentioned 3 solutions, and I don't quite know how to do the second and the third ones. Any helps would be appreciated! Thanks in advance! |
No problem, the main issue is that you want to bust the cache when the contents of the spritemap changes. The best way to do this is to use Then the next, and probably harder-to-solve issue is that you now have a filename with a dynamic value. The solution to this really depends on your stack, which is why it's hard to answer this question as I haven't worked with Laravel (mix) in a few years. I expect that both the second and third option in #147 (comment) are possible in your case. The second option describes a solution where you use the power of the templating engine that's used by the project. The templating engine probably has access to the filesystem and should be able to figure out what the filename of the spritemap is. Then this templating engine injects the SVG directly into the HTML that it's generating, this way you don't have to reference an external file but you can reference an element that's available in the DOM already. The main downside of this approach is that you're forcing users to download the entire spritemap because it's part of the HTML document, kind of defeating one of the main points on why you'd want to use a spritemap in the first place. The third option describes a solution that makes use of another webpack plugin. This plugin generates a manifest JSON file with original filenames as keys and hashed filenames as values, allowing you to basically do
I hope this clears up the approaches you can take, I'm sure there are other ways to accomplish this but this should at least give you some insights. |
@cascornelissen Thanks a ton for the quick answer and detailed explanation 😇 // main layout file
<script>
window.sprite_path = "{!! mix('svg/sprite.svg') !!}";
</script> and then <use :xlink:href="'/svg/' + sprite_path + '#' + icon"/>
...
data(){
return {
sprite_path: window.sprite_path.split('/').pop(),
}
}, The only ugly side of this approach was the mix webpack configuration because webpack doesn't pick up the sprite file generated by this plugin for some reason 🤔 Thanks a lot again anyway, great stuff 😄👍 |
This would be the exact way I want to go in a current project. But after spending time trying to figure out which plugin you may have in mind I still have no idea. I have found plugins which generate manifest files but none of them made a variable available in template files or allowed me to import the manifest. Can you give some more details on this? |
That comment seems to refer back to the comment I made earlier in this very same thread: #147 (comment). That earlier comment mentions
That's just the first step though, you'll need to import this JSON file and use the correct key to reach the hashed filename value.
Most plug-ins aren't going to provide this as it fully depends on your setup. If you're using a templating engine you'll have to look into the documentation for that tool on how to import and interpret JSON files. If, for example, you're using Laravel's Blade something like this will probably work. If you're rendering things client-side it might be as "easy" as using a dynamic import to fetch the file. |
In my projects I use the html-loader so the hashed paths are written into the resulting html files when linking images. My knowledge is a little limited here, but afaik the image paths are parsed into require functions. I was hoping for a solution which works alike for spritemaps generated by this plugin, without having to use a template engine or client side scripts, just a static html file with the hashed filename for the sprite. But if I get you right now, the manifest will not help me here. Thank you for the explanation. |
I don't have much experience with |
Hi i have a quiestion and can't find solution anywhere. We want to add hash to the generated sprite, so when we add or change an icon, the browser will download newer spritefile.
This can be easily achieved with
.addPlugin(createSvgSpritemapPluginInstance('images/icons.[contenthash].svg'))
And this will generate the file and line in manifest.json
"dist/images/icons.svg": "/dist/images/icons.68e91515460ebaae.svg",
.The problem is how to use this file in a React component or in any JS file? I am trying to do an import but can't seem to find the right combination:
/import icons from '/dist/images/icons.svg';
The problem is that the file physicaly doesn't exist and is generated during compilation.
Webpack will exit with
`This dependency was not found:
`
is there a way how to use file with hash in JS file?
The text was updated successfully, but these errors were encountered: