[LiveComponent] Rebuild Component on reconnect when props changed in between#3537
Open
Amoifr wants to merge 1 commit into
Open
[LiveComponent] Rebuild Component on reconnect when props changed in between#3537Amoifr wants to merge 1 commit into
Amoifr wants to merge 1 commit into
Conversation
Member
|
For 3.x, 2.x is not maintained anymore, and please follow the PR template (it's not the first time 😬) |
…between When a parent action removes and re-adds a child live component that lives on the same DOM element, Stimulus reuses the controller instance — it only calls disconnect() then connect() again, not initialize(). createComponent() is therefore never re-run, so the Component keeps a stale ValueStore that no longer matches the freshly rendered HTML, and Component.set() throws "Invalid model name" the next time the user types into a field. Detect the divergence in connect() by comparing the current propsValue with the props the Component was originally built with, and rebuild it when they differ. Behavior is unchanged on the first connect() (props match by construction) and on plain reconnects without any prop change. Fixes symfony#3424
b1b1e48 to
af195b6
Compare
Contributor
📊 Packages dist files size differenceThanks for the PR! Here is the difference in size of the packages dist files between the base branch and the PR.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Contributor
Author
|
Sorry for the slip @Kocal — rebased onto |
Contributor
Author
|
@Kocal in my defense — I catch on quick, you just have to explain things to me at length 😅 |
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.
Summary
When a parent live component action removes and re-adds a child live component on the same DOM element, Stimulus keeps the controller instance alive — it only calls
disconnect()thenconnect()again, notinitialize(). BecausecreateComponent()only runs ininitialize(), theComponent(and itsValueStore) keep the stale props from before the parent action, even though the server-rendered HTML now exposes a fresh set.The next user input then throws
Uncaught Error: Invalid model name "xyz.content"— the model name has changed, butValueStore.has()still looks up against the old props.Fix
In
connect(), compare the currentpropsValue(which Stimulus has refreshed from the livedata-live-props-valueattribute) againstcomponent.valueStore.getOriginalProps(). When they differ, rebuild theComponent.connect()afterinitialize(): props match by construction → no rebuild.The diagnosis and proposed fix are from @Pechynho in #3424 (confirmed working by another reporter in the thread).
Test plan
rebuilds the Component on reconnect when props changed in betweenexercises the bug scenario by callingcontroller.disconnect()/ mutatingpropsValue/controller.connect(), and asserts theComponentinstance has been swapped and the newValueStorereflects the fresh props.keeps the existing Component on reconnect when props are unchangedasserts that a no-op reconnect cycle does not recreate theComponent.oxlint+oxfmt --checkclean.Fixes #3424