Client bundle lists collections in load-completion order, so identical builds get different chunk hashes

Aperta Adatta ai principianti
#582 0 commenti 0 reazioni 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
89/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
bun, node.js, nuxt, typescript, vite

Direzione di ricerca

Leggi src/core/bundle.ts nella sezione relativa a resolveBundleIcons e test/bundle.test.ts; l’issue include una riproduzione e propone un ordinamento deterministico. Inizia eseguendo lo scenario di test fornito e controllando l’ordine attuale delle collection. Il lavoro è completato quando le collection vengono risolte in un ordine stabile per punti di codice e il test di regressione passa.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Environment
  • @nuxt/icon 2.6.3 (the same code is on main at 28c9943)
  • nuxt 4.6.4, vite 8.3.8, @iconify/utils 3.1.7
  • Linux x64, Bun 1.4.4 (bun --bun nuxt build) and Node 22.23.4
Reproduction

nuxt.config.ts, with @iconify-json/lucide, @iconify-json/ph, @iconify-json/cib and @iconify-json/simple-icons installed:

export default defineNuxtConfig({
  modules: ['@nuxt/icon'],
  icon: {
    clientBundle: {
      icons: [
        'lucide:arrow-down', 'lucide:x', 'lucide:check',
        'ph:gear', 'ph:house', 'ph:user',
        'cib:ideal', 'cib:bancontact',
        'simple-icons:github', 'simple-icons:americanexpress',
      ],
    },
  },
  compatibilityDate: '2026-11-01',
})

The order is easiest to see with the exported resolver. order.mjs in the project root:

import { resolveBundleIcons } from '@nuxt/icon/utils'

const { collections } = await resolveBundleIcons({
  icons: [
    'lucide:arrow-down', 'lucide:x', 'lucide:check',
    'ph:gear', 'ph:house', 'ph:user',
    'cib:ideal', 'cib:bancontact',
    'simple-icons:github', 'simple-icons:americanexpress',
  ],
})
console.log(collections.map(c => c.prefix).join(','))

Each run needs its own process, because the collection loader caches within one:

seq 64 | xargs -P 8 -I{} bun order.mjs | sort | uniq -c
     44 lucide,cib,ph,simple-icons
     18 lucide,cib,simple-icons,ph
      1 cib,lucide,simple-icons,ph
      1 cib,lucide,ph,simple-icons

node order.mjs gives three orders as well (61 / 2 / 1 out of 64), less often.

Effect on builds

The resolved collections are serialized as they are into nuxt-icon-client-bundle.mjs. When their order changes, the chunk that holds the client bundle changes, though its only difference is the order of the collections inside JSON.parse(...). Every chunk that imports it then gets a new hash as well.

I built three identical copies of the app above at the same time, as on a busy CI runner, round after round with bun --bun nuxt build:

Builds Collection orders Distinct sets of _nuxt/*.js names
2.6.3 36 3 3
2.6.3 with the fix below 36 1 1

The two most common orders differ in 8 of this app's 13 chunk names. Six sequential builds on an idle machine all matched, which makes the problem easy to miss.

In a larger app (115 chunks), two builds whose only difference was a stylesheet that no script references renamed 51 chunks. The only root of those renames was the chunk holding the client bundle. Returning visitors download those chunks again after each deploy, even though their code is unchanged.

Expected

Identical inputs produce identical chunk names.

Cause

resolveBundleIcons loads collections inside Promise.all. addIcon adds a collection to the collections map when its first icon resolves, so the map holds the collections in the order their files finished loading:

#446 (for #445) sorted the icons inside each collection, but the collections are still returned in insertion order:

With scan: true, the order in which the loads start varies too, because scanned files are read in parallel. Both the module's client bundle (src/context.ts) and the standalone Vite plugin (src/vite.ts) use this function.

Suggested fix
   return {
-    collections: [...collections.values()],
+    collections: [...collections.values()].sort((a, b) => a.prefix < b.prefix ? -1 : a.prefix > b.prefix ? 1 : 0),
     count,

The test below, for test/bundle.test.ts, fails today: custom collections resolve immediately, so without the sort they come back in request order (['zeta', 'alpha']).

it('returns collections in a stable order', async () => {
  const collection = (prefix: string) => ({
    prefix,
    icons: { foo: { body: '<path d="M0 0h24v24H0z"/>' } },
    width: 24,
    height: 24,
  })
  const result = await resolveBundleIcons({
    icons: ['zeta:foo', 'alpha:foo'],
    customCollections: [collection('zeta'), collection('alpha')],
    resolvePaths: [root],
  })

  expect(result.collections.map(c => c.prefix)).toEqual(['alpha', 'zeta'])
})

The comparison is by code point on purpose. Under Node, localeCompare() follows the build machine's locale: LANG=et_EE.UTF-8 sorts zondicons before tabler, and LANG=da_DK.UTF-8 sorts names starting with aa last. The icon-name sort from #446 uses localeCompare(), so it can also differ between machines.

Lingua principale
TypeScript
Stelle
1.2k
Fork
103
Merge medio
3g 6h
PR unite (30g)
33

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.