Repository navigation
reze: create the avatar image element once so a sibling fallback does not recreate it - #13
Merged
Merged
Conversation
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.
Problem
In
@rebase-ui/reze, anAvatar.Imageplaced next to anAvatar.Fallbackis rebuilt every time the loading status changes. When the browser cache is bypassed (DevTools "Disable cache",Cache-Control: no-storeresponses), each new<img>is notcompleteright away, so the status goes back toloading, the image loads, the status becomesloaded, and the<img>is rebuilt again. The result is an endless loop: about 700loadevents per second on a single avatar in a Firefox profile, a frozen tab that ignores reload.With the cache enabled a rebuilt
<img>comes from the memory cache alreadycomplete, so the status does not flip back and the loop never starts. That is why it goes unnoticed in normal use.Cause
Avatar.Rootrenders its children as an array. Reze inserts an array that contains function slots with one shared effect: whenever any slot's dependencies change, every slot function is called again.Avatar.Fallbackreads the loading status inside its slot, so each status change re-runs the slots of all siblings. The slot returned byAvatar.Imageis created bydynamicElement, and calling it again runscreateComponentagain, which creates a new<img>.Fix
AvatarImagenow returns a slot that creates its element on the first call and returns the same element on later calls. The element is built lazily inside the slot, because creating it in the component body would write the status store during a render binding, which Reze rejects.One side effect: changing the
asprop ofAvatar.Imageafter the first render no longer swaps the element.Avatar.RootandAvatar.Fallbackstill react toasas before.Verification
loadevent the image next to a fallback is the same element. It fails onmainand passes with the fix.@rebase-ui/rezeproject: 1542 tests pass,tsc --noEmitis clean.Avatar.Root+Avatar.Image+Avatar.Fallback, countingsetAttributecalls (the counter stops at 400):loadevent, 1<img>; the same 3 calls with the cache enabled.@rebase-ui/solidis not affected. The same test (image identity afterloadnext to a fallback) passes in Chromium onsolid; its component body runs once and the element is created once, so I did not add it there.A changeset (
@rebase-ui/rezepatch) is included.