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

Send post_id to the REST API in the ServerSideRender component within the Editor #9323

Merged

Conversation

alexsanford
Copy link
Contributor

Description

Fixes #8178

This PR adds a wrapper around the ServerSideRender component within the @wordpress/editor package that adds a post_id parameter to the REST API call. This ensures the global $post object is set properly. Previously, it was only set when rendering to the frontend, but not when rendering for the API call.

This PR is an iteration of this previous PR: #8270

How has this been tested?

This was tested locally using a dynamic block that displayed information from the $post object, ensuring the post information was rendered properly on the frontend and in the editor using ServerSideRender.

An automated test was added to ensure that the parameter is added properly to the URL path.

Also I verified that the current two blocks using ServerSideRender (Archives and Latest Comments) still work as expected.

Types of changes

This change should be a non-breaking enhancement.

That said, I did change the name of the rendererPathWithAttributes function to rendererPath, and it's technically possible that some 3rd-party code is importing and using that. But I assume that's ok since it's not part of Gutenberg's documented API.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@@ -20,10 +20,11 @@ import { addQueryArgs } from '@wordpress/url';
import Placeholder from '../placeholder';
import Spinner from '../spinner';

export function rendererPathWithAttributes( block, attributes = null ) {
export function rendererPath( block, attributes = null, urlQueryArgs = null ) {
Copy link
Member

@jorgefilipecosta jorgefilipecosta Sep 4, 2018

Choose a reason for hiding this comment

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

Minor: I think for this case we may be able to do urlQueryArgs = {} on the parameter list, and then ...urlQueryArgs inside addQueryArgs.
For attributes probably we did not follow this path because we needed to wrap it in an object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. Done.

@@ -0,0 +1,3 @@
# ServerSideRender
Copy link
Member

Choose a reason for hiding this comment

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

Minor: We normally name the file README.md (in caps). Maybe we can append some text saying it adds the post_id to the urlQueryArgs of ServerSideRender exposed in '@wordpress/components'.

Copy link
Contributor Author

@alexsanford alexsanford Sep 7, 2018

Choose a reason for hiding this comment

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

Heh. Turns out I had fixed the name in a rebase and forgot to push! 🤦‍♂️

Updated the text.

const { getCurrentPostId } = select( 'core/editor' );

return {
urlQueryArgs: {
Copy link
Member

Choose a reason for hiding this comment

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

We are ignoring the urlQueryArgs we may be receiving. Maybe we can just check if in urlQueryArgs props this component receives the property post_id is not set and if that's case we add it. urlQueryArgs would default to "{}" so on the most common case we would only add the post_id (equivalent to what we have now).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I've updated the implementation for this. It no longer uses withSelect but just returns a functional component instead.

@@ -2,5 +2,6 @@
* Internal dependencies
Copy link
Member

Choose a reason for hiding this comment

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

We have some prior art in having a component in @wordpress/editor that adds/enhances props to a component in @worpress/components, e.g., https://github.com/WordPress/gutenberg/blob/master/packages/editor/src/components/panel-color/index.js and we implemented them in src/components maybe we can also add this one there to be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

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

This is looking good 👍 Thank you for iterating on this @alexsanford! I left some comments with possible improvements we can do but from the functional point of view, the tests went great.

@alexsanford alexsanford force-pushed the fix/post-id-wrapper-for-ssr branch from df6daa6 to d2d6039 Compare September 7, 2018 10:39
@alexsanford alexsanford force-pushed the fix/post-id-wrapper-for-ssr branch from d2d6039 to ff0eb6b Compare September 7, 2018 11:42
@alexsanford alexsanford force-pushed the fix/post-id-wrapper-for-ssr branch from ff0eb6b to 0c8481c Compare September 7, 2018 11:44
@alexsanford
Copy link
Contributor Author

@jorgefilipecosta Thanks for the comments! I've addressed them above. Note that since I needed to force push anyway (because of an earlier rebase that didn't get pushed) I went ahead and rebased this onto master (no conflicts).

Copy link
Member

@danielbachhuber danielbachhuber left a comment

Choose a reason for hiding this comment

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

One note to address.

@@ -20,10 +20,11 @@ import { addQueryArgs } from '@wordpress/url';
import Placeholder from '../placeholder';
import Spinner from '../spinner';

export function rendererPathWithAttributes( block, attributes = null ) {
export function rendererPath( block, attributes = null, urlQueryArgs = {} ) {
Copy link
Member

Choose a reason for hiding this comment

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

Given this is an exported function, do we need to keep (or deprecate) the original rendererPathWithAttributes()?

Copy link
Member

Choose a reason for hiding this comment

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

Phrased another way: maybe we should keep the name as rendererPathWithAttributes(), and it simply supports a third urlQueryArgs argument.

Copy link
Member

@jorgefilipecosta jorgefilipecosta Sep 11, 2018

Choose a reason for hiding this comment

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

Hi @danielbachhuber, the function rendererPathWithAttributes is exported just for testing but is not exported outside of @wordpress/components as we only export the default export outside of the module (the component)

export { default as ServerSideRender } from './server-side-render';

No plugin could have used this function so even if we change the name I don't think deprecations are necessary.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, makes sense. Thanks for clarifying @jorgefilipecosta

@danielbachhuber danielbachhuber added [Package] Server Side Render /packages/server-side-render [Type] Enhancement A suggestion for improvement. labels Sep 11, 2018
@jorgefilipecosta jorgefilipecosta added this to the 3.9 milestone Sep 12, 2018
Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Server Side Render /packages/server-side-render [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Include 'post_id' in REST API request to render ServerSideRender block
3 participants