Skip to content

[Live] Always send HTML over the wire instead of JSON #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2022

Conversation

weaverryan
Copy link
Member

Q A
Bug fix? yes (one part of it is)
New feature? yes
Tickets None
License MIT

Hi!

Currently, the LiveComponent controller expects to get JSON back from the server, containing { data: ..., html: '...' }. But, Stimulus is so cool, that this isn't necessary! The LiveComponent Ajax requests can simply return HTML in all cases. The rendered element will have an updated data-live-data-value attribute with the latest data. And so, the Stimulus component instantly reads that updated data and incorporates it: there is no need to send back the "component data" on a separate key in the JSON.

This makes the live component system more boring in a good way: it's just a way to return HTML (and then we have fancy JavaScript that adds all the interactivity). It's also a bit faster now, as previously we were dehydrating the component data twice on re-render.

I can't think of any downsides to this approach - everything seems to work just fine.

Cheers!

}

throw new Error(`The update() method could not be called for "${clonedElement.outerHTML}": the element must either have a "data-value" or "value" attribute set.`);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated bug fix from a feature I merged recently #203. That PR added this check, but it IS ok to have an empty value - it just means, for example, that a textarea is empty! For some reason, the tests didn't catch this before, but after this change, one test DID start failing.

Copy link
Member

@kbond kbond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this simplifies things a lot!

@weaverryan weaverryan force-pushed the use-html-across-wire branch from 28ff20e to f18d797 Compare January 25, 2022 14:05
@weaverryan weaverryan merged commit 48e8f84 into symfony:2.x Jan 25, 2022
@norkunas norkunas mentioned this pull request Feb 1, 2022
kbond added a commit that referenced this pull request Feb 1, 2022
This PR was merged into the 2.x branch.

Discussion
----------

Rebuild js dist files

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| Tickets       | N/A
| License       | MIT

Tried to update my project deps to use 2-x.dev of LiveComponent, but now that renderer returns HTML instead of JSON, it fails, because the LiveController still uses JSON.

This rebuilds dist files from #226, #245, #247 changes.

Commits
-------

6906cc6 Rebuild js dist files
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.

2 participants