Skip to content
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

[4.x] Prevent serialization errors with@nocache directive when using Blade view components #9409

Merged

Conversation

duncanmcclean
Copy link
Member

@duncanmcclean duncanmcclean commented Jan 26, 2024

This pull request fixes a serialization error with the @nocache directive when using a Blade view component with a custom method.

This PR will evaluate any custom methods in Blade view components.

Fixes #8203.
Related: #6935

Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

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

Filtering out the variable prevents the error, but you are no longer able to use it.

// Layout.php

public function custom()
{
  return 'my custom var';
}
{{-- layout.blade.php --}}
@include('mypartial')
{{-- mypartial.blade.php --}}
{{ $custom }} // outputs "my custom var"

I used @include instead of @nocache there to show that it's supposed to work. If you use @nocache, without this PR it'll throw that serialization error, with this PR it'll throw undefined variable $custom.

I think instead of filtering them out, we should evaluate them. Then we can store the evaluated version in the context.

@duncanmcclean duncanmcclean changed the title [4.x] Filter out InvokableComponentVariable from nocache context [4.x] Prevent serialization errors with@nocache directive when using Blade view components Jan 29, 2024
@duncanmcclean
Copy link
Member Author

@jasonvarga Oops, I thought I had it working previously but I see the issue now. 🙈

I've updated this PR so it'll evaluate the values instead. I've also updated the PR title & description to better represent the changes.

@jasonvarga jasonvarga merged commit 2d58efb into 4.x Jan 30, 2024
37 checks passed
@jasonvarga jasonvarga deleted the fix/nocache-serialization-with-custom-blade-components branch January 30, 2024 21:36
@kgirzadas
Copy link
Contributor

Before this change, in partials included using @nocache directive, I was accessing page data through $page variable (e.g. $page->some_field->handle()). After these changes, it no longer works because everything in the $page variable is converted to an array. Was this the expected effect?

@jasonvarga
Copy link
Member

Was this the expected effect?

No, but I believe #9449 should resolve that.

@kgirzadas
Copy link
Contributor

Was this the expected effect?

No, but I believe #9449 should resolve that.

I can confirm that the fix resolves the mentioned issue.

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.

Serialization of Closure is not allowed - Blade layout component
3 participants