@wordpress/server-side-render 6.3.0 → 6.4.1-next.46f643fa0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +73 -85
- package/build/hook.js +171 -0
- package/build/hook.js.map +1 -0
- package/build/index.js +28 -42
- package/build/index.js.map +1 -1
- package/build/server-side-render.js +95 -133
- package/build/server-side-render.js.map +1 -1
- package/build-module/hook.js +161 -0
- package/build-module/hook.js.map +1 -0
- package/build-module/index.js +16 -40
- package/build-module/index.js.map +1 -1
- package/build-module/server-side-render.js +97 -133
- package/build-module/server-side-render.js.map +1 -1
- package/package.json +11 -12
- package/src/hook.js +177 -0
- package/src/index.js +16 -38
- package/src/server-side-render.js +94 -157
- package/src/test/index.js +2 -4
|
@@ -1,63 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
3
|
Object.defineProperty(exports, "__esModule", {
|
|
5
4
|
value: true
|
|
6
5
|
});
|
|
7
|
-
exports.
|
|
8
|
-
exports.
|
|
9
|
-
exports.rendererPath = rendererPath;
|
|
10
|
-
var _es = _interopRequireDefault(require("fast-deep-equal/es6"));
|
|
11
|
-
var _compose = require("@wordpress/compose");
|
|
6
|
+
exports.ServerSideRender = ServerSideRender;
|
|
7
|
+
exports.ServerSideRenderWithPostId = ServerSideRenderWithPostId;
|
|
12
8
|
var _element = require("@wordpress/element");
|
|
13
9
|
var _i18n = require("@wordpress/i18n");
|
|
14
|
-
var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
|
|
15
|
-
var _url = require("@wordpress/url");
|
|
16
10
|
var _components = require("@wordpress/components");
|
|
17
|
-
var
|
|
11
|
+
var _data = require("@wordpress/data");
|
|
12
|
+
var _hook = require("./hook");
|
|
18
13
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
19
14
|
/**
|
|
20
|
-
*
|
|
15
|
+
* WordPress dependencies
|
|
21
16
|
*/
|
|
22
17
|
|
|
23
18
|
/**
|
|
24
|
-
*
|
|
19
|
+
* Internal dependencies
|
|
25
20
|
*/
|
|
26
21
|
|
|
27
22
|
const EMPTY_OBJECT = {};
|
|
28
|
-
function rendererPath(block, attributes = null, urlQueryArgs = {}) {
|
|
29
|
-
return (0, _url.addQueryArgs)(`/wp/v2/block-renderer/${block}`, {
|
|
30
|
-
context: 'edit',
|
|
31
|
-
...(null !== attributes ? {
|
|
32
|
-
attributes
|
|
33
|
-
} : {}),
|
|
34
|
-
...urlQueryArgs
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
function removeBlockSupportAttributes(attributes) {
|
|
38
|
-
const {
|
|
39
|
-
backgroundColor,
|
|
40
|
-
borderColor,
|
|
41
|
-
fontFamily,
|
|
42
|
-
fontSize,
|
|
43
|
-
gradient,
|
|
44
|
-
textColor,
|
|
45
|
-
className,
|
|
46
|
-
...restAttributes
|
|
47
|
-
} = attributes;
|
|
48
|
-
const {
|
|
49
|
-
border,
|
|
50
|
-
color,
|
|
51
|
-
elements,
|
|
52
|
-
spacing,
|
|
53
|
-
typography,
|
|
54
|
-
...restStyles
|
|
55
|
-
} = attributes?.style || EMPTY_OBJECT;
|
|
56
|
-
return {
|
|
57
|
-
...restAttributes,
|
|
58
|
-
style: restStyles
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
23
|
function DefaultEmptyResponsePlaceholder({
|
|
62
24
|
className
|
|
63
25
|
}) {
|
|
@@ -67,12 +29,12 @@ function DefaultEmptyResponsePlaceholder({
|
|
|
67
29
|
});
|
|
68
30
|
}
|
|
69
31
|
function DefaultErrorResponsePlaceholder({
|
|
70
|
-
|
|
32
|
+
message,
|
|
71
33
|
className
|
|
72
34
|
}) {
|
|
73
35
|
const errorMessage = (0, _i18n.sprintf)(
|
|
74
36
|
// translators: %s: error message describing the problem
|
|
75
|
-
(0, _i18n.__)('Error loading block: %s'),
|
|
37
|
+
(0, _i18n.__)('Error loading block: %s'), message);
|
|
76
38
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Placeholder, {
|
|
77
39
|
className: className,
|
|
78
40
|
children: errorMessage
|
|
@@ -111,117 +73,117 @@ function DefaultLoadingResponsePlaceholder({
|
|
|
111
73
|
});
|
|
112
74
|
}
|
|
113
75
|
function ServerSideRender(props) {
|
|
76
|
+
const prevContentRef = (0, _element.useRef)('');
|
|
114
77
|
const {
|
|
115
78
|
className,
|
|
116
79
|
EmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,
|
|
117
80
|
ErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,
|
|
118
|
-
LoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder
|
|
81
|
+
LoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder,
|
|
82
|
+
...restProps
|
|
119
83
|
} = props;
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const latestPropsRef = (0, _element.useRef)(props);
|
|
126
|
-
(0, _element.useLayoutEffect)(() => {
|
|
127
|
-
latestPropsRef.current = props;
|
|
128
|
-
}, [props]);
|
|
129
|
-
const fetchData = (0, _element.useCallback)(() => {
|
|
130
|
-
var _sanitizedAttributes, _sanitizedAttributes2;
|
|
131
|
-
if (!isMountedRef.current) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
const {
|
|
135
|
-
attributes,
|
|
136
|
-
block,
|
|
137
|
-
skipBlockSupportAttributes = false,
|
|
138
|
-
httpMethod = 'GET',
|
|
139
|
-
urlQueryArgs
|
|
140
|
-
} = latestPropsRef.current;
|
|
141
|
-
setIsLoading(true);
|
|
142
|
-
let sanitizedAttributes = attributes && (0, _blocks.__experimentalSanitizeBlockAttributes)(block, attributes);
|
|
143
|
-
if (skipBlockSupportAttributes) {
|
|
144
|
-
sanitizedAttributes = removeBlockSupportAttributes(sanitizedAttributes);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// If httpMethod is 'POST', send the attributes in the request body instead of the URL.
|
|
148
|
-
// This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
|
|
149
|
-
const isPostRequest = 'POST' === httpMethod;
|
|
150
|
-
const urlAttributes = isPostRequest ? null : (_sanitizedAttributes = sanitizedAttributes) !== null && _sanitizedAttributes !== void 0 ? _sanitizedAttributes : null;
|
|
151
|
-
const path = rendererPath(block, urlAttributes, urlQueryArgs);
|
|
152
|
-
const data = isPostRequest ? {
|
|
153
|
-
attributes: (_sanitizedAttributes2 = sanitizedAttributes) !== null && _sanitizedAttributes2 !== void 0 ? _sanitizedAttributes2 : null
|
|
154
|
-
} : null;
|
|
155
|
-
|
|
156
|
-
// Store the latest fetch request so that when we process it, we can
|
|
157
|
-
// check if it is the current request, to avoid race conditions on slow networks.
|
|
158
|
-
const fetchRequest = fetchRequestRef.current = (0, _apiFetch.default)({
|
|
159
|
-
path,
|
|
160
|
-
data,
|
|
161
|
-
method: isPostRequest ? 'POST' : 'GET'
|
|
162
|
-
}).then(fetchResponse => {
|
|
163
|
-
if (isMountedRef.current && fetchRequest === fetchRequestRef.current && fetchResponse) {
|
|
164
|
-
setResponse(fetchResponse.rendered);
|
|
165
|
-
}
|
|
166
|
-
}).catch(error => {
|
|
167
|
-
if (isMountedRef.current && fetchRequest === fetchRequestRef.current) {
|
|
168
|
-
setResponse({
|
|
169
|
-
error: true,
|
|
170
|
-
errorMsg: error.message
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
}).finally(() => {
|
|
174
|
-
if (isMountedRef.current && fetchRequest === fetchRequestRef.current) {
|
|
175
|
-
setIsLoading(false);
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
return fetchRequest;
|
|
179
|
-
}, []);
|
|
180
|
-
const debouncedFetchData = (0, _compose.useDebounce)(fetchData, 500);
|
|
84
|
+
const {
|
|
85
|
+
content,
|
|
86
|
+
status,
|
|
87
|
+
error
|
|
88
|
+
} = (0, _hook.useServerSideRender)(restProps);
|
|
181
89
|
|
|
182
|
-
//
|
|
183
|
-
// let the async fetch callbacks know when to stop.
|
|
90
|
+
// Store the previous successful HTML response to show while loading.
|
|
184
91
|
(0, _element.useEffect)(() => {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
isMountedRef.current = false;
|
|
188
|
-
};
|
|
189
|
-
}, []);
|
|
190
|
-
(0, _element.useEffect)(() => {
|
|
191
|
-
// Don't debounce the first fetch. This ensures that the first render
|
|
192
|
-
// shows data as soon as possible.
|
|
193
|
-
if (prevProps === undefined) {
|
|
194
|
-
fetchData();
|
|
195
|
-
} else if (!(0, _es.default)(prevProps, props)) {
|
|
196
|
-
debouncedFetchData();
|
|
92
|
+
if (content) {
|
|
93
|
+
prevContentRef.current = content;
|
|
197
94
|
}
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
const hasEmptyResponse = response === '';
|
|
201
|
-
const hasError = !!response?.error;
|
|
202
|
-
if (isLoading) {
|
|
95
|
+
}, [content]);
|
|
96
|
+
if (status === 'loading') {
|
|
203
97
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)(LoadingResponsePlaceholder, {
|
|
204
98
|
...props,
|
|
205
|
-
children:
|
|
99
|
+
children: !!prevContentRef.current && /*#__PURE__*/(0, _jsxRuntime.jsx)(_element.RawHTML, {
|
|
206
100
|
className: className,
|
|
207
|
-
children:
|
|
101
|
+
children: prevContentRef.current
|
|
208
102
|
})
|
|
209
103
|
});
|
|
210
104
|
}
|
|
211
|
-
if (
|
|
105
|
+
if (status === 'success' && !content) {
|
|
212
106
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)(EmptyResponsePlaceholder, {
|
|
213
107
|
...props
|
|
214
108
|
});
|
|
215
109
|
}
|
|
216
|
-
if (
|
|
110
|
+
if (status === 'error') {
|
|
217
111
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)(ErrorResponsePlaceholder, {
|
|
218
|
-
|
|
112
|
+
message: error,
|
|
219
113
|
...props
|
|
220
114
|
});
|
|
221
115
|
}
|
|
222
116
|
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_element.RawHTML, {
|
|
223
117
|
className: className,
|
|
224
|
-
children:
|
|
118
|
+
children: content
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* A component that renders server-side content for blocks.
|
|
124
|
+
*
|
|
125
|
+
* Note: URL query will include the current post ID when applicable.
|
|
126
|
+
* This is useful for blocks that depend on the context of the current post for rendering.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```jsx
|
|
130
|
+
* import { ServerSideRender } from '@wordpress/server-side-render';
|
|
131
|
+
* // Legacy import for WordPress 6.8 and earlier
|
|
132
|
+
* // import { default as ServerSideRender } from '@wordpress/server-side-render';
|
|
133
|
+
*
|
|
134
|
+
* function Example() {
|
|
135
|
+
* return (
|
|
136
|
+
* <ServerSideRender
|
|
137
|
+
* block="core/archives"
|
|
138
|
+
* attributes={ { showPostCounts: true } }
|
|
139
|
+
* urlQueryArgs={ { customArg: 'value' } }
|
|
140
|
+
* className="custom-class"
|
|
141
|
+
* />
|
|
142
|
+
* );
|
|
143
|
+
* }
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* @param {Object} props Component props.
|
|
147
|
+
* @param {string} props.block The identifier of the block to be serverside rendered.
|
|
148
|
+
* @param {Object} props.attributes The block attributes to be sent to the server for rendering.
|
|
149
|
+
* @param {string} [props.className] Additional classes to apply to the wrapper element.
|
|
150
|
+
* @param {string} [props.httpMethod='GET'] The HTTP method to use ('GET' or 'POST'). Default is 'GET'
|
|
151
|
+
* @param {Object} [props.urlQueryArgs] Additional query arguments to append to the request URL.
|
|
152
|
+
* @param {boolean} [props.skipBlockSupportAttributes=false] Whether to remove block support attributes before sending.
|
|
153
|
+
* @param {Function} [props.EmptyResponsePlaceholder] Component rendered when the API response is empty.
|
|
154
|
+
* @param {Function} [props.ErrorResponsePlaceholder] Component rendered when the API response is an error.
|
|
155
|
+
* @param {Function} [props.LoadingResponsePlaceholder] Component rendered while the API request is loading.
|
|
156
|
+
*
|
|
157
|
+
* @return {JSX.Element} The rendered server-side content.
|
|
158
|
+
*/
|
|
159
|
+
function ServerSideRenderWithPostId({
|
|
160
|
+
urlQueryArgs = EMPTY_OBJECT,
|
|
161
|
+
...props
|
|
162
|
+
}) {
|
|
163
|
+
const currentPostId = (0, _data.useSelect)(select => {
|
|
164
|
+
// FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.
|
|
165
|
+
// It is used by blocks that can be loaded into a *non-post* block editor.
|
|
166
|
+
// eslint-disable-next-line @wordpress/data-no-store-string-literals
|
|
167
|
+
const postId = select('core/editor')?.getCurrentPostId();
|
|
168
|
+
|
|
169
|
+
// For templates and template parts we use a custom ID format.
|
|
170
|
+
// Since they aren't real posts, we don't want to use their ID
|
|
171
|
+
// for server-side rendering. Since they use a string based ID,
|
|
172
|
+
// we can assume real post IDs are numbers.
|
|
173
|
+
return postId && typeof postId === 'number' ? postId : null;
|
|
174
|
+
}, []);
|
|
175
|
+
const newUrlQueryArgs = (0, _element.useMemo)(() => {
|
|
176
|
+
if (!currentPostId) {
|
|
177
|
+
return urlQueryArgs;
|
|
178
|
+
}
|
|
179
|
+
return {
|
|
180
|
+
post_id: currentPostId,
|
|
181
|
+
...urlQueryArgs
|
|
182
|
+
};
|
|
183
|
+
}, [currentPostId, urlQueryArgs]);
|
|
184
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(ServerSideRender, {
|
|
185
|
+
urlQueryArgs: newUrlQueryArgs,
|
|
186
|
+
...props
|
|
225
187
|
});
|
|
226
188
|
}
|
|
227
189
|
//# sourceMappingURL=server-side-render.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_es","_interopRequireDefault","require","_compose","_element","_i18n","_apiFetch","_url","_components","_blocks","_jsxRuntime","EMPTY_OBJECT","rendererPath","block","attributes","urlQueryArgs","addQueryArgs","context","removeBlockSupportAttributes","backgroundColor","borderColor","fontFamily","fontSize","gradient","textColor","className","restAttributes","border","color","elements","spacing","typography","restStyles","style","DefaultEmptyResponsePlaceholder","jsx","Placeholder","children","__","DefaultErrorResponsePlaceholder","response","errorMessage","sprintf","errorMsg","DefaultLoadingResponsePlaceholder","showLoader","setShowLoader","useState","useEffect","timeout","setTimeout","clearTimeout","jsxs","position","top","left","marginTop","marginLeft","Spinner","opacity","ServerSideRender","props","EmptyResponsePlaceholder","ErrorResponsePlaceholder","LoadingResponsePlaceholder","isMountedRef","useRef","fetchRequestRef","setResponse","prevProps","usePrevious","isLoading","setIsLoading","latestPropsRef","useLayoutEffect","current","fetchData","useCallback","_sanitizedAttributes","_sanitizedAttributes2","skipBlockSupportAttributes","httpMethod","sanitizedAttributes","__experimentalSanitizeBlockAttributes","isPostRequest","urlAttributes","path","data","fetchRequest","apiFetch","method","then","fetchResponse","rendered","catch","error","message","finally","debouncedFetchData","useDebounce","undefined","fastDeepEqual","hasResponse","hasEmptyResponse","hasError","RawHTML"],"sources":["@wordpress/server-side-render/src/server-side-render.js"],"sourcesContent":["/**\n * External dependencies\n */\nimport fastDeepEqual from 'fast-deep-equal/es6';\n\n/**\n * WordPress dependencies\n */\nimport { useDebounce, usePrevious } from '@wordpress/compose';\nimport {\n\tRawHTML,\n\tuseCallback,\n\tuseEffect,\n\tuseLayoutEffect,\n\tuseRef,\n\tuseState,\n} from '@wordpress/element';\nimport { __, sprintf } from '@wordpress/i18n';\nimport apiFetch from '@wordpress/api-fetch';\nimport { addQueryArgs } from '@wordpress/url';\nimport { Placeholder, Spinner } from '@wordpress/components';\nimport { __experimentalSanitizeBlockAttributes } from '@wordpress/blocks';\n\nconst EMPTY_OBJECT = {};\n\nexport function rendererPath( block, attributes = null, urlQueryArgs = {} ) {\n\treturn addQueryArgs( `/wp/v2/block-renderer/${ block }`, {\n\t\tcontext: 'edit',\n\t\t...( null !== attributes ? { attributes } : {} ),\n\t\t...urlQueryArgs,\n\t} );\n}\n\nexport function removeBlockSupportAttributes( attributes ) {\n\tconst {\n\t\tbackgroundColor,\n\t\tborderColor,\n\t\tfontFamily,\n\t\tfontSize,\n\t\tgradient,\n\t\ttextColor,\n\t\tclassName,\n\t\t...restAttributes\n\t} = attributes;\n\n\tconst { border, color, elements, spacing, typography, ...restStyles } =\n\t\tattributes?.style || EMPTY_OBJECT;\n\n\treturn {\n\t\t...restAttributes,\n\t\tstyle: restStyles,\n\t};\n}\n\nfunction DefaultEmptyResponsePlaceholder( { className } ) {\n\treturn (\n\t\t<Placeholder className={ className }>\n\t\t\t{ __( 'Block rendered as empty.' ) }\n\t\t</Placeholder>\n\t);\n}\n\nfunction DefaultErrorResponsePlaceholder( { response, className } ) {\n\tconst errorMessage = sprintf(\n\t\t// translators: %s: error message describing the problem\n\t\t__( 'Error loading block: %s' ),\n\t\tresponse.errorMsg\n\t);\n\treturn <Placeholder className={ className }>{ errorMessage }</Placeholder>;\n}\n\nfunction DefaultLoadingResponsePlaceholder( { children } ) {\n\tconst [ showLoader, setShowLoader ] = useState( false );\n\n\tuseEffect( () => {\n\t\t// Schedule showing the Spinner after 1 second.\n\t\tconst timeout = setTimeout( () => {\n\t\t\tsetShowLoader( true );\n\t\t}, 1000 );\n\t\treturn () => clearTimeout( timeout );\n\t}, [] );\n\n\treturn (\n\t\t<div style={ { position: 'relative' } }>\n\t\t\t{ showLoader && (\n\t\t\t\t<div\n\t\t\t\t\tstyle={ {\n\t\t\t\t\t\tposition: 'absolute',\n\t\t\t\t\t\ttop: '50%',\n\t\t\t\t\t\tleft: '50%',\n\t\t\t\t\t\tmarginTop: '-9px',\n\t\t\t\t\t\tmarginLeft: '-9px',\n\t\t\t\t\t} }\n\t\t\t\t>\n\t\t\t\t\t<Spinner />\n\t\t\t\t</div>\n\t\t\t) }\n\t\t\t<div style={ { opacity: showLoader ? '0.3' : 1 } }>\n\t\t\t\t{ children }\n\t\t\t</div>\n\t\t</div>\n\t);\n}\n\nexport default function ServerSideRender( props ) {\n\tconst {\n\t\tclassName,\n\t\tEmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,\n\t\tErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,\n\t\tLoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder,\n\t} = props;\n\n\tconst isMountedRef = useRef( false );\n\tconst fetchRequestRef = useRef();\n\tconst [ response, setResponse ] = useState( null );\n\tconst prevProps = usePrevious( props );\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\tconst latestPropsRef = useRef( props );\n\n\tuseLayoutEffect( () => {\n\t\tlatestPropsRef.current = props;\n\t}, [ props ] );\n\n\tconst fetchData = useCallback( () => {\n\t\tif ( ! isMountedRef.current ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst {\n\t\t\tattributes,\n\t\t\tblock,\n\t\t\tskipBlockSupportAttributes = false,\n\t\t\thttpMethod = 'GET',\n\t\t\turlQueryArgs,\n\t\t} = latestPropsRef.current;\n\n\t\tsetIsLoading( true );\n\n\t\tlet sanitizedAttributes =\n\t\t\tattributes &&\n\t\t\t__experimentalSanitizeBlockAttributes( block, attributes );\n\n\t\tif ( skipBlockSupportAttributes ) {\n\t\t\tsanitizedAttributes =\n\t\t\t\tremoveBlockSupportAttributes( sanitizedAttributes );\n\t\t}\n\n\t\t// If httpMethod is 'POST', send the attributes in the request body instead of the URL.\n\t\t// This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.\n\t\tconst isPostRequest = 'POST' === httpMethod;\n\t\tconst urlAttributes = isPostRequest\n\t\t\t? null\n\t\t\t: sanitizedAttributes ?? null;\n\t\tconst path = rendererPath( block, urlAttributes, urlQueryArgs );\n\t\tconst data = isPostRequest\n\t\t\t? { attributes: sanitizedAttributes ?? null }\n\t\t\t: null;\n\n\t\t// Store the latest fetch request so that when we process it, we can\n\t\t// check if it is the current request, to avoid race conditions on slow networks.\n\t\tconst fetchRequest = ( fetchRequestRef.current = apiFetch( {\n\t\t\tpath,\n\t\t\tdata,\n\t\t\tmethod: isPostRequest ? 'POST' : 'GET',\n\t\t} )\n\t\t\t.then( ( fetchResponse ) => {\n\t\t\t\tif (\n\t\t\t\t\tisMountedRef.current &&\n\t\t\t\t\tfetchRequest === fetchRequestRef.current &&\n\t\t\t\t\tfetchResponse\n\t\t\t\t) {\n\t\t\t\t\tsetResponse( fetchResponse.rendered );\n\t\t\t\t}\n\t\t\t} )\n\t\t\t.catch( ( error ) => {\n\t\t\t\tif (\n\t\t\t\t\tisMountedRef.current &&\n\t\t\t\t\tfetchRequest === fetchRequestRef.current\n\t\t\t\t) {\n\t\t\t\t\tsetResponse( {\n\t\t\t\t\t\terror: true,\n\t\t\t\t\t\terrorMsg: error.message,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t} )\n\t\t\t.finally( () => {\n\t\t\t\tif (\n\t\t\t\t\tisMountedRef.current &&\n\t\t\t\t\tfetchRequest === fetchRequestRef.current\n\t\t\t\t) {\n\t\t\t\t\tsetIsLoading( false );\n\t\t\t\t}\n\t\t\t} ) );\n\n\t\treturn fetchRequest;\n\t}, [] );\n\n\tconst debouncedFetchData = useDebounce( fetchData, 500 );\n\n\t// When the component unmounts, set isMountedRef to false. This will\n\t// let the async fetch callbacks know when to stop.\n\tuseEffect( () => {\n\t\tisMountedRef.current = true;\n\t\treturn () => {\n\t\t\tisMountedRef.current = false;\n\t\t};\n\t}, [] );\n\n\tuseEffect( () => {\n\t\t// Don't debounce the first fetch. This ensures that the first render\n\t\t// shows data as soon as possible.\n\t\tif ( prevProps === undefined ) {\n\t\t\tfetchData();\n\t\t} else if ( ! fastDeepEqual( prevProps, props ) ) {\n\t\t\tdebouncedFetchData();\n\t\t}\n\t} );\n\n\tconst hasResponse = !! response;\n\tconst hasEmptyResponse = response === '';\n\tconst hasError = !! response?.error;\n\n\tif ( isLoading ) {\n\t\treturn (\n\t\t\t<LoadingResponsePlaceholder { ...props }>\n\t\t\t\t{ hasResponse && ! hasError && (\n\t\t\t\t\t<RawHTML className={ className }>{ response }</RawHTML>\n\t\t\t\t) }\n\t\t\t</LoadingResponsePlaceholder>\n\t\t);\n\t}\n\n\tif ( hasEmptyResponse || ! hasResponse ) {\n\t\treturn <EmptyResponsePlaceholder { ...props } />;\n\t}\n\n\tif ( hasError ) {\n\t\treturn <ErrorResponsePlaceholder response={ response } { ...props } />;\n\t}\n\n\treturn <RawHTML className={ className }>{ response }</RawHTML>;\n}\n"],"mappings":";;;;;;;;;AAGA,IAAAA,GAAA,GAAAC,sBAAA,CAAAC,OAAA;AAKA,IAAAC,QAAA,GAAAD,OAAA;AACA,IAAAE,QAAA,GAAAF,OAAA;AAQA,IAAAG,KAAA,GAAAH,OAAA;AACA,IAAAI,SAAA,GAAAL,sBAAA,CAAAC,OAAA;AACA,IAAAK,IAAA,GAAAL,OAAA;AACA,IAAAM,WAAA,GAAAN,OAAA;AACA,IAAAO,OAAA,GAAAP,OAAA;AAA0E,IAAAQ,WAAA,GAAAR,OAAA;AArB1E;AACA;AACA;;AAGA;AACA;AACA;;AAgBA,MAAMS,YAAY,GAAG,CAAC,CAAC;AAEhB,SAASC,YAAYA,CAAEC,KAAK,EAAEC,UAAU,GAAG,IAAI,EAAEC,YAAY,GAAG,CAAC,CAAC,EAAG;EAC3E,OAAO,IAAAC,iBAAY,EAAE,yBAA0BH,KAAK,EAAG,EAAE;IACxDI,OAAO,EAAE,MAAM;IACf,IAAK,IAAI,KAAKH,UAAU,GAAG;MAAEA;IAAW,CAAC,GAAG,CAAC,CAAC,CAAE;IAChD,GAAGC;EACJ,CAAE,CAAC;AACJ;AAEO,SAASG,4BAA4BA,CAAEJ,UAAU,EAAG;EAC1D,MAAM;IACLK,eAAe;IACfC,WAAW;IACXC,UAAU;IACVC,QAAQ;IACRC,QAAQ;IACRC,SAAS;IACTC,SAAS;IACT,GAAGC;EACJ,CAAC,GAAGZ,UAAU;EAEd,MAAM;IAAEa,MAAM;IAAEC,KAAK;IAAEC,QAAQ;IAAEC,OAAO;IAAEC,UAAU;IAAE,GAAGC;EAAW,CAA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CAAW;IACtD,CAC0B,CAAC;EAE/B;EAEA,IAAK+D,gBAAgB,IAAI,CAAED,WAAW,EAAG;IACxC,oBAAO,IAAA5F,WAAA,CAAAyB,GAAA,EAAC2B,wBAAwB;MAAA,GAAMD;IAAK,CAAI,CAAC;EACjD;EAEA,IAAK2C,QAAQ,EAAG;IACf,oBAAO,IAAA9F,WAAA,CAAAyB,GAAA,EAAC4B,wBAAwB;MAACvB,QAAQ,EAAGA,QAAU;MAAA,GAAMqB;IAAK,CAAI,CAAC;EACvE;EAEA,oBAAO,IAAAnD,WAAA,CAAAyB,GAAA,EAAC/B,QAAA,CAAAqG,OAAO;IAAChF,SAAS,EAAGA,SAAW;IAAAY,QAAA,EAAGG;EAAQ,CAAW,CAAC;AAC/D","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["_element","require","_i18n","_components","_data","_hook","_jsxRuntime","EMPTY_OBJECT","DefaultEmptyResponsePlaceholder","className","jsx","Placeholder","children","__","DefaultErrorResponsePlaceholder","message","errorMessage","sprintf","DefaultLoadingResponsePlaceholder","showLoader","setShowLoader","useState","useEffect","timeout","setTimeout","clearTimeout","jsxs","style","position","top","left","marginTop","marginLeft","Spinner","opacity","ServerSideRender","props","prevContentRef","useRef","EmptyResponsePlaceholder","ErrorResponsePlaceholder","LoadingResponsePlaceholder","restProps","content","status","error","useServerSideRender","current","RawHTML","ServerSideRenderWithPostId","urlQueryArgs","currentPostId","useSelect","select","postId","getCurrentPostId","newUrlQueryArgs","useMemo","post_id"],"sources":["@wordpress/server-side-render/src/server-side-render.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport {\n\tRawHTML,\n\tuseEffect,\n\tuseState,\n\tuseRef,\n\tuseMemo,\n} from '@wordpress/element';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { Placeholder, Spinner } from '@wordpress/components';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { useServerSideRender } from './hook';\n\nconst EMPTY_OBJECT = {};\n\nfunction DefaultEmptyResponsePlaceholder( { className } ) {\n\treturn (\n\t\t<Placeholder className={ className }>\n\t\t\t{ __( 'Block rendered as empty.' ) }\n\t\t</Placeholder>\n\t);\n}\n\nfunction DefaultErrorResponsePlaceholder( { message, className } ) {\n\tconst errorMessage = sprintf(\n\t\t// translators: %s: error message describing the problem\n\t\t__( 'Error loading block: %s' ),\n\t\tmessage\n\t);\n\treturn <Placeholder className={ className }>{ errorMessage }</Placeholder>;\n}\n\nfunction DefaultLoadingResponsePlaceholder( { children } ) {\n\tconst [ showLoader, setShowLoader ] = useState( false );\n\n\tuseEffect( () => {\n\t\t// Schedule showing the Spinner after 1 second.\n\t\tconst timeout = setTimeout( () => {\n\t\t\tsetShowLoader( true );\n\t\t}, 1000 );\n\t\treturn () => clearTimeout( timeout );\n\t}, [] );\n\n\treturn (\n\t\t<div style={ { position: 'relative' } }>\n\t\t\t{ showLoader && (\n\t\t\t\t<div\n\t\t\t\t\tstyle={ {\n\t\t\t\t\t\tposition: 'absolute',\n\t\t\t\t\t\ttop: '50%',\n\t\t\t\t\t\tleft: '50%',\n\t\t\t\t\t\tmarginTop: '-9px',\n\t\t\t\t\t\tmarginLeft: '-9px',\n\t\t\t\t\t} }\n\t\t\t\t>\n\t\t\t\t\t<Spinner />\n\t\t\t\t</div>\n\t\t\t) }\n\t\t\t<div style={ { opacity: showLoader ? '0.3' : 1 } }>\n\t\t\t\t{ children }\n\t\t\t</div>\n\t\t</div>\n\t);\n}\n\nexport function ServerSideRender( props ) {\n\tconst prevContentRef = useRef( '' );\n\tconst {\n\t\tclassName,\n\t\tEmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,\n\t\tErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,\n\t\tLoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder,\n\t\t...restProps\n\t} = props;\n\n\tconst { content, status, error } = useServerSideRender( restProps );\n\n\t// Store the previous successful HTML response to show while loading.\n\tuseEffect( () => {\n\t\tif ( content ) {\n\t\t\tprevContentRef.current = content;\n\t\t}\n\t}, [ content ] );\n\n\tif ( status === 'loading' ) {\n\t\treturn (\n\t\t\t<LoadingResponsePlaceholder { ...props }>\n\t\t\t\t{ !! prevContentRef.current && (\n\t\t\t\t\t<RawHTML className={ className }>\n\t\t\t\t\t\t{ prevContentRef.current }\n\t\t\t\t\t</RawHTML>\n\t\t\t\t) }\n\t\t\t</LoadingResponsePlaceholder>\n\t\t);\n\t}\n\n\tif ( status === 'success' && ! content ) {\n\t\treturn <EmptyResponsePlaceholder { ...props } />;\n\t}\n\n\tif ( status === 'error' ) {\n\t\treturn <ErrorResponsePlaceholder message={ error } { ...props } />;\n\t}\n\n\treturn <RawHTML className={ className }>{ content }</RawHTML>;\n}\n\n/**\n * A component that renders server-side content for blocks.\n *\n * Note: URL query will include the current post ID when applicable.\n * This is useful for blocks that depend on the context of the current post for rendering.\n *\n * @example\n * ```jsx\n * import { ServerSideRender } from '@wordpress/server-side-render';\n * // Legacy import for WordPress 6.8 and earlier\n * // import { default as ServerSideRender } from '@wordpress/server-side-render';\n *\n * function Example() {\n * return (\n * <ServerSideRender\n * block=\"core/archives\"\n * attributes={ { showPostCounts: true } }\n * urlQueryArgs={ { customArg: 'value' } }\n * className=\"custom-class\"\n * />\n * );\n * }\n * ```\n *\n * @param {Object} props Component props.\n * @param {string} props.block The identifier of the block to be serverside rendered.\n * @param {Object} props.attributes The block attributes to be sent to the server for rendering.\n * @param {string} [props.className] Additional classes to apply to the wrapper element.\n * @param {string} [props.httpMethod='GET'] The HTTP method to use ('GET' or 'POST'). Default is 'GET'\n * @param {Object} [props.urlQueryArgs] Additional query arguments to append to the request URL.\n * @param {boolean} [props.skipBlockSupportAttributes=false] Whether to remove block support attributes before sending.\n * @param {Function} [props.EmptyResponsePlaceholder] Component rendered when the API response is empty.\n * @param {Function} [props.ErrorResponsePlaceholder] Component rendered when the API response is an error.\n * @param {Function} [props.LoadingResponsePlaceholder] Component rendered while the API request is loading.\n *\n * @return {JSX.Element} The rendered server-side content.\n */\nexport function ServerSideRenderWithPostId( {\n\turlQueryArgs = EMPTY_OBJECT,\n\t...props\n} ) {\n\tconst currentPostId = useSelect( ( select ) => {\n\t\t// FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.\n\t\t// It is used by blocks that can be loaded into a *non-post* block editor.\n\t\t// eslint-disable-next-line @wordpress/data-no-store-string-literals\n\t\tconst postId = select( 'core/editor' )?.getCurrentPostId();\n\n\t\t// For templates and template parts we use a custom ID format.\n\t\t// Since they aren't real posts, we don't want to use their ID\n\t\t// for server-side rendering. Since they use a string based ID,\n\t\t// we can assume real post IDs are numbers.\n\t\treturn postId && typeof postId === 'number' ? postId : null;\n\t}, [] );\n\n\tconst newUrlQueryArgs = useMemo( () => {\n\t\tif ( ! currentPostId ) {\n\t\t\treturn urlQueryArgs;\n\t\t}\n\t\treturn {\n\t\t\tpost_id: currentPostId,\n\t\t\t...urlQueryArgs,\n\t\t};\n\t}, [ currentPostId, urlQueryArgs ] );\n\n\treturn <ServerSideRender urlQueryArgs={ newUrlQueryArgs } { ...props } />;\n}\n"],"mappings":";;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AAOA,IAAAC,KAAA,GAAAD,OAAA;AACA,IAAAE,WAAA,GAAAF,OAAA;AACA,IAAAG,KAAA,GAAAH,OAAA;AAKA,IAAAI,KAAA,GAAAJ,OAAA;AAA6C,IAAAK,WAAA,GAAAL,OAAA;AAjB7C;AACA;AACA;;AAYA;AACA;AACA;;AAGA,MAAMM,YAAY,GAAG,CAAC,CAAC;AAEvB,SAASC,+BAA+BA,CAAE;EAAEC;AAAU,CAAC,EAAG;EACzD,oBACC,IAAAH,WAAA,CAAAI,GAAA,EAACP,WAAA,CAAAQ,WAAW;IAACF,SAAS,EAAGA,SAAW;IAAAG,QAAA,EACjC,IAAAC,QAAE,EAAE,0BAA2B;EAAC,CACtB,CAAC;AAEhB;AAEA,SAASC,+BAA+BA,CAAE;EAAEC,OAAO;EAAEN;AAAU,CAAC,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C9D;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASM,0BAA0BA,CAAE;EAC3CC,YAAY,GAAG3C,YAAY;EAC3B,GAAG6B;AACJ,CAAC,EAAG;EACH,MAAMe,aAAa,GAAG,IAAAC,eAAS,EAAIC,MAAM,IAAM;IAC9C;IACA;IACA;IACA,MAAMC,MAAM,GAAGD,MAAM,CAAE,aAAc,CAAC,EAAEE,gBAAgB,CAAC,CAAC;;IAE1D;IACA;IACA;IACA;IACA,OAAOD,MAAM,IAAI,OAAOA,MAAM,KAAK,QAAQ,GAAGA,MAAM,GAAG,IAAI;EAC5D,CAAC,EAAE,EAAG,CAAC;EAEP,MAAME,eAAe,GAAG,IAAAC,gBAAO,EAAE,MAAM;IACtC,IAAK,CAAEN,aAAa,EAAG;MACtB,OAAOD,YAAY;IACpB;IACA,OAAO;MACNQ,OAAO,EAAEP,aAAa;MACtB,GAAGD;IACJ,CAAC;EACF,CAAC,EAAE,CAAEC,aAAa,EAAED,YAAY,CAAG,CAAC;EAEpC,oBAAO,IAAA5C,WAAA,CAAAI,GAAA,EAACyB,gBAAgB;IAACe,YAAY,EAAGM,eAAiB;IAAA,GAAMpB;EAAK,CAAI,CAAC;AAC1E","ignoreList":[]}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { debounce } from '@wordpress/compose';
|
|
5
|
+
import { useEffect, useState, useRef } from '@wordpress/element';
|
|
6
|
+
import apiFetch from '@wordpress/api-fetch';
|
|
7
|
+
import { addQueryArgs } from '@wordpress/url';
|
|
8
|
+
import { __experimentalSanitizeBlockAttributes } from '@wordpress/blocks';
|
|
9
|
+
export function rendererPath(block, attributes = null, urlQueryArgs = {}) {
|
|
10
|
+
return addQueryArgs(`/wp/v2/block-renderer/${block}`, {
|
|
11
|
+
context: 'edit',
|
|
12
|
+
...(null !== attributes ? {
|
|
13
|
+
attributes
|
|
14
|
+
} : {}),
|
|
15
|
+
...urlQueryArgs
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
export function removeBlockSupportAttributes(attributes) {
|
|
19
|
+
const {
|
|
20
|
+
backgroundColor,
|
|
21
|
+
borderColor,
|
|
22
|
+
fontFamily,
|
|
23
|
+
fontSize,
|
|
24
|
+
gradient,
|
|
25
|
+
textColor,
|
|
26
|
+
className,
|
|
27
|
+
...restAttributes
|
|
28
|
+
} = attributes;
|
|
29
|
+
const {
|
|
30
|
+
border,
|
|
31
|
+
color,
|
|
32
|
+
elements,
|
|
33
|
+
shadow,
|
|
34
|
+
spacing,
|
|
35
|
+
typography,
|
|
36
|
+
...restStyles
|
|
37
|
+
} = attributes?.style || {};
|
|
38
|
+
return {
|
|
39
|
+
...restAttributes,
|
|
40
|
+
style: restStyles
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @typedef {Object} ServerSideRenderResponse
|
|
46
|
+
* @property {string} status - The current request status: 'idle', 'loading', 'success', or 'error'.
|
|
47
|
+
* @property {string} [content] - The rendered block content (available when status is 'success').
|
|
48
|
+
* @property {string} [error] - The error message (available when status is 'error').
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A hook for server-side rendering a preview of dynamic blocks to display in the editor.
|
|
53
|
+
*
|
|
54
|
+
* Handles fetching server-rendered previews for blocks, managing loading states,
|
|
55
|
+
* and automatically debouncing requests to prevent excessive API calls. It supports both
|
|
56
|
+
* GET and POST requests, with POST requests used for larger attribute payloads.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* Basic usage:
|
|
60
|
+
*
|
|
61
|
+
* ```jsx
|
|
62
|
+
* import { RawHTML } from '@wordpress/element';
|
|
63
|
+
* import { useServerSideRender } from '@wordpress/server-side-render';
|
|
64
|
+
*
|
|
65
|
+
* function MyServerSideRender( { attributes, block } ) {
|
|
66
|
+
* const { content, status, error } = useServerSideRender( {
|
|
67
|
+
* attributes,
|
|
68
|
+
* block,
|
|
69
|
+
* } );
|
|
70
|
+
*
|
|
71
|
+
* if ( status === 'loading' ) {
|
|
72
|
+
* return <div>Loading...</div>;
|
|
73
|
+
* }
|
|
74
|
+
*
|
|
75
|
+
* if ( status === 'error' ) {
|
|
76
|
+
* return <div>Error: { error }</div>;
|
|
77
|
+
* }
|
|
78
|
+
*
|
|
79
|
+
* return <RawHTML>{ content }</RawHTML>;
|
|
80
|
+
* }
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @param {Object} args The hook configuration object.
|
|
84
|
+
* @param {Object} args.attributes The block attributes to be sent to the server for rendering.
|
|
85
|
+
* @param {string} args.block The identifier of the block to be serverside rendered. Example: 'core/archives'.
|
|
86
|
+
* @param {boolean} [args.skipBlockSupportAttributes=false] Whether to remove block support attributes before sending.
|
|
87
|
+
* @param {string} [args.httpMethod='GET'] The HTTP method to use ('GET' or 'POST'). Default is 'GET'.
|
|
88
|
+
* @param {Object} [args.urlQueryArgs] Additional query arguments to append to the request URL.
|
|
89
|
+
*
|
|
90
|
+
* @return {ServerSideRenderResponse} The server-side render response object.
|
|
91
|
+
*/
|
|
92
|
+
export function useServerSideRender(args) {
|
|
93
|
+
var _sanitizedAttributes;
|
|
94
|
+
const [response, setResponse] = useState({
|
|
95
|
+
status: 'idle'
|
|
96
|
+
});
|
|
97
|
+
const shouldDebounceRef = useRef(false);
|
|
98
|
+
const {
|
|
99
|
+
attributes,
|
|
100
|
+
block,
|
|
101
|
+
skipBlockSupportAttributes = false,
|
|
102
|
+
httpMethod = 'GET',
|
|
103
|
+
urlQueryArgs
|
|
104
|
+
} = args;
|
|
105
|
+
let sanitizedAttributes = attributes && __experimentalSanitizeBlockAttributes(block, attributes);
|
|
106
|
+
if (skipBlockSupportAttributes) {
|
|
107
|
+
sanitizedAttributes = removeBlockSupportAttributes(sanitizedAttributes);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// If httpMethod is 'POST', send the attributes in the request body instead of the URL.
|
|
111
|
+
// This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
|
|
112
|
+
const isPostRequest = 'POST' === httpMethod;
|
|
113
|
+
const urlAttributes = isPostRequest ? null : sanitizedAttributes;
|
|
114
|
+
const path = rendererPath(block, urlAttributes, urlQueryArgs);
|
|
115
|
+
const body = isPostRequest ? JSON.stringify({
|
|
116
|
+
attributes: (_sanitizedAttributes = sanitizedAttributes) !== null && _sanitizedAttributes !== void 0 ? _sanitizedAttributes : null
|
|
117
|
+
}) : undefined;
|
|
118
|
+
useEffect(() => {
|
|
119
|
+
const controller = new AbortController();
|
|
120
|
+
const debouncedFetch = debounce(function () {
|
|
121
|
+
{
|
|
122
|
+
setResponse({
|
|
123
|
+
status: 'loading'
|
|
124
|
+
});
|
|
125
|
+
apiFetch({
|
|
126
|
+
path,
|
|
127
|
+
method: isPostRequest ? 'POST' : 'GET',
|
|
128
|
+
body,
|
|
129
|
+
headers: isPostRequest ? {
|
|
130
|
+
'Content-Type': 'application/json'
|
|
131
|
+
} : {},
|
|
132
|
+
signal: controller.signal
|
|
133
|
+
}).then(res => {
|
|
134
|
+
setResponse({
|
|
135
|
+
status: 'success',
|
|
136
|
+
content: res ? res.rendered : ''
|
|
137
|
+
});
|
|
138
|
+
}).catch(error => {
|
|
139
|
+
// The request was aborted, do not update the response.
|
|
140
|
+
if (error.name === 'AbortError') {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
setResponse({
|
|
144
|
+
status: 'error',
|
|
145
|
+
error: error.message
|
|
146
|
+
});
|
|
147
|
+
}).finally(() => {
|
|
148
|
+
// Debounce requests after first fetch.
|
|
149
|
+
shouldDebounceRef.current = true;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}, shouldDebounceRef.current ? 500 : 0);
|
|
153
|
+
debouncedFetch();
|
|
154
|
+
return () => {
|
|
155
|
+
controller.abort();
|
|
156
|
+
debouncedFetch.cancel();
|
|
157
|
+
};
|
|
158
|
+
}, [path, isPostRequest, body]);
|
|
159
|
+
return response;
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=hook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["debounce","useEffect","useState","useRef","apiFetch","addQueryArgs","__experimentalSanitizeBlockAttributes","rendererPath","block","attributes","urlQueryArgs","context","removeBlockSupportAttributes","backgroundColor","borderColor","fontFamily","fontSize","gradient","textColor","className","restAttributes","border","color","elements","shadow","spacing","typography","restStyles","style","useServerSideRender","args","_sanitizedAttributes","response","setResponse","status","shouldDebounceRef","skipBlockSupportAttributes","httpMethod","sanitizedAttributes","isPostRequest","urlAttributes","path","body","JSON","stringify","undefined","controller","AbortController","debouncedFetch","method","headers","signal","then","res","content","rendered","catch","error","name","message","finally","current","abort","cancel"],"sources":["@wordpress/server-side-render/src/hook.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { debounce } from '@wordpress/compose';\nimport { useEffect, useState, useRef } from '@wordpress/element';\nimport apiFetch from '@wordpress/api-fetch';\nimport { addQueryArgs } from '@wordpress/url';\nimport { __experimentalSanitizeBlockAttributes } from '@wordpress/blocks';\n\nexport function rendererPath( block, attributes = null, urlQueryArgs = {} ) {\n\treturn addQueryArgs( `/wp/v2/block-renderer/${ block }`, {\n\t\tcontext: 'edit',\n\t\t...( null !== attributes ? { attributes } : {} ),\n\t\t...urlQueryArgs,\n\t} );\n}\n\nexport function removeBlockSupportAttributes( attributes ) {\n\tconst {\n\t\tbackgroundColor,\n\t\tborderColor,\n\t\tfontFamily,\n\t\tfontSize,\n\t\tgradient,\n\t\ttextColor,\n\t\tclassName,\n\t\t...restAttributes\n\t} = attributes;\n\n\tconst {\n\t\tborder,\n\t\tcolor,\n\t\telements,\n\t\tshadow,\n\t\tspacing,\n\t\ttypography,\n\t\t...restStyles\n\t} = attributes?.style || {};\n\n\treturn {\n\t\t...restAttributes,\n\t\tstyle: restStyles,\n\t};\n}\n\n/**\n * @typedef {Object} ServerSideRenderResponse\n * @property {string} status - The current request status: 'idle', 'loading', 'success', or 'error'.\n * @property {string} [content] - The rendered block content (available when status is 'success').\n * @property {string} [error] - The error message (available when status is 'error').\n */\n\n/**\n * A hook for server-side rendering a preview of dynamic blocks to display in the editor.\n *\n * Handles fetching server-rendered previews for blocks, managing loading states,\n * and automatically debouncing requests to prevent excessive API calls. It supports both\n * GET and POST requests, with POST requests used for larger attribute payloads.\n *\n * @example\n * Basic usage:\n *\n * ```jsx\n * import { RawHTML } from '@wordpress/element';\n * import { useServerSideRender } from '@wordpress/server-side-render';\n *\n * function MyServerSideRender( { attributes, block } ) {\n * const { content, status, error } = useServerSideRender( {\n * attributes,\n * block,\n * } );\n *\n * if ( status === 'loading' ) {\n * return <div>Loading...</div>;\n * }\n *\n * if ( status === 'error' ) {\n * return <div>Error: { error }</div>;\n * }\n *\n * return <RawHTML>{ content }</RawHTML>;\n * }\n * ```\n *\n * @param {Object} args The hook configuration object.\n * @param {Object} args.attributes The block attributes to be sent to the server for rendering.\n * @param {string} args.block The identifier of the block to be serverside rendered. Example: 'core/archives'.\n * @param {boolean} [args.skipBlockSupportAttributes=false] Whether to remove block support attributes before sending.\n * @param {string} [args.httpMethod='GET'] The HTTP method to use ('GET' or 'POST'). Default is 'GET'.\n * @param {Object} [args.urlQueryArgs] Additional query arguments to append to the request URL.\n *\n * @return {ServerSideRenderResponse} The server-side render response object.\n */\nexport function useServerSideRender( args ) {\n\tconst [ response, setResponse ] = useState( { status: 'idle' } );\n\tconst shouldDebounceRef = useRef( false );\n\n\tconst {\n\t\tattributes,\n\t\tblock,\n\t\tskipBlockSupportAttributes = false,\n\t\thttpMethod = 'GET',\n\t\turlQueryArgs,\n\t} = args;\n\n\tlet sanitizedAttributes =\n\t\tattributes &&\n\t\t__experimentalSanitizeBlockAttributes( block, attributes );\n\n\tif ( skipBlockSupportAttributes ) {\n\t\tsanitizedAttributes =\n\t\t\tremoveBlockSupportAttributes( sanitizedAttributes );\n\t}\n\n\t// If httpMethod is 'POST', send the attributes in the request body instead of the URL.\n\t// This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.\n\tconst isPostRequest = 'POST' === httpMethod;\n\tconst urlAttributes = isPostRequest ? null : sanitizedAttributes;\n\tconst path = rendererPath( block, urlAttributes, urlQueryArgs );\n\tconst body = isPostRequest\n\t\t? JSON.stringify( { attributes: sanitizedAttributes ?? null } )\n\t\t: undefined;\n\n\tuseEffect( () => {\n\t\tconst controller = new AbortController();\n\t\tconst debouncedFetch = debounce(\n\t\t\tfunction () {\n\t\t\t\t{\n\t\t\t\t\tsetResponse( { status: 'loading' } );\n\n\t\t\t\t\tapiFetch( {\n\t\t\t\t\t\tpath,\n\t\t\t\t\t\tmethod: isPostRequest ? 'POST' : 'GET',\n\t\t\t\t\t\tbody,\n\t\t\t\t\t\theaders: isPostRequest\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t\t\t\t }\n\t\t\t\t\t\t\t: {},\n\t\t\t\t\t\tsignal: controller.signal,\n\t\t\t\t\t} )\n\t\t\t\t\t\t.then( ( res ) => {\n\t\t\t\t\t\t\tsetResponse( {\n\t\t\t\t\t\t\t\tstatus: 'success',\n\t\t\t\t\t\t\t\tcontent: res ? res.rendered : '',\n\t\t\t\t\t\t\t} );\n\t\t\t\t\t\t} )\n\t\t\t\t\t\t.catch( ( error ) => {\n\t\t\t\t\t\t\t// The request was aborted, do not update the response.\n\t\t\t\t\t\t\tif ( error.name === 'AbortError' ) {\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tsetResponse( {\n\t\t\t\t\t\t\t\tstatus: 'error',\n\t\t\t\t\t\t\t\terror: error.message,\n\t\t\t\t\t\t\t} );\n\t\t\t\t\t\t} )\n\t\t\t\t\t\t.finally( () => {\n\t\t\t\t\t\t\t// Debounce requests after first fetch.\n\t\t\t\t\t\t\tshouldDebounceRef.current = true;\n\t\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t},\n\t\t\tshouldDebounceRef.current ? 500 : 0\n\t\t);\n\n\t\tdebouncedFetch();\n\n\t\treturn () => {\n\t\t\tcontroller.abort();\n\t\t\tdebouncedFetch.cancel();\n\t\t};\n\t}, [ path, isPostRequest, body ] );\n\n\treturn response;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,QAAQ,QAAQ,oBAAoB;AAC7C,SAASC,SAAS,EAAEC,QAAQ,EAAEC,MAAM,QAAQ,oBAAoB;AAChE,OAAOC,QAAQ,MAAM,sBAAsB;AAC3C,SAASC,YAAY,QAAQ,gBAAgB;AAC7C,SAASC,qCAAqC,QAAQ,mBAAmB;AAEzE,OAAO,SAASC,YAAYA,CAAEC,KAAK,EAAEC,UAAU,GAAG,IAAI,EAAEC,YAAY,GAAG,CAAC,CAAC,EAAG;EAC3E,OAAOL,YAAY,CAAE,yBAA0BG,KAAK,EAAG,EAAE;IACxDG,OAAO,EAAE,MAAM;IACf,IAAK,IAAI,KAAKF,UAAU,GAAG;MAAEA;IAAW,CAAC,GAAG,CAAC,CAAC,CAAE;IAChD,GAAGC;EACJ,CAAE,CAAC;AACJ;AAEA,OAAO,SAASE,4BAA4BA,CAAEH,UAAU,EAAG;EAC1D,MAAM;IACLI,eAAe;IACfC,WAAW;IACXC,UAAU;IACVC,QAAQ;IACRC,QAAQ;IACRC,SAAS;IACTC,SAAS;IACT,GAAGC;EACJ,CAAC,GAAGX,UAAU;EAEd,MAAM;IACLY,MAAM;IACNC,KAAK;IACLC,QAAQ;IACRC,MAAM;IACNC,OAAO;IACPC,UAAU;IACV,GAAGC;EACJ,CAAC,GAAGlB,UAAU,EAAEmB,KAAK,IAAI,CAAC,CAAC;EAE3B,OAAO;IACN,GAAGR,cA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|
3
|
-
*/
|
|
4
|
-
import { useMemo } from '@wordpress/element';
|
|
5
|
-
import { useSelect } from '@wordpress/data';
|
|
6
|
-
|
|
7
1
|
/**
|
|
8
2
|
* Internal dependencies
|
|
9
3
|
*/
|
|
10
|
-
import
|
|
4
|
+
import { ServerSideRenderWithPostId } from './server-side-render';
|
|
5
|
+
import { useServerSideRender } from './hook';
|
|
11
6
|
|
|
12
7
|
/**
|
|
13
|
-
*
|
|
8
|
+
* A compatibility layer for the `ServerSideRender` component when used with `wp` global namespace.
|
|
9
|
+
*
|
|
10
|
+
* @deprecated Use `ServerSideRender` non-default export instead.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```js
|
|
14
|
+
* import ServerSideRender from '@wordpress/server-side-render';
|
|
15
|
+
* ```
|
|
14
16
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const currentPostId = useSelect(select => {
|
|
22
|
-
// FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.
|
|
23
|
-
// It is used by blocks that can be loaded into a *non-post* block editor.
|
|
24
|
-
// eslint-disable-next-line @wordpress/data-no-store-string-literals
|
|
25
|
-
const postId = select('core/editor')?.getCurrentPostId();
|
|
26
|
-
|
|
27
|
-
// For templates and template parts we use a custom ID format.
|
|
28
|
-
// Since they aren't real posts, we don't want to use their ID
|
|
29
|
-
// for server-side rendering. Since they use a string based ID,
|
|
30
|
-
// we can assume real post IDs are numbers.
|
|
31
|
-
return postId && typeof postId === 'number' ? postId : null;
|
|
32
|
-
}, []);
|
|
33
|
-
const newUrlQueryArgs = useMemo(() => {
|
|
34
|
-
if (!currentPostId) {
|
|
35
|
-
return urlQueryArgs;
|
|
36
|
-
}
|
|
37
|
-
return {
|
|
38
|
-
post_id: currentPostId,
|
|
39
|
-
...urlQueryArgs
|
|
40
|
-
};
|
|
41
|
-
}, [currentPostId, urlQueryArgs]);
|
|
42
|
-
return /*#__PURE__*/_jsx(ServerSideRender, {
|
|
43
|
-
urlQueryArgs: newUrlQueryArgs,
|
|
44
|
-
...props
|
|
45
|
-
});
|
|
46
|
-
}
|
|
17
|
+
const ServerSideRenderCompat = ServerSideRenderWithPostId;
|
|
18
|
+
ServerSideRenderCompat.ServerSideRender = ServerSideRenderWithPostId;
|
|
19
|
+
ServerSideRenderCompat.useServerSideRender = useServerSideRender;
|
|
20
|
+
export { ServerSideRenderWithPostId as ServerSideRender };
|
|
21
|
+
export { useServerSideRender };
|
|
22
|
+
export default ServerSideRenderCompat;
|
|
47
23
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["ServerSideRenderWithPostId","useServerSideRender","ServerSideRenderCompat","ServerSideRender"],"sources":["@wordpress/server-side-render/src/index.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { ServerSideRenderWithPostId } from './server-side-render';\nimport { useServerSideRender } from './hook';\n\n/**\n * A compatibility layer for the `ServerSideRender` component when used with `wp` global namespace.\n *\n * @deprecated Use `ServerSideRender` non-default export instead.\n *\n * @example\n * ```js\n * import ServerSideRender from '@wordpress/server-side-render';\n * ```\n */\nconst ServerSideRenderCompat = ServerSideRenderWithPostId;\nServerSideRenderCompat.ServerSideRender = ServerSideRenderWithPostId;\nServerSideRenderCompat.useServerSideRender = useServerSideRender;\n\nexport { ServerSideRenderWithPostId as ServerSideRender };\nexport { useServerSideRender };\nexport default ServerSideRenderCompat;\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,0BAA0B,QAAQ,sBAAsB;AACjE,SAASC,mBAAmB,QAAQ,QAAQ;;AAE5C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,sBAAsB,GAAGF,0BAA0B;AACzDE,sBAAsB,CAACC,gBAAgB,GAAGH,0BAA0B;AACpEE,sBAAsB,CAACD,mBAAmB,GAAGA,mBAAmB;AAEhE,SAASD,0BAA0B,IAAIG,gBAAgB;AACvD,SAASF,mBAAmB;AAC5B,eAAeC,sBAAsB","ignoreList":[]}
|