Skip to content
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

transformAssetUrls strips all queries and breaks SVG sprites #490

Open
7 tasks done
dirkniblick opened this issue Dec 13, 2024 · 4 comments · May be fixed by #491
Open
7 tasks done

transformAssetUrls strips all queries and breaks SVG sprites #490

dirkniblick opened this issue Dec 13, 2024 · 4 comments · May be fixed by #491
Labels
bug Something isn't working p3-minor-bug 🔨 An edge case that only affects very specific usage (priority)

Comments

@dirkniblick
Copy link

Describe the bug

I updated one of my work projects to Vite 6 and all the images in SVG tags disappeared. If I move Vite back to 5.4.9 or later, which is where I was prior to updating my project, the SVGs come back.

Reproduction

https://github.com/dirkniblick/vite-6-breaks-svg

Steps to reproduce

Here's a reproduction of the repo README:

Project

I created a project using npm create vue@latest and removed most of the default project files (i.e., very little is left in /src). At this point, it just displays a couple SVGs. Here's the most relevant files:

src/vite.config.js 👈 Unchanged from when project was created

import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'

// https://vite.dev/config/
export default defineConfig({
    plugins: [
        vue()
    ],
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        },
    },
})

src/assets/shapes.svg

<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg">
    <circle id="circle" r="10" cx="10" cy="10" fill="red"/>
    <rect id="square" x="0" y="0" width="20" height="20" fill="green"/>
</svg>

src/App.vue

<template>
    <svg viewBox="0 0 20 20" width="10rem">
        <use href="@/assets/shapes.svg#circle"/>
    </svg>
    <svg viewBox="0 0 20 20" width="10rem">
        <use href="@/assets/shapes.svg#square"/>
    </svg>
</template>

Expectation

With Vite 5.4.11 we get this in Firefox (I've tested all this in Chrome too):

vite5browser

The developer tools shows this:

vite5devtools

Problem

If I change Vite to 6.0.0, or anything later, and don't change anything else, the shapes disappear:

vite6browser

The developer tools shows this:

vite6devtools

Comparison

It looks like Vite is not processing the file correctly.
If there's something I need to change in the Vite config, please let me know, but I haven't spotted anything specific to SVGs in the migration document or any other issue on Github. Here's the developer tools side-by-side, but you can see the <use href= is quite different and the use tag doesn't seem to contain the path code anymore.

Vite 5.4.11 Vite 6.0.3
vite5devtools vite6devtools

System Info

System:
    OS: Windows 11 10.0.22631
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i9-12900H
    Memory: 33.88 GB / 63.68 GB
  Binaries:
    Node: 18.19.1 - C:\Program Files\nodejs\node.EXE
    npm: 10.2.4 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (130.0.2849.68)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @vitejs/plugin-vue: ^5.2.1 => 5.2.1 
    vite: ^6.0.3 => 6.0.3

Used Package Manager

npm

Logs

No response

Validations

@btea
Copy link
Contributor

btea commented Dec 14, 2024

Currently, you can set assetsinlinelimit to 0 to make svg render normally. https://vite.dev/config/build-options.html#build-assetsinlinelimit

@bluwy
Copy link
Member

bluwy commented Dec 15, 2024

We should have something like https://github.com/vitejs/vite/blob/63b82f1e29a00d06a82144fd03ea8d6eff114290/packages/vite/src/node/plugins/asset.ts#L452-L453 but it might not be kicking in somehow.

@btea
Copy link
Contributor

btea commented Dec 15, 2024

In the current scenario, the corresponding SVG id value after converting the vue file does not carry #, which fails to meet this condition. example

@sapphi-red
Copy link
Member

Yeah, this is caused by plugin-vue / @vue/compiler-sfc stripping the queries automatically.

@sapphi-red sapphi-red transferred this issue from vitejs/vite Dec 16, 2024
@sapphi-red sapphi-red changed the title Vite 6 Breaks SVGs transformAssetUrls strips all queries and breaks SVG sprites Dec 16, 2024
@btea btea linked a pull request Dec 16, 2024 that will close this issue
9 tasks
@haoqunjiang haoqunjiang added bug Something isn't working p3-minor-bug 🔨 An edge case that only affects very specific usage (priority) and removed pending triage labels Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p3-minor-bug 🔨 An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants