You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Keyed x-for with seems to not be robust against adding/removing elements #4843
Safafi/Vivaldi (chrome) on macOS, Chrome on Windows
Describe the issue you're experiencing
I am trying to implement virtual scrolling on a large photo album. My approach is to make a div sized to the height of the full album, and put another div inside that with its top set to slightly above the window scroll region and render a subrange of the images inside that.
Or in slightly simplified code:
<div :style="{ height: height }">
<div :style="{ top: top }" @scroll.window="UpdateLayout()">
<template x-for="i in visible_end - visible_start">
<img src="photos[visible_start + i - 1]">
</template>
</div>
</div>
This works fine, except that Alpine reuses img tags, and if you change an img's src it will show the old image (thumbnail in this case) until the new one has fully loaded, which is visibly janky while scrolling the page.
I believe the standard fix for this is to add :key=visible_start + i to force Alpine to create new elements, which it does, but it also seems like Alpine gets confused by the additions/removals and eventually messes up the page.
Scroll around for a bit and you will notice that 1) all the elements can "jump" up or down when they shouldn't and 2) sometimes elements get duplicated, most obviously sometimes if you scroll all the way back to the top the first row gets duplicated. If you remove :key in the inspector it all works as intended.
Screenshots/screen recordings
Screen.Recording.2026-06-14.at.10.00.24.am.mov
How do you expect it to work?
.
Please confirm (incomplete submissions will not be addressed)
I have provided easy and step-by-step instructions to reproduce the bug.
I have provided code samples as text and NOT images.
I understand my bug report will be removed if I haven't met the criteria above.
As an immediate workaround or in case this isn't actually a bug, <template x-for="i in Array.from( { length: visible_end - visible_start }, ( _, i ) => visible_start + i )" :key="i"> seems to work
Thanks for the great repro @mikejsavage — this one was subtle. Confirmed on main: with :key="visible_start + i", when the visible window shifts, reused keyed elements can render with a stale loop scope. In my repro (window shifting from 1–15 to 13–30), the first reused rows rendered 25, 26, 27 where 13, 14, 15 should be — stale i plus the new visible_start — which is also why you saw rows duplicated further down.
The root cause is in the scheduler: an effect that already ran earlier in the same flush couldn't be re-queued, so after x-for refreshed the reused element's scope, its x-text never re-ran. I verified @joshhanley's fix in #4851 against this exact scenario and it resolves it — same repro passes on that branch. Your Array.from workaround is a solid stopgap until that lands.
Activity
joshhanley commentedon Jun 14, 2026
@mikejsavage thanks for reporting! Can you please provide code for a simple reproduction that we can copy to our machines to investigate? Thanks!
mikejsavage commentedon Jun 15, 2026
https://mikejsavage.co.uk/alpinejs.txt is self contained besides
<script defer src="./alpine-3.15.12.js"></script>and can be opened as a file. The relevant code is 7 lines at the endjoshhanley commentedon Jun 15, 2026
@mikejsavage please just paste the code here in code blocks. Thanks!
mikejsavage commentedon Jun 15, 2026
mikejsavage commentedon Jun 15, 2026
As an immediate workaround or in case this isn't actually a bug,
<template x-for="i in Array.from( { length: visible_end - visible_start }, ( _, i ) => visible_start + i )" :key="i">seems to workjoshhanley commentedon Jul 1, 2026
@mikejsavage thanks for that! I've submitted a PR #4851 with a fix.
mikejsavage commentedon Jul 1, 2026
Great, thanks!
calebporzio commentedon Jul 4, 2026
Thanks for the great repro @mikejsavage — this one was subtle. Confirmed on main: with
:key="visible_start + i", when the visible window shifts, reused keyed elements can render with a stale loop scope. In my repro (window shifting from1–15to13–30), the first reused rows rendered25, 26, 27where13, 14, 15should be — staleiplus the newvisible_start— which is also why you saw rows duplicated further down.The root cause is in the scheduler: an effect that already ran earlier in the same flush couldn't be re-queued, so after
x-forrefreshed the reused element's scope, itsx-textnever re-ran. I verified @joshhanley's fix in #4851 against this exact scenario and it resolves it — same repro passes on that branch. YourArray.fromworkaround is a solid stopgap until that lands.