{"record":{"id":"2809723ea6184148","repo":"sveltejs/svelte","slug":"each-key-volatile","errorCode":"each_key_volatile","errorMessage":"each_key_volatile\nKeyed each block has key that is not idempotent — the key for item at index ${index} was `${a}` but is now `${b}`. Keys must be the same each time for a given item\nhttps://svelte.dev/e/each_key_volatile","messagePattern":"each_key_volatile\nKeyed each block has key that is not idempotent — the key for item at index (.+?) was `(.+?)` but is now `(.+?)`\\. Keys must be the same each time for a given item\nhttps://svelte\\.dev/e/each_key_volatile","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/svelte/src/internal/client/errors.js","lineNumber":165,"sourceCode":"\t}\n}\n\n/**\n * Keyed each block has key that is not idempotent — the key for item at index %index% was `%a%` but is now `%b%`. Keys must be the same each time for a given item\n * @param {string} index\n * @param {string} a\n * @param {string} b\n * @returns {never}\n */\nexport function each_key_volatile(index, a, b) {\n\tif (DEV) {\n\t\tconst error = new Error(`each_key_volatile\\nKeyed each block has key that is not idempotent — the key for item at index ${index} was \\`${a}\\` but is now \\`${b}\\`. Keys must be the same each time for a given item\\nhttps://svelte.dev/e/each_key_volatile`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/each_key_volatile`);\n\t}\n}\n\n/**\n * `%rune%` cannot be used inside an effect cleanup function\n * @param {string} rune\n * @returns {never}\n */\nexport function effect_in_teardown(rune) {\n\tif (DEV) {\n\t\tconst error = new Error(`effect_in_teardown\\n\\`${rune}\\` cannot be used inside an effect cleanup function\\nhttps://svelte.dev/e/effect_in_teardown`);\n\n\t\terror.name = 'Svelte error';\n\n\t\tthrow error;\n\t} else {\n\t\tthrow new Error(`https://svelte.dev/e/effect_in_teardown`);\n\t}","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/sveltejs/svelte/blob/20b341f10048cf1016a2028ac7eee5595cfef6a5/packages/svelte/src/internal/client/errors.js#L147-L183","documentation":"Runtime error `each_key_volatile`: the keyed-each key function returned a different value for the same item between renders (`a` then `b`). Keys must be idempotent — the same item must always produce the same key — otherwise the reconciler cannot track identity. The message shows the index and the two conflicting key values.","triggerScenarios":"At runtime (DEV) when the keying function is non-deterministic or reads mutable state, e.g. `(Math.random())`, `(item.id++)`, `(Date.now())`, or a key that depends on a counter that changes each render.","commonSituations":"Using random/hash-of-object keys; keying on a property that the application mutates between renders; keying on an index that shifts as the list changes; objects whose `id` is assigned lazily on first access.","solutions":["Key on a stable, immutable property of each item (an ID assigned once at creation).","If items lack a stable ID, generate and assign one when the data is first loaded (e.g. via a `crypto.randomUUID()` map) and never change it.","Remove any side effects or random values from the key expression."],"exampleFix":"{#each items as item (Math.floor(Math.random()*1000))}\n<!-- key changes every render -->\n\n{#each items as item (item.id)}\n<!-- stable id assigned at load -->","handlingStrategy":"validation","validationCode":"// Verify the key function is idempotent across two calls on the same item.\nfunction isKeyIdempotent(items, keyFn) {\n  return items.every(item => keyFn(item) === keyFn(item));\n}\n// also assert no side effects / no randomness in the key expression:\nfunction riskyKeyExpr(template) {\n  return /\\(\\s*(Math\\.random|Date\\.now|crypto\\.)|\\+\\+|\\-\\-/.test(template);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Key on a stable immutable property assigned once at item creation.","If items lack IDs, generate them once when the data is loaded and store them on the item.","Keep key expressions pure and free of side effects or randomness."],"tags":["svelte5","runtime","each","keyed-list","idempotency","rendering"],"backgroundTag":null,"analyzedSha":"20b341f10048cf1016a2028ac7eee5595cfef6a5","analyzedAt":"2026-08-12T23:37:30.399Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}