Fix "Snapshot missing" errors caused by unstable Livewire keys in the comment list#100
Open
ItsMalikJones wants to merge 3 commits into
Open
Fix "Snapshot missing" errors caused by unstable Livewire keys in the comment list#100ItsMalikJones wants to merge 3 commits into
ItsMalikJones wants to merge 3 commits into
Conversation
The list loop previously keyed each child component by `$comment->getContentHash()` — an MD5 of body + reactions that changes whenever a comment is edited or reacted to, and that collides for two comments with identical content. Both cases caused Livewire "Snapshot missing" errors after the keyed component re-keyed itself mid-lifecycle. `getId()` is stable across edits/reactions and unique per row, which is what Livewire keys need.
Include the class name in Livewire component keys to prevent collisions when Comment and RenderableComment instances share the same ID. Mark `getContentHash()` as deprecated since it's no longer used for key generation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #72.
Problem
comment-list.blade.phpkeyed each child<livewire:dynamic-component>by$comment->getContentHash(). For aComment,getContentHash()returnsmd5(body + reaction ids), which has two failure modes:so the keyed component effectively re-keys itself mid-lifecycle.
hash, so they share a
wire:key.Either case breaks Livewire's DOM reconciliation and surfaces as
Snapshot missing on Livewire componenterrors.Reproduce: post two comments with identical bodies, or edit / react to an
existing comment in a list — the component throws "Snapshot missing".
Fix
Key each component by
$comment::class . ':' . $comment->getId():edited or reacted to.
getId()returns the primary key.Commentand aRenderableCommentthat happen to share a numeric id from colliding. This isa real scenario when
getComments()is overridden to merge non-comment itemsinto the list.
Deprecation
getContentHash()is no longer used anywhere in the package. It is marked@deprecatedon theContracts\RenderableCommentinterface and bothimplementations (
Comment,RenderableComment). It is not removed — it ispart of the public contract, so removal is deferred to a future major version.
Tests
Three tests added to
tests/Livewire/CommentListTest.php:renders duplicate-content comments without key collision— two comments withidentical bodies render distinct keys.
keeps comment keys stable across edits— a comment's key is unchanged afterits body is edited.
renders Comment and RenderableComment sharing an id without key collision—a
Commentand aRenderableCommentwith the same id render distinct keys.Backwards compatibility
No breaking changes. The
wire:keyvalue format is internal to Livewire'srendering and is not a public API.
getContentHash()remains callable(deprecated only).