-
-
Notifications
You must be signed in to change notification settings - Fork 375
[LiveComponent] Support JSONResponse for LiveActions #2967
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
base: 2.x
Are you sure you want to change the base?
[LiveComponent] Support JSONResponse for LiveActions #2967
Conversation
📊 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.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion!
Here some majors comments that must be addressed, the actual implementation is partially broken and must be fixed.
Also, please ensure to add tests PHP-side and JS-side, as we want to be sure to not break anything.
Could you also add documentation about returning a custom Response
and JsonResponse
as well?
Thanks!
After some time to think, here are my « at this moment » thoughts
as much I understand the précise need you are dealing with right now, I am really Not sure this is direction we should go to. LiveComponent is already complex for many, and if I agree we need to open more extension point, they should be only that and we cannot make promise or deal with BC afterward for them implémentation. On the contrary I think — and i join you here — we must adress more or the « basic / regular » needs people using LiveComponent have. Including but not limited to component lifecycle (delete, update, refresh), identification, partial rendering or no render, locking or sleep, etc) But for all this we need, imho, to stop stacking features and more keep a global / easy to understand philosophy here that would ease both maintenance and new feature dev, improve code quality testability and security. all of this is not directly related to this PR but it is something I wanted to write down as a block for some time, in order to give clear vision of my personal state of mind here Again: this is my own personal opinion right now and am more than open to any feedback / critique or exchange on it. |
It would be great if we could learn about the features planned for version 3.0. For example, I'm considering implementing something similar to what Livewire provides — like custom error handlers. Here's a use case: window.LiveComponent.onError = (response, statusCode, component) => {
if (statusCode === 500) {
console.error('Server error in component:', component.name);
return true; // continue with default error handling
}
return true;
}; Another example relates to dynamic behavior based on responses. While it's possible to attach a Stimulus controller to a component's root element, sometimes we need to react programmatically to the result of a LiveAction — and based on that result, decide whether to re-render the component or not. In such cases, it would be very useful to have the ability to disable rendering from within a JsonResponse inside a LiveAction method, allowing us to use the returned data on the frontend without triggering a component re-render. |
Summary
This PR improves the LiveComponent JavaScript request handling to correctly distinguish between standard HTML LiveComponent responses and pure JSON API responses from LiveAction methods.
Specifically, it prevents error rendering in the frontend when a LiveAction returns a
JsonResponse
(such as for AJAX-like API interactions) by adding an additional check for theapplication/json
content type in the response headers.Problem
When using LiveComponent actions that return
JsonResponse
(e.g., for API-like operations), the LiveComponent JS handler previously attempted to render an error if the response did not contain a LiveComponent HTML payload.This caused issues in cases where a component intentionally returns JSON (for example, on deletion or API-style actions), leading to a confusing user experience.
Solution
performRequest
method in the LiveComponent JS handler to also check forapplication/json
in the response'sContent-Type
header before triggering error rendering.Example Usage
Stimulus Controller
Twig Example
LiveComponent Example