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

Best approach for dynamic block rendering in the editor #28684

Open
tellthemachines opened this issue Feb 3, 2021 · 4 comments
Open

Best approach for dynamic block rendering in the editor #28684

tellthemachines opened this issue Feb 3, 2021 · 4 comments

Comments

@tellthemachines
Copy link
Contributor

@tellthemachines tellthemachines commented Feb 3, 2021

What problem does this address?

Currently, core dynamic blocks use one of two different approaches to rendering in the editor:

  • server-side rendering with ServerSideRender, which reproduces the markup defined in the block's index.php (e.g. Archives, Latest Comments)
  • client-side rendering with the REST API and reconstructing the markup defined in index.php in JS. (e.g. Categories, Latest Posts)

The developer docs recommend client-side rendering:

Server-side render is meant as a fallback; client-side rendering in JavaScript is always preferred (client rendering is faster and allows better editor manipulation)

However, it's not clear that client-side rendering does in fact work better:

  • Performance-wise, blocks using ServerSideRender load faster than blocks using JS to render the same amount of content.
  • Duplication of markup between PHP and JS files makes things messy to maintain and error-prone.

What is your proposed solution?

I'd love to see us leverage ServerSideRender on all our dynamic blocks, but in order to do so we'll need to improve it:

  • It shouldn't have to wrap its content in an extra piece of markup;
  • It should be able to work with block context (this will also require changes to the REST API endpoint)

Thoughts and ideas welcome!

(This issue was prompted by work on #28265 , where I opted to use to use ServerSideRender but ran into the issue of not being able to use block context to customise markup output.)

@mtias
Copy link
Contributor

@mtias mtias commented Feb 3, 2021

We tend to recommend client-blocks because it's intrinsic to how the editor is meant to work: direct manipulation with instant feedback for all manipulations, which we can only ensure properly client side. ServerSideRender should generally be avoided outside of very naive cases that won't require much interaction or are supposed to work as embeds (tag cloud comes to mind). I would probably not recommend it for the pages block because the moment we start supporting broader customization tools (typography, colors, sizing, possibly reordering, etc) the roundtrip becomes a hindrance to the UX. Server-side blocks are also not well-suited for the mobile apps.

@gziolo
Copy link
Member

@gziolo gziolo commented Feb 4, 2021

What if one day we would have React Server Components implemented in PHP? 😄

I guess it's far in the future to see it enabled in React, but they are working on the serialization layer so, in theory, it could work with any language. As far as I understood the idea, it would address most of the concerns with the code duplication between Edit in JS and save in PHP. The use case for the mobile apps is probably more difficult to address though.

@mtias
Copy link
Contributor

@mtias mtias commented Feb 4, 2021

The streaming protocol would indeed be cool to explore given we already have the block abstraction established. It wouldn't be too out there to consider passing component state through it. We briefly discussed this with @luisherranz in the context of Frontity and how it could consume blocks in the front-end in the future.

But that also points to another important distinction of server-client, which is edit() is meant to tailor its component for editing / interaction while server || save() is meant to render the best representation for the front-end. We don't have the concept of edit on the server.

@gziolo
Copy link
Member

@gziolo gziolo commented Feb 4, 2021

But that also points to another important distinction of server-client, which is edit() is meant to tailor its component for editing / interaction while server || save() is meant to render the best representation for the front-end. We don't have the concept of edit on the server.

This is a good reminder. It's easy to fall into a trap of wanting to share code between edit and save. We often see reports that React hooks don't work with save on the client. This is a good thing as it helps to understand that save isn't interactive and only translates components into HTML output. It's all interesting, but also extremely hard to communicate to the folks writing blocks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants