SVG mode: ids inside icons come from a global counter, so the same page renders different HTML on every request
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 77/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- nuxt, typescript
- Ambito
- frontend
Direzione di ricerca
Leggi src/runtime/components/svg.ts ed esamina come NuxtIconSvg passa le props a Icon di Iconify; il problema identifica la prop id stabile mancante e suggerisce useId() di Vue. Controlla i test esistenti sul rendering SVG e le correzioni precedenti di useId() nei commit f5e74f5, quindi verifica che renderizzazioni ripetute sul server producano gli stessi id SVG e che gli id del server e del client restino coerenti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Environment
@nuxt/icon: 2.2.1@iconify/vue: 5.0.0- Nuxt: 4.6.4
icon.mode: 'svg'
Reproduction
StackBlitz: https://stackblitz.com/edit/github-komwydjq-7hp6bfpa
Note: the StackBlitz starter currently locks
@nuxt/cli4.0.1, whose dev server crashes in StackBlitz withoptions.hostname.toLowerCase is not a function. The reproduction therefore overrides@nuxt/clito 4.0.3 (pnpm.overrides) and pinsnuxtto 4.6.4. Nothing else inpackage.jsonchanged.
Based on the Nuxt v4 starter with @nuxt/icon added. One custom icon whose SVG contains an id (a gradient):
<!-- app/assets/icons/gradient.svg -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><defs><linearGradient id="fill"><stop offset="0" stop-color="red"/><stop offset="1" stop-color="blue"/></linearGradient></defs><circle cx="12" cy="12" r="10" fill="url(#fill)"/></svg>
// nuxt.config.ts
export default defineNuxtConfig({
modules: ['@nuxt/icon'],
icon: {
mode: 'svg',
customCollections: [{ prefix: 'custom', dir: './app/assets/icons' }],
},
})
<!-- app/app.vue -->
<script setup lang="ts">
// Reads the id the server rendered into the icon (hydration keeps the server markup).
const renderedId = ref('')
onMounted(() => {
renderedId.value = document.querySelector('linearGradient')?.id ?? ''
})
</script>
<template>
<Icon name="custom:gradient" size="64" />
<p>Gradient id rendered by the server: <strong>{{ renderedId }}</strong></p>
</template>
Reload the page a few times. The id counts up with every request, so the same page renders different HTML each time:
iconifyVue1, iconifyVue2, iconifyVue3, iconifyVue4, iconifyVue5 (5 reloads, nuxt dev)
iconifyVue0, iconifyVue1, iconifyVue2, iconifyVue3, iconifyVue4 (5 requests, production build)
Describe the bug
NuxtIconSvg renders Iconify's <Icon> without an id prop. Without it, Iconify's replaceIDs() prefixes ids with iconifyVue plus a module-level counter that keeps counting across all requests the server handles. Each render of the same page therefore produces different ids, and different HTML.
Iconify already has a solution for this. With an id prop it uses id + 'ID' + localCounter (per component instance), as noted in @iconify/vue: "Counter for ids based on 'id' property to render icons consistently on server and client".
Why it matters
- ISR / caching: Vercel only skips an ISR write when a revalidation produces identical output (ISR limits and pricing). With changing ids, every revalidation of an unchanged page is billed as a new write. The same goes for anything that compares or hashes the HTML (ETags, diffing).
- Server/client consistency: the ids rendered on the server depend on how many requests the instance has served before, so they can't match what the client would generate.
Suggested fix
Pass a stable id to Iconify in NuxtIconSvg, e.g. Vue's useId():
// src/runtime/components/svg.ts
const id = useId()
// …
return () => h(Iconify, {
icon: name.value,
ssr: true,
id,
customise: resolveCustomizeFn(props.customize, options.customize),
}, slots)
useId() is stable per position in the component tree, on the server and the client. Note #310 / #356 (hydration mismatch with useId() next to <Icon />): those were about <Icon /> shifting other components' useId() values. They were fixed in f5e74f5, so calling useId() here should be safe, but it's worth a test.
I'm happy to open a PR.
Workaround
Pass the id from a wrapper component:
<script setup lang="ts">
defineProps<{ name: string }>()
const iconId = useId()
</script>
<template>
<Icon :id="iconId" :name="name" />
</template>
With this, repeated renders of the same page are byte-identical (verified on Vercel together with a fix for Nuxt's payload key order).
- Lingua principale
- TypeScript
- Stelle
- 1.2k
- Fork
- 103
- Merge medio
- 3g 6h
- PR unite (30g)
- 32
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di nuxt/icon
-
Client bundle lists collections in load-completion order, so identical builds get different chunk hashesForse già presa @lafllamme l’ha presa 2 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 89/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 4/5 3-5 giorni Idoneità per principianti 38/100
nuxt/icon#492 · 1 commento · 2 reazioni ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 35/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 30/100
I maintainer di solito rispondono entro 1 giorno
Issue simili
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
liberation-data/drivine#826 ·
I maintainer di solito rispondono entro 1 giorno
-
【优化】“打开文件列表”,建议对当前文件进行高对比度标注Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
zhitongblog/solomd#623 ·
I maintainer di solito rispondono entro 1 giorno
-
aufwand: klein documentation Einsteiger-Familie freigegeben Haushalts-Admin
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement frontend
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
openwallet-foundation/eudiplo#1600 ·
-
docs(website): add a GitLab CI/CD button next to the GitHub Action button on the homepageForse già presa @shryssssss-maker l’ha presa oggi. Apertadocumentation enhancement first-timers-only good first issue
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
OWASP/cve-lite-cli#1739 · 2 commenti ·
I maintainer di solito rispondono entro 2 giorni