npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wordpress/server-side-render

v4.33.0

Published

The component used with WordPress to server-side render a preview of dynamic blocks to display in the editor.

Downloads

198,627

Readme

ServerSideRender

ServerSideRender is a component used for server-side rendering a preview of dynamic blocks to display in the editor. Server-side rendering in a block's edit function should be limited to blocks that are heavily dependent on existing PHP rendering logic that is heavily intertwined with data, particularly when there are no endpoints available.

ServerSideRender may also be used when a legacy block is provided as a backward compatibility measure, rather than needing to re-write the deprecated code that the block may depend on.

ServerSideRender should be regarded as a fallback or legacy mechanism, it is not appropriate for developing new features against.

New blocks should be built in conjunction with any necessary REST API endpoints, so that JavaScript can be used for rendering client-side in the edit function. This gives the best user experience, instead of relying on using the PHP render_callback. The logic necessary for rendering should be included in the endpoint, so that both the client-side JavaScript and server-side PHP logic should require a minimal amount of differences.

This package is meant to be used only with WordPress core. Feel free to use it in your own project but please keep in mind that it might never get fully documented.

Installation

Install the module

npm install @wordpress/server-side-render --save

This package assumes that your code will run in an ES2015+ environment. If you're using an environment that has limited or no support for such language features and APIs, you should include the polyfill shipped in @wordpress/babel-preset-default in your code.

Usage

The props accepted by the component are described below.

Props

attributes

An object containing the attributes of the block to be server-side rendered. E.g: { displayAsDropdown: true }, { showHierarchy: true }, etc...

  • Type: Object
  • Required: No

block

The identifier of the block to be server-side rendered. Examples: "core/archives", "core/latest-comments", "core/rss", etc...

  • Type: String
  • Required: Yes

className

A class added to the DOM element that wraps the server side rendered block. Examples: "my-custom-server-side-rendered".

  • Type: String
  • Required: No

httpMethod

The HTTP request method to use, either 'GET' or 'POST'. It's 'GET' by default. The 'POST' value will cause an error on WP earlier than 5.5, unless 'rest_endpoints' is filtered in PHP to allow this. If 'POST', this sends the attributes in the request body, not in the URL. This can allow a bigger attributes object.

  • Type: String
  • Required: No
  • Default: 'GET'

Example:

function add_rest_method( $endpoints ) {
    if ( is_wp_version_compatible( '5.5' ) ) {
        return $endpoints;
    }

    foreach ( $endpoints as $route => $handler ) {
        if ( isset( $endpoints[ $route ][0] ) ) {
            $endpoints[ $route ][0]['methods'] = [ WP_REST_Server::READABLE, WP_REST_Server::CREATABLE ];
        }
    }

    return $endpoints;
}
add_filter( 'rest_endpoints', 'add_rest_method');

skipBlockSupportAttributes

Remove attributes and style properties applied by the block supports. This prevents duplication of styles in the block wrapper and the ServerSideRender components. Even if certain features skip serialization to HTML markup by __experimentalSkipSerialization, all attributes and style properties are removed.

  • Type: Boolean
  • Required: No
  • Default: false

urlQueryArgs

Query arguments to apply to the request URL. E.g: { post_id: 12 }.

  • Type: Object
  • Required: No

EmptyResponsePlaceholder

The component is rendered when the API response is empty. The component will receive the value of the API response, and all props passed into ServerSideRenderer.

  • Type: Component
  • Required: No

ErrorResponsePlaceholder

The component is rendered when the API response is an error. The component will receive the value of the API response, and all props passed into ServerSideRenderer.

  • Type: Component
  • Required: No

LoadingResponsePlaceholder

The component is rendered while the API request is being processed (loading state). The component will receive the value of the API response, and all props passed into ServerSideRenderer.

  • Type: Component
  • Required: No

Example usage

const MyServerSideRender = () => (
	<ServerSideRender LoadingResponsePlaceholder={ MyAmazingPlaceholder } />
);

Usage

Render core/archives preview.

import ServerSideRender from '@wordpress/server-side-render';

const MyServerSideRender = () => (
	<ServerSideRender
		block="core/archives"
		attributes={ {
			showPostCounts: true,
			displayAsDropdown: false,
		} }
	/>
);

If imported from the wp global, an alias is required to work in JSX.

const { serverSideRender: ServerSideRender } = wp;

const MyServerSideRender = () => (
	<ServerSideRender
		block="core/archives"
		attributes={ {
			showPostCounts: true,
			displayAsDropdown: false,
		} }
	/>
);

Output

Output uses the block's render_callback function, set when defining the block.

API Endpoint

The API endpoint for getting the output for ServerSideRender is /wp/v2/block-renderer/:block. It will use the block's render_callback method.

If you pass attributes to ServerSideRender, the block must also be registered and have its attributes defined in PHP.

register_block_type(
	'core/archives',
	array(
		'api_version' => 3,
		'attributes'      => array(
			'showPostCounts'    => array(
				'type'      => 'boolean',
				'default'   => false,
			),
			'displayAsDropdown' => array(
				'type'      => 'boolean',
				'default'   => false,
			),
		),
		'render_callback' => 'render_block_core_archives',
	)
);

Contributing to this package

This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.

To find out more about contributing to this package or Gutenberg as a whole, please read the project's main contributor guide.