bobbingwide / oik-blocks

WordPress 5.0 blocks for oik shortcodes
https://oik-plugins.com/oik-plugins/oik-blocks
GNU General Public License v3.0
1 stars 0 forks source link

Experiment with ServerSideRender #16

Closed bobbingwide closed 2 years ago

bobbingwide commented 5 years ago

Gutenberg provides a mechanism called ServerSideRender which allows a block to be rendered by the server. It’s intended for legacy code, which is exactly what some of the shortcodes are. We should experiment with this solution to attempt to render the real block that’s displayed for a subset of shortcodes. Starting with:

Block Shortcode Consideration
Oik-block/wp wp attributes: v, m, p
oik-block/geshi bw_geshi Generic Syntax Highlighting for lang=none,css,php,html,javascript,jquery
Oik-block/search bw_search
Oik-block/login bw_login consider protected content
Oik-block/list bw_list many attributes!
Oik-block/googlemap bw_show_googlemap needs to cater for Google’s JavaScript

For some logic the server side rendering may require the global $post to be set. Other logic needs to know the current user.

bobbingwide commented 5 years ago

See #12 for a summary of required blocks.

bobbingwide commented 5 years ago

While developing the Fields block I discovered that the version of ServerSideRender that passes the post_id to the REST API is in wp.editor; not wp.components as currently documented.

The code was changed in PR 8720, but there don't appear to be any deprecation notices. Each block developed so far should be updated.

Perhaps there should be a summary of where to import components from.

bobbingwide commented 5 years ago

If you register a Server Side Rendered block with

supports: { 
customClassName: true, 
className: true }

then you have to ensure that these fields are registered in the PHP. If you don't then you'll get Error loading block: Invalid parameter(s): attributes when you attempt to complete the Additional CSS Class field.

The problem only occurs in the editor. When the post is displayed there isn't any checking.

Need to confirm what in the client end sets customClassName.

Also, it's not a good idea to set supports.html: true for this type of block.

bobbingwide commented 2 years ago

Closing this. I've developed a number of blocks that use Server Side Rendering. I experienced quite a few problems along the way. But I reckon I've got it sussed now. See https://github.com/bobbingwide/oik-bob-bing-wide/issues/42