-
Notifications
You must be signed in to change notification settings - Fork 10.9k
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
Passing an Eloquent Model to an anonymous component results in expensive toJson call #39232
Comments
I have similar issues in an application I'm working on. The blade view somehow calls public function toArray()
{
return [];
} |
I'll ask Taylor to have a look at this. Thanks. |
PRs welcome. |
This may be a dumb question -- but do we need to sanitize objects with anonymous components? Here the compiler is outputting the sanitization of the object, causing the serialization, but the serialized post is not actually being output inside of the HTML, it's just being passed to the component bag so its attributes can be retrieved using framework/src/Illuminate/View/Compilers/ComponentTagCompiler.php Lines 594 to 603 in 024067f
Rendered: <?php $component->withAttributes(['post' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute($post)]); ?> Unless the post's properties will be output in the case of the below blade (I also lack enough knowledge to know for sure): <h1 {{ $attributes }}></h1> Maybe the I.e.: <?php $component->withProps(['post' => $post]); ?> |
Potential fix PR here: #39319 |
Description
This blog post explains the issue nicely, so I'm basically just copying it here. Hope you don't mind, @hbgl!
Take a look at this anonymous component:
And here is how it is used in, say, a post list page:
In this example, the posts are Eloquent models that were previously loaded from the database. Now, if you run this code, you will notice nothing strange in particular. Everything works as intended. However, if you dig a little deeper, you will find some unexpected behavior:
Every time the component is instantiated, the post model is being serialized to JSON.
The cause for this behavior lies in the way the Blade component compiler processes attributes inside the
sanitizeComponentAttribute
function.Basically, any object that implements a
__toString
function will be converted to a string. In the case of an Eloquent model, this will convert it to JSON. Depending on how large your model is and how often the component is used, this will waste a significant amount of resources on constructing JSON strings that are never used.In the blog post, @hbgl goes on to describe a solution: use a class based component and add your props to the constructor arguments. After you do that, your objects will no longer be converted to strings.
However, I think this bug is worth fixing. In my project I use a lot of anonymous components and my app's performance is suffering pretty significantly from this bug. While I may well go down the road of converting them into classes, I think this bug really limits the real-world utility and scalability of anonymous components.
I don't have a good solution in mind – my understanding of the internals here is too limited. But I do wonder, since props are defined at the top of each anonymous component's template, is there some way to read those in advance and then prevent them from being sanitized and passed into the AttributeBag?
Note: This issue is not the same as #34346, where props being passed into dynamic components were being HTML-encoded. This issue is about anonymous components and still persists.
The text was updated successfully, but these errors were encountered: