Skip to content

Conversation

@dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Nov 25, 2025

It's possible that due to how new elements are inserted into the array that effect.last is wrong. We need to ensure it is really the last item to keep items properly connected to the graph. In addition we link offscreen items after all onscreen items, to ensure they don't have wrong pointers.

#17244 as an alternative didn't work because it is possible due to how we don't link all items during reconciliation to end up with the wrong effect.last, which subsequently can mean that an item "falls out" of the graph. The added test showcases how this goes wrong with the previous logic:

  1. start with [1, 2, 3]
  2. create 4 -> is now effect.last
  3. insert at second position -> [1, 4, 2, 3] -> effect.last now 2 (not 3 because that isn't relinked because the order didn't change and reconciliation sees that)
  4. create 5 -> is now effect.last, which means 2.next is now 5 and no longer 3
  5. insert at third position -> effect.last now 2 again -> noone has 3 as next -> 3 fell out of the graph
  6. modify 3 -> nothing happens

The fix is to go back to the behavior prior to #17150 which is "set the last item as last", additionally taking into account offscreen items.

Fixes #17201
Fixes #17239

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

It's possible that due to how new elements are inserted into the array that `effect.last` is wrong. We need to ensure it is really the last item to keep items properly connected to the graph. In addition we link offscreen items after all onscreen items, to ensure they don't have wrong pointers.

Fixes #17201
@changeset-bot
Copy link

changeset-bot bot commented Nov 25, 2025

🦋 Changeset detected

Latest commit: 3058046

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@17240

@Rich-Harris
Copy link
Member

I think the fix should be in link rather than reconcile: #17244

@dummdidumm dummdidumm closed this Nov 25, 2025
@dummdidumm dummdidumm deleted the each-block-insertion-fix branch November 25, 2025 21:43
@dummdidumm dummdidumm restored the each-block-insertion-fix branch November 26, 2025 11:22
@dummdidumm dummdidumm reopened this Nov 26, 2025
@Rich-Harris
Copy link
Member

While digging into this I built a stress test that checks whether reconciliation succeeds and effects remaining linked to the tree. It turns out that there are some tricky bugs around transitions that are unaffected by this PR; I aim to investigate those next.

In the meantime, it looks like this PR succeeds where main fails with transitions disabled (try preset 3 with this branch and 5.44.1. I would love it if we could avoid the extra list traversal, but for now I think it's a price worth paying.

@Rich-Harris Rich-Harris merged commit da4cd0e into main Nov 26, 2025
18 checks passed
@Rich-Harris Rich-Harris deleted the each-block-insertion-fix branch November 26, 2025 19:06
@github-actions github-actions bot mentioned this pull request Nov 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parts of application infrequently broken after v5.43.12 [5.43.12] unshift breaks keyed #each index

3 participants