Repository navigation
Should i load SVG files with @nuxt/image or use a simple img tag? #1218
Replies: 2 comments 5 replies
|
I’d suggest inlining them as Vue components with import svgLoader from 'vite-svg-loader'
export default defineNuxtConfig({
vite: {
plugins: [
svgLoader(),
],
},
}) |
|
I would not use For SVGs the usual image-optimization benefits are mostly not there: there is no useful raster resize/quality/format pipeline, and routing the file through IPX can add an unnecessary request/transform layer. If the SVG is a local icon or illustration, I would use one of these instead: <img src="/images/my-vector.svg" alt="...">or import it from assets if your setup needs bundling/fingerprinting: <script setup lang="ts">
import iconUrl from "~/assets/images/my-vector.svg"
</script>
<template>
<img :src="iconUrl" alt="...">
</template>If you need to style the SVG internals, inline it as a Vue component, for example with I would reserve |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Comparison
With
imgtagusing
string:or using import (only relevant for local images):
With
nuxt-imgonly
stringpossible:My thoughts
nuxt-imgornuxt-picturefor every image keeps the code consistent and easy to understandimgtag for.svgfiles would save an unnecessary request to the ipx endpointimgtag allows to use imported.svgWhat is your opinion? Is there an official recommendation?
All reactions