SVG mode: ids inside icons come from a global counter, so the same page renders different HTML on every request

Aperta Adatta ai principianti
#585 1 commento 1 reazione 0 assegnatari Vedi su GitHub

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/cli 4.0.1, whose dev server crashes in StackBlitz with options.hostname.toLowerCase is not a function. The reproduction therefore overrides @nuxt/cli to 4.0.3 (pnpm.overrides) and pins nuxt to 4.6.4. Nothing else in package.json changed.

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

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nuxt/icon

Tutte le issue di nuxt/icon

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.