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

Update ServerSideRender dependencies in the dynamic blocks documentation #12344

Closed
pojke opened this issue Nov 27, 2018 · 11 comments
Closed

Update ServerSideRender dependencies in the dynamic blocks documentation #12344

pojke opened this issue Nov 27, 2018 · 11 comments
Labels
Good First Issue An issue that's suitable for someone looking to contribute for the first time Needs Dev Ready for, and needs developer efforts [Package] Server Side Render /packages/server-side-render [Type] Developer Documentation Documentation for developers

Comments

@pojke
Copy link

pojke commented Nov 27, 2018

There seems to be some issue with documentation and recent update. It works in v.4.4.0 but not since 4.5.0

Here's proof of concept. It either needs documentation improvement or it's a bug.

Following: https://wordpress.org/gutenberg/handbook/blocks/creating-dynamic-blocks/ you should be able to create a simple block.

index.php

/*
  Plugin Name:  TEST
  Plugin URI:   www.google.com
  Description:  Demo
  Version:      1
  Author:       Ralf Klis
  Author URI:
  License:      GPL2
  License URI:  https://www.gnu.org/licenses/gpl-2.0.html
  Text Domain:
 */

function my_plugin_render_block_latest_post($attributes, $content) {
    $recent_posts = wp_get_recent_posts(array(
        'numberposts' => 1,
        'post_status' => 'publish',
    ));
    if (count($recent_posts) === 0) {
        return 'No posts';
    }
    $post    = $recent_posts[0];
    $post_id = $post['ID'];
    return sprintf(
            '<a class="wp-block-my-plugin-latest-post" href="%1$s">%2$s</a>', esc_url(get_permalink($post_id)), esc_html(get_the_title($post_id))
    );
}

function gutenberg_boilerplate_block() {
    wp_register_script(
            'latest-post', plugins_url('latest-post/block.js', __FILE__), array('wp-blocks', 'wp-element')
    );
//    echo plugins_url('latest-post/block.js', __FILE__);exit();
    register_block_type('my-plugin/latest-post', array(
        'editor_script'   => 'latest-post',
        'render_callback' => 'my_plugin_render_block_latest_post'
    ));
}

add_action('init', 'gutenberg_boilerplate_block');

latest-post/block.js

var el = wp.element.createElement,
    registerBlockType = wp.blocks.registerBlockType,
    ServerSideRender = wp.components.ServerSideRender;

registerBlockType( 'my-plugin/latest-post', {
    title: 'Latest Post TEST',
    icon: 'megaphone',
    category: 'widgets',

    edit: function( props ) {
        // ensure the block attributes matches this plugin's name
        return (
            el(ServerSideRender, {
                block: "my-plugin/latest-post",
                attributes:  props.attributes
            })
        );
    },

    save: function() {
        // Rendering in PHP
        return null;
    },
} );

This worked in 4.4 but now produces block.js?ver=4.9.8:5 Uncaught TypeError: Cannot read property 'ServerSideRender' of undefined

There's something missing in documentation what's changed between versions.

A quick workaround, which is silly, is to attach the empty JS file dependent on block. This fixes the problem if added on the very bottom of index.php file

function load_gutenberg_scripts() {
    wp_enqueue_script(
            'random-error', plugins_url('/latest-post/empty.js', __FILE__), ['wp-i18n', 'wp-blocks', 'wp-edit-post', 'wp-element', 'wp-editor', 'wp-components', 'wp-data', 'wp-plugins', 'wp-edit-post', 'wp-api'], '0.1'
    );
}

add_action('enqueue_block_assets', 'load_gutenberg_scripts');
@pojke
Copy link
Author

pojke commented Nov 27, 2018

It is most likely related to #12057

@designsimply designsimply added [Package] Server Side Render /packages/server-side-render Needs Technical Feedback Needs testing from a developer perspective. labels Nov 27, 2018
@adekherry
Copy link

Hi,

I also have a similar issue, but I'm using es6. Previously i need to replace const { ServerSideRender } = wp.components; with import { ServerSideRender } from '@wordpress/components';.

But I get an error on the console log that saying the API endpoint for my custom block not registered (error 404). It's related to #12057. Then I notice there's something wrong on the API endpoint. I get something like this http://localhost/wp/v2/block-renderer/. With v3 I get something like this http://localhost/project/wp-json/wp/v2/block-renderer/ which is working fine.

Anyone, please help. Thx

@swissspidy
Copy link
Member

Your script is missing the wp-components dependency. That‘s why wp.components is undefined.

Your workaround only works because it loads all sorts of dependencies, including wp-components.

The rule of thumb is: if you want to access wp.* in JS, add wp-* as a dependency to your JS.

@pojke
Copy link
Author

pojke commented Nov 27, 2018

Thanks, but the key is that the code is copy and paste from an official handbook.

@swissspidy
Copy link
Member

https://wordpress.org/gutenberg/handbook/blocks/creating-dynamic-blocks/ doesn‘t show how to register/enqueue the JS, so which page do you feel should be improved?

@pojke
Copy link
Author

pojke commented Nov 27, 2018

Exactly the one you quoted that starts with “The following code example shows how to create the latest post block dynamic block.”
Then you copy and paste the “following code” and it does work. And then 4.5 update comes in and it doesn’t anymore.

@Otshelnik-Fm
Copy link

https://wordpress.org/gutenberg/handbook/blocks/creating-dynamic-blocks/ doesn‘t show how to register/enqueue the JS, so which page do you feel should be improved?

Oops! That page can’t be found

@chrisvanpatten
Copy link
Contributor

@Otshelnik-Fm https://github.com/WordPress/gutenberg/blob/master/docs/designers-developers/developers/tutorials/block-tutorial/creating-dynamic-blocks.md

The block tutorial got mistakenly removed from the handbook on WordPress.org so there's the direct link. Working on getting it back :)

@roguewdesign
Copy link

As @swissspidy mentioned, you need to include the 'wp-components' dependency:

wp_register_script('your-custom-block-js', 'your-file-directory/blocks.js', array( 'wp-blocks', 'wp-element', 'wp-components' ) );

Make sure to include the 'wp-components' when registering your block.js file.

This fixed the issue, but the documentation page does need updating.

@bobbingwide
Copy link
Contributor

Since PR 8720 ServerSideRender comes from wp.editor not wp.components. Docs need updating.

@youknowriad youknowriad added [Type] Developer Documentation Documentation for developers Good First Issue An issue that's suitable for someone looking to contribute for the first time and removed Needs Technical Feedback Needs testing from a developer perspective. labels Apr 22, 2019
@youknowriad youknowriad changed the title Cannot read property 'ServerSideRender' of undefined problem since v4.5 Update ServerSideRender dependencies in the dynamic blocks documentation Apr 22, 2019
@gziolo gziolo added the Needs Dev Ready for, and needs developer efforts label Oct 21, 2019
@skorasaurus
Copy link
Member

ServerSideRender had been moved to its own package since the last time this issue was commented on; the dynamic blocks documentation regarding this was updated in #19012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good First Issue An issue that's suitable for someone looking to contribute for the first time Needs Dev Ready for, and needs developer efforts [Package] Server Side Render /packages/server-side-render [Type] Developer Documentation Documentation for developers
Projects
None yet
Development

No branches or pull requests