-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
ServerSideRender does not handle usesContext #34882
Comments
👋 @sc0ttkclark ! I'm not sure I understand your use case and the challenges you face. Can you expand a bit more? In general here you can find some information of how Did you mean how to access the code in the Happy to help if you have any questions 😄 |
Hey @ntsekouras, what's up! Thanks for getting back to me about this. For clarification, I have a block that This gets tricky when you use the Query Loop block, which can manipulate postId and postType for it's purposes (yay!) but that can't be used from the REST API preview context :( |
One more point to make sure it was clear -- if Using the |
I can see how it could get missed when the block context was introduced. I think the solution proposed is worth exploring. Looking at https://developer.wordpress.org/rest-api/reference/rendered-blocks/, I see that |
What do you think about |
In the REST API endpoint for block types, it's In this case, it isn't really I'm not sure how much work it would require to override |
I like this approach more. The overriding controller would mean supporting it until the plugin requires WP 5.9. I'll try to create both PRs next week if there are no other takers. |
All this sounds great to me, for now I'm using my own custom URL argument that I look for in the REST API, in the future I will switch that to the new param name (and in a future WP version I can remove that custom handling). |
What problem does this address?
I have a block that supports usesContext and it works if you're dealing with client-side rendering.
For server side rendering via
ServerSideRender
-- that's where things drop off. When rendering the block on the frontend (output) it will receive context fine. However, the context is never passed into the REST API endpoint to receive the preview.I have to manually build and pass this in myself.
Here's an example of what the workaround right now is (simplified for example purposes).
What is your proposed solution?
I think a good solution would be to add another attributes-like optional argument
context={ context }
toServerSideRender
, to pass in the context if you received it / use it from your edit component.This would also need to include additional code in the REST API endpoint
WP_REST_Block_Renderer_Controller
so that it can support the new passed context information. Currently, it uses acontext
param in the endpoint to denote view vs edit. So this would need to be a new param name likecontextValues
and accept an array, similar to how theattributes
param works.All of this would give block authors more flexibility with their server side rendered blocks than they have right now.
An example implementation of the
ServerSideRender
usage would be like this:The text was updated successfully, but these errors were encountered: