Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/manifest-devhub.json
Original file line number Diff line number Diff line change
Expand Up @@ -899,12 +899,6 @@
"markdown_source": "../packages/components/src/select-control/README.md",
"parent": "components"
},
{
"title": "ServerSideRender",
"slug": "server-side-render",
"markdown_source": "../packages/components/src/server-side-render/README.md",
"parent": "components"
},
{
"title": "SlotFill",
"slug": "slot-fill",
Expand Down Expand Up @@ -1361,6 +1355,12 @@
"markdown_source": "../packages/scripts/README.md",
"parent": "packages"
},
{
"title": "@wordpress/server-side-render",
"slug": "packages-server-side-render",
"markdown_source": "../packages/server-side-render/README.md",
"parent": "packages"
},
{
"title": "@wordpress/shortcode",
"slug": "packages-shortcode",
Expand Down
15 changes: 15 additions & 0 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ function gutenberg_register_scripts_and_styles() {
)
);

// Add back compatibility for calls to wp.components.ServerSideRender.
wp_add_inline_script(
'wp-server-side-render',
implode(
"\n",
array(
'( function() {',
' if ( wp && wp.components && wp.serverSideRender && ! wp.components.ServerSideRender ) {',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An issue with this is if wp-components is enqueued after wp-server-side-render, it will never be shimmed. We could potentially resolve this by defining wp-components as a dependency of wp-server-side-render (optionally checking that it's at least registered).

' wp.components.ServerSideRender = wp.serverSideRender;',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a component, the capitalization of serverSideRender is wrong. It should be ServerSideRender?

' };',
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The semi-colon here is unnecessary.

'} )();',
)
)
);

// Editor Styles.
// This empty stylesheet is defined to ensure backward compatibility.
gutenberg_override_style( 'wp-blocks', false );
Expand Down
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@wordpress/priority-queue": "file:packages/priority-queue",
"@wordpress/redux-routine": "file:packages/redux-routine",
"@wordpress/rich-text": "file:packages/rich-text",
"@wordpress/server-side-render": "file:packages/server-side-render",
"@wordpress/shortcode": "file:packages/shortcode",
"@wordpress/token-list": "file:packages/token-list",
"@wordpress/url": "file:packages/url",
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
"@wordpress/keycodes": "file:../keycodes",
"@wordpress/server-side-render": "file:../server-side-render",
"@wordpress/viewport": "file:../viewport",
"classnames": "^2.2.5",
"fast-average-color": "4.3.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/archives/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { InspectorControls } from '@wordpress/block-editor';
import { ServerSideRender } from '@wordpress/editor';
import ServerSideRender from '@wordpress/server-side-render';

export default function ArchivesEdit( { attributes, setAttributes } ) {
const { showPostCounts, displayAsDropdown } = attributes;
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/latest-comments/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
RangeControl,
ToggleControl,
} from '@wordpress/components';
import { ServerSideRender } from '@wordpress/editor';
import ServerSideRender from '@wordpress/server-side-render';
import { Component } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/legacy-widget/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
BlockControls,
InspectorControls,
} from '@wordpress/block-editor';
import { ServerSideRender } from '@wordpress/editor';
import ServerSideRender from '@wordpress/server-side-render';

/**
* Internal dependencies
Expand Down
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

- Added missing documentation for `DropdownMenu` props `menuLabel`, `position`, `className`.

### Breaking Change

- `ServerSideRender` is no longer part of components. It was extracted to an independent package `@wordpress/server-side-render`.


## 7.4.0 (2019-05-21)

Expand Down
1 change: 0 additions & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"dependencies": {
"@babel/runtime": "^7.4.4",
"@wordpress/a11y": "file:../a11y",
"@wordpress/api-fetch": "file:../api-fetch",
Comment thread
jorgefilipecosta marked this conversation as resolved.
"@wordpress/compose": "file:../compose",
"@wordpress/dom": "file:../dom",
"@wordpress/element": "file:../element",
Expand Down
1 change: 0 additions & 1 deletion packages/components/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export { default as SelectControl } from './select-control';
export { default as Snackbar } from './snackbar';
export { default as SnackbarList } from './snackbar/list';
export { default as Spinner } from './spinner';
export { default as ServerSideRender } from './server-side-render';
Comment thread
jorgefilipecosta marked this conversation as resolved.
export { default as TabPanel } from './tab-panel';
export { default as TextControl } from './text-control';
export { default as TextareaControl } from './textarea-control';
Expand Down
3 changes: 3 additions & 0 deletions packages/editor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### Deprecations
- The `getAutosave`, `getAutosaveAttribute`, and `hasAutosave` selectors are deprecated. Please use the `getAutosave` selector in the `@wordpress/core-data` package.
- The `resetAutosave` action is deprecated. An equivalent action `receiveAutosaves` has been added to the `@wordpress/core-data` package.
- `ServerSideRender` component was deprecated. The component is now available in `@wordpress/server-side-render`.



### Internal

Expand Down
1 change: 1 addition & 0 deletions packages/editor/src/components/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,4 @@ export {
withColors,
withFontSizes,
};
export { default as ServerSideRender } from '@wordpress/server-side-render';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a deprecated call, when using it from this location?

Copy link
Copy Markdown
Contributor

@mcsf mcsf Mar 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just ran into this while writing a plugin. Because wp.components.ServerSideRender is available with no deprecation warnings, it's easy to think that this is still the canonical place for that component.

The other thing is that the wp-components module hasn't listed wp-server-side-render as a dependency, so as a plugin author I have to know that my plugin depends on wp-server-side-render on top of the rest. Finally, we are in a strange situation now because those two modules are interdependent. I believe that, going forward, the only proper solution is to remove ServerSideRender from components's deprecated components.

Of course, before that is possible, we should at least wrap wp.components.ServerSideRender so that it logs a deprecation warning.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this line was about wp-editor and I missed that. I guess it speaks to the general confusion. :) Never mind the bit about mutual dependencies, then.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 change: 0 additions & 1 deletion packages/editor/src/components/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Block Creation Components
export * from './autocompleters';
export { default as ServerSideRender } from './server-side-render';

// Post Related Components
export { default as AutosaveMonitor } from './autosave-monitor';
Expand Down
3 changes: 0 additions & 3 deletions packages/editor/src/components/server-side-render/README.md

This file was deleted.

18 changes: 0 additions & 18 deletions packages/editor/src/components/server-side-render/index.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/server-side-render/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
5 changes: 5 additions & 0 deletions packages/server-side-render/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Unreleased

### Enhancements

- Extracted the package from `@wordpress/components` and `@wordpress/editor`;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ ServerSideRender should be regarded as a fallback or legacy mechanism, it is not

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.
Comment thread
jorgefilipecosta marked this conversation as resolved.

> 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 ES2015+ such as lower versions of IE then using [core-js](https://github.com/zloirock/core-js) or [@babel/polyfill](https://babeljs.io/docs/en/next/babel-polyfill) will add support for these methods. Learn more about it in [Babel docs](https://babeljs.io/docs/en/next/caveats)._

## Usage
Comment thread
jorgefilipecosta marked this conversation as resolved.

The props accepted by the component are described below.


## Props

### attributes
Expand Down Expand Up @@ -43,7 +60,7 @@ E.g: `{ post_id: 12 }`.
Render core/archives preview.

```jsx
import { ServerSideRender } from '@wordpress/components';
import { ServerSideRender } from '@wordpress/server-side-render';
Comment thread
jorgefilipecosta marked this conversation as resolved.

const MyServerSideRender = () => (
<ServerSideRender
Expand Down
37 changes: 37 additions & 0 deletions packages/server-side-render/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@wordpress/server-side-render",
"version": "1.0.0-alpha.1",
"description": "The component used with WordPress to server-side render a preview of dynamic blocks to display in the editor.",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"keywords": [
"wordpress",
"server-side",
"render"
],
"homepage": "https://github.com/WordPress/gutenberg/tree/master/packages/server-side-render/README.md",
"repository": {
"type": "git",
"url": "https://github.com/WordPress/gutenberg.git",
"directory": "packages/server-side-render"
},
"bugs": {
"url": "https://github.com/WordPress/gutenberg/issues"
},
"main": "build/index.js",
"module": "build-module/index.js",
"react-native": "src/index",
"dependencies": {
"@babel/runtime": "^7.4.4",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/components": "file:../components",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wordpress/api-fetch is missing on the list of dependencies.

"@wordpress/data": "file:../data",
"@wordpress/element": "file:../element",
"@wordpress/i18n": "file:../i18n",
"@wordpress/url": "file:../url",
"lodash": "^4.17.11"
},
"publishConfig": {
"access": "public"
}
}
46 changes: 46 additions & 0 deletions packages/server-side-render/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { withSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import ServerSideRender from './server-side-render';

/**
* Constants
*/
const EMPTY_OBJECT = {};

