-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Send post_id to the REST API in the ServerSideRender component within the Editor #9323
Conversation
@@ -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 ) { |
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.
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.
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.
Good call. Done.
@@ -0,0 +1,3 @@ | |||
# ServerSideRender |
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.
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'.
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.
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: { |
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.
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).
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.
Good catch! I've updated the implementation for this. It no longer uses withSelect
but just returns a functional component instead.
packages/editor/src/utils/index.js
Outdated
@@ -2,5 +2,6 @@ | |||
* Internal dependencies |
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.
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.
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.
Done!
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.
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.
df6daa6
to
d2d6039
Compare
d2d6039
to
ff0eb6b
Compare
ff0eb6b
to
0c8481c
Compare
@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 |
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.
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 = {} ) { |
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.
Given this is an exported function, do we need to keep (or deprecate) the original rendererPathWithAttributes()
?
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.
Phrased another way: maybe we should keep the name as rendererPathWithAttributes()
, and it simply supports a third urlQueryArgs
argument.
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.
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.
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.
Ah, makes sense. Thanks for clarifying @jorgefilipecosta
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.
LGTM 👍
Description
Fixes #8178
This PR adds a wrapper around the
ServerSideRender
component within the@wordpress/editor
package that adds apost_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 usingServerSideRender
.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 torendererPath
, 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: