-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Extract ServerSideRender component to an independent package #15635
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
Changes from all commits
f579af4
c062f23
d468439
104cbae
4782648
5882bd6
532a432
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 ) {', | ||
| ' wp.components.ServerSideRender = wp.serverSideRender;', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is a component, the capitalization of |
||
| ' };', | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,3 +117,4 @@ export { | |
| withColors, | ||
| withFontSizes, | ||
| }; | ||
| export { default as ServerSideRender } from '@wordpress/server-side-render'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just ran into this while writing a plugin. Because
Of course, before that is possible, we should at least wrap
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, this line was about
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| package-lock=false |
| 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 |
|---|---|---|
| @@ -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", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| "@wordpress/data": "file:../data", | ||
| "@wordpress/element": "file:../element", | ||
| "@wordpress/i18n": "file:../i18n", | ||
| "@wordpress/url": "file:../url", | ||
| "lodash": "^4.17.11" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } | ||
| 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' ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 @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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, |
||
| 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 } /> | ||
| ); | ||
| } | ||
| ); | ||
There was a problem hiding this comment.
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-componentsis enqueued afterwp-server-side-render, it will never be shimmed. We could potentially resolve this by definingwp-componentsas a dependency ofwp-server-side-render(optionally checking that it's at least registered).