export default withSelect(
( select ) => {
const coreEditorSelect = select( 'core/editor' );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is a bit concerning in my opinion. This package doesn't depend on @wordpress/editor explicitly but it will use it when it happens to be loaded on the page. While it solves the original issue of removing the dependency of core blocks on @wordpress/editor, in fact, it only hides it.

@jorgefilipecosta and @youknowriad, have you considered feeding the block editor provider with some commonly used data which is specific to the page context? In this case, it would be the current post id. In the context of widget areas, it might be necessary to know the id of the widget area. Well, it might turn out that this is the only component which would take advantage of it, so it's all academic :) I definitely don't want to block this PR but I wanted to hear your opinions.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @gziolo, if we follow the context approach (feeding the block editor provider with some commonly used data which is specific to the page context), wouldn't the context consumer component need to be part of @wordpress/editor? In that case, we would even need to have a direct dependency on @wordpress/editor.

Based on your idea, I think a possible solution may be to put context as part of the ServerSideRender package and expose a component ServerSideRenderAdditionalDataProvider that allows to other components to pass additional props that are used in server-side render calls (the default is an empty object and no additional props are passed). The editor would use this component to pass the post id, later if the widgets also need to pass something specific it is possible to do the same.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, ServerSideRenderProvider would solve this issue as well. I don't think there is a perfect solution to this problem. All I can think of have some drawbacks. @youknowriad will know better what is the most anticipated one 😄

if ( coreEditorSelect ) {
const currentPostId = coreEditorSelect.getCurrentPostId();
if ( currentPostId ) {
return {
currentPostId,
};
}
}
return EMPTY_OBJECT;
}
)(
( { urlQueryArgs = EMPTY_OBJECT, currentPostId, ...props } ) => {
const newUrlQueryArgs = useMemo( () => {
if ( ! currentPostId ) {
return urlQueryArgs;
}
return {
post_id: currentPostId,
...urlQueryArgs,
};
}, [ currentPostId, urlQueryArgs ] );

return (
<ServerSideRender urlQueryArgs={ newUrlQueryArgs } { ...props } />
);
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ import {
import { __, sprintf } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
*/
import Placeholder from '../placeholder';
import Spinner from '../spinner';
import {
Placeholder,
Spinner,
} from '@wordpress/components';

export function rendererPath( block, attributes = null, urlQueryArgs = {} ) {
return addQueryArgs( `/wp/v2/block-renderer/${ block }`, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { rendererPath } from '../index';
import { rendererPath } from '../server-side-render';

describe( 'rendererPath', function() {
test( 'should return an base path for empty input', function() {
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module.exports = {
'dom-ready',
'redux-routine',
'token-list',
'server-side-render',
'shortcode',
].map( camelCaseDash ) ),
new CopyWebpackPlugin(
Expand Down