-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ServerSideRender: Introduce a new 'useServerSideRender' hook #70543
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
Conversation
Size Change: -349 B (-0.02%) Total Size: 1.89 MB
ℹ️ View Unchanged
|
Flaky tests detected in 2ea5fa6. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/16371612834
|
10cd73d
to
6ccfd36
Compare
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @amir2mi. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
@WordPress/gutenberg-core, the code is ready for review and initial feedback. I'm working on the remaining to-do list items, which should be a blocker for the review. |
6ccfd36
to
0056685
Compare
Would love some docs, here either in the PR description or JSDoc so it's exposed. |
This comment has been minimized.
This comment has been minimized.
0056685
to
23e5aab
Compare
Pushed the JSDoc documentation and e2e tests for Currently, I’m unsure if I can also export the new hook without introducing breaking changes. Does anyone have experience with similar alterations for core packages? Why?
|
/** | ||
* @typedef {Object} ServerSideRenderResponse | ||
* @property {string} status - The current request status: 'idle', 'loading', 'success', or 'error'. | ||
* @property {string} [html] - The rendered HTML content (available when status is 'success'). |
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.
Maybe content
will be a more suitable name here. The render_callback
can return just string content without any HTML.
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.
Updated in 2ea5fa6.
I'm not sure if this is the correct way to do it, but what would happen if we exported the hook like this? Detailsdiff --git a/packages/server-side-render/src/index.js b/packages/server-side-render/src/index.js
index 5d90709bea..2b3f874ab6 100644
--- a/packages/server-side-render/src/index.js
+++ b/packages/server-side-render/src/index.js
@@ -9,6 +9,8 @@ import { useSelect } from '@wordpress/data';
*/
import ServerSideRender from './server-side-render';
+export { useServerSideRender } from './hook';
+
/**
* Constants
*/
diff --git a/tools/webpack/packages.js b/tools/webpack/packages.js
index c99c25ee01..4730fde776 100644
--- a/tools/webpack/packages.js
+++ b/tools/webpack/packages.js
@@ -114,7 +114,6 @@ const exportDefaultPackages = [
'dom-ready',
'redux-routine',
'token-list',
- 'server-side-render',
'shortcode',
'warning',
]; From what I've tested, the following import statements should all work: import ServerSideRender from '@wordpress/server-side-render';
import { useServerSideRender } from '@wordpress/server-side-render';
import ServerSideRender, { useServerSideRender } from '@wordpress/server-side-render'; |
@t-hamano, I tried that, but it breaks the code that relies on Examples: // Doesn't work.
const ServerSideRender = wp.serverSideRender;
// This works, but we'll still be breaking the BC.
const { default: ServerSideRender } = wp.serverSideRender; Update@gziolo suggested "exporting" the new hook as a property on the default exported component. It solves the backward-compatibility problem with Code diff
diff --git packages/server-side-render/src/index.js packages/server-side-render/src/index.js
index 5d90709bea2..c2c3484b718 100644
--- packages/server-side-render/src/index.js
+++ packages/server-side-render/src/index.js
@@ -8,16 +8,14 @@ import { useSelect } from '@wordpress/data';
* Internal dependencies
*/
import ServerSideRender from './server-side-render';
+import { useServerSideRender } from './hook';
/**
* Constants
*/
const EMPTY_OBJECT = {};
-export default function ExportedServerSideRender( {
- urlQueryArgs = EMPTY_OBJECT,
- ...props
-} ) {
+function ExportedServerSideRender( { urlQueryArgs = EMPTY_OBJECT, ...props } ) {
const currentPostId = useSelect( ( select ) => {
// FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.
// It is used by blocks that can be loaded into a *non-post* block editor.
@@ -43,3 +41,8 @@ export default function ExportedServerSideRender( {
return <ServerSideRender urlQueryArgs={ newUrlQueryArgs } { ...props } />;
}
+
+ExportedServerSideRender.ServerSideRender = ExportedServerSideRender;
+ExportedServerSideRender.useServerSideRender = useServerSideRender;
+
+export default ExportedServerSideRender;
Screenshot |
Possible solution: the module in the NPM package exports // src/index.js
export default ServerSideRender; // mark as @deprecated maybe
export { ServerSideRender, useServerSideRender }; This encourages plugin authors to write When building the // src/script-index.js
import * as ssr from './index.js';
function attachExports( main, all ) {
for ( const key in all ) {
if ( key !== 'default' ) {
main[ key ] = all[ key ];
}
}
return main;
}
export default attachExports( ssr.default, ssr ); And then make the webpack config in Building the NPM package ( |
@jsnajdr, unfortunately, a separate import path was triggering a parse error. To avoid modifying the webpack configuration, I ultimately settled on a mixed solution for the exports, which soft-deprecates the default export. The new and old exports are functioning as expected in my tests, but I would appreciate a second opinion on this matter. Screenshot |
d3d523b
to
18b8d44
Compare
18b8d44
to
1ac1e37
Compare
I've adjusted the JSDocs for the component/hook. The API sections of the README file are now generated automatically. |
2ea5fa6
to
b3e518a
Compare
@WordPress/gutenberg-core, I would appreciate final feedback/reviews here to land this enhancement |
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.
My only concern is folks continuing to rely on these components/hooks more where ideally tailored UI experiences are better.
100%. The tailored UI experience is what we should promote. SSR is a good stepping stone when refactoring existing code, such as shortcodes, and you want to provide a temporary basic UI. |
…ss#70543) Unlinked contributors: amir2mi. Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: youknowriad <youknowriad@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org> Co-authored-by: Soean <soean@git.wordpress.org> Co-authored-by: gziolo <gziolo@git.wordpress.org> Co-authored-by: oxyc <scholdstrom@git.wordpress.org>
Why?
Based on #35294 (comment).
Closes #7346.
Closes #32247.
PR extract data fetching logic from
ServerSideRender
into a newuseServerSideRender
hook. It will give consumers more control over the block content when they're using the server-side rendering technique.I've also refactored and improved the data fetching effect in the new hook. The effect now has a clear set of dependencies, which allows for aborting the request and canceling the debounced function.
Todos
ServerSideRender
. Choosing this over unit tests, since it gives us better coverage.Convert at least theLet's do it separately and for the whole package.hook.js
file into TS.Testing Instructions