nuxt-svgo icon indicating copy to clipboard operation
nuxt-svgo copied to clipboard

`?url` export option bypasses svgo

Open frederikheld opened this issue 2 years ago • 9 comments
trafficstars

The nuxt-svgo docs don't mention this feature, but the vite-svg-loader docs do: it is possible to import a svg as a file url (instead of embedding it into the source of the page) and it works with nuxt-svgo as well. I use this feature to generate an optimized version of the svg to use it as favicon.

Unfortunately this option seems to bypass the optimization through svgo. The default import embeds an optimized version of the svg into the source code, but the url import generates a file that is exactly the same as the asset that went in. Is this on purpose? Is there any way to have the optimization with the url import? Unfortunately the vite-svg-loader docs only mention that it is possible, but neither how it works inside nor how it can be configured...

I fiddled around with the svgoConfig option of nuxt-svgo but couldn't find any working configuration. Any ideas?

frederikheld avatar Jun 16 '23 17:06 frederikheld

looking at the source it does not support optimizing the ?url import, because there's a built-in ?url handler: https://vitejs.dev/guide/assets.html#explicit-url-imports

but since we are handling the import before that, we can probably simply url encode it ourselves and not rely on vite's functionality, this should be easy I guess. but it will be a breaking change, because it can potentially break it for anyone who is relying on it, what do you think @cpsoinos ? should we release it as a breaking change or as minor if we are going to do so ?

I'll work on it when I have time, not sure if I can do this week

jd1378 avatar Jun 16 '23 20:06 jd1378

Thanks for considering adding this feature :-)

Maybe keep the current behavior as default and add a config option that allows to run url imports through svgo optimization? This would avoid the breaking change ...

frederikheld avatar Jun 16 '23 23:06 frederikheld

hmm, good idea, thanks!

jd1378 avatar Jun 16 '23 23:06 jd1378

.... simply url encode it ourselves ...

I was really tired and I see that I misunderstood the feature lol what ?url does is that it gives the url for that file, not url encode it (what I originally thought it was)

I have to rethink how to do it, and how feasible it is

jd1378 avatar Jun 17 '23 13:06 jd1378

I guess I would be happy with the url encoded version as well, if it can be used as a favicon ...

frederikheld avatar Jul 05 '23 01:07 frederikheld

sorry, I didn't have access to my laptop for around 1.5 weeks I'll get to it soon

url encoded version probably can be done quite easily, but not sure about how the direct url to optimized file works, so will put that for a later time

jd1378 avatar Jul 05 '23 01:07 jd1378

I have released the url_encode query feature @frederikheld let me know if there's any issue I have updated the readme to include a note about it as well

jd1378 avatar Jul 05 '23 03:07 jd1378

@frederikheld did you manage to test it ?

jd1378 avatar Jul 07 '23 15:07 jd1378

I've seen that you implemented it, thanks a lot, that's awesome. However I did not find the time to test it as I have to take care of a different project atm. As soon as i get back to this project, I'll let you know if something doesn't work as expected :-) Thanks again for providing a solution so quickly!

frederikheld avatar Jul 10 '23 22:07 frederikheld