UNPKG

@wordpress/server-side-render

Version:

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

165 lines (113 loc) 6.7 kB
# 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 ```bash 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`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._ ## API <!-- START TOKEN(Autogenerated API docs) --> ### default > **Deprecated** Use `ServerSideRender` non-default export instead. A compatibility layer for the `ServerSideRender` component when used with `wp` global namespace. _Usage_ ```js import ServerSideRender from '@wordpress/server-side-render'; ``` ### ServerSideRender A component that renders server-side content for blocks. Note: URL query will include the current post ID when applicable. This is useful for blocks that depend on the context of the current post for rendering. _Usage_ ```jsx import { ServerSideRender } from '@wordpress/server-side-render'; // Legacy import for WordPress 6.8 and earlier // import { default as ServerSideRender } from '@wordpress/server-side-render'; function Example() { return ( <ServerSideRender block="core/archives" attributes={ { showPostCounts: true } } urlQueryArgs={ { customArg: 'value' } } className="custom-class" /> ); } ``` _Parameters_ - _props_ `Object`: Component props. - _props.block_ `string`: The identifier of the block to be serverside rendered. - _props.attributes_ `Object`: The block attributes to be sent to the server for rendering. - _props.className_ `[string]`: Additional classes to apply to the wrapper element. - _props.httpMethod_ `[string]`: The HTTP method to use ('GET' or 'POST'). Default is 'GET' - _props.urlQueryArgs_ `[Object]`: Additional query arguments to append to the request URL. - _props.skipBlockSupportAttributes_ `[boolean]`: Whether to remove block support attributes before sending. - _props.EmptyResponsePlaceholder_ `[Function]`: Component rendered when the API response is empty. - _props.ErrorResponsePlaceholder_ `[Function]`: Component rendered when the API response is an error. - _props.LoadingResponsePlaceholder_ `[Function]`: Component rendered while the API request is loading. _Returns_ - `JSX.Element`: The rendered server-side content. ### useServerSideRender A hook for server-side rendering a preview of dynamic blocks to display in the editor. Handles fetching server-rendered previews for blocks, managing loading states, and automatically debouncing requests to prevent excessive API calls. It supports both GET and POST requests, with POST requests used for larger attribute payloads. _Usage_ Basic usage: ```jsx import { RawHTML } from '@wordpress/element'; import { useServerSideRender } from '@wordpress/server-side-render'; function MyServerSideRender( { attributes, block } ) { const { content, status, error } = useServerSideRender( { attributes, block, } ); if ( status === 'loading' ) { return <div>Loading...</div>; } if ( status === 'error' ) { return <div>Error: { error }</div>; } return <RawHTML>{ content }</RawHTML>; } ``` _Parameters_ - _args_ `Object`: The hook configuration object. - _args.attributes_ `Object`: The block attributes to be sent to the server for rendering. - _args.block_ `string`: The identifier of the block to be serverside rendered. Example: 'core/archives'. - _args.skipBlockSupportAttributes_ `[boolean]`: Whether to remove block support attributes before sending. - _args.httpMethod_ `[string]`: The HTTP method to use ('GET' or 'POST'). Default is 'GET'. - _args.urlQueryArgs_ `[Object]`: Additional query arguments to append to the request URL. _Returns_ - `ServerSideRenderResponse`: The server-side render response object. <!-- END TOKEN(Autogenerated API docs) --> ## 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. ```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](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) 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](https://github.com/WordPress/gutenberg/tree/HEAD/CONTRIBUTING.md). <br /><br /><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>