@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
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -20,122 +20,110 @@ npm install @wordpress/server-side-render --save
|
|
|
20
20
|
|
|
21
21
|
_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for such language features and APIs, you should include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._
|
|
22
22
|
|
|
23
|
-
##
|
|
23
|
+
## API
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
<!-- START TOKEN(Autogenerated API docs) -->
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
### default
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
> **Deprecated** Use `ServerSideRender` non-default export instead.
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
E.g: `{ displayAsDropdown: true }`, `{ showHierarchy: true }`, etc...
|
|
31
|
+
A compatibility layer for the `ServerSideRender` component when used with `wp` global namespace.
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
- Required: No
|
|
33
|
+
_Usage_
|
|
36
34
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
Examples: "core/archives", "core/latest-comments", "core/rss", etc...
|
|
41
|
-
|
|
42
|
-
- Type: `String`
|
|
43
|
-
- Required: Yes
|
|
44
|
-
|
|
45
|
-
### className
|
|
46
|
-
|
|
47
|
-
A class added to the DOM element that wraps the server side rendered block.
|
|
48
|
-
Examples: "my-custom-server-side-rendered".
|
|
49
|
-
|
|
50
|
-
- Type: `String`
|
|
51
|
-
- Required: No
|
|
52
|
-
|
|
53
|
-
### httpMethod
|
|
35
|
+
```js
|
|
36
|
+
import ServerSideRender from '@wordpress/server-side-render';
|
|
37
|
+
```
|
|
54
38
|
|
|
55
|
-
|
|
39
|
+
### ServerSideRender
|
|
56
40
|
|
|
57
|
-
-
|
|
58
|
-
- Required: No
|
|
59
|
-
- Default: 'GET'
|
|
41
|
+
A component that renders server-side content for blocks.
|
|
60
42
|
|
|
61
|
-
|
|
43
|
+
Note: URL query will include the current post ID when applicable. This is useful for blocks that depend on the context of the current post for rendering.
|
|
62
44
|
|
|
63
|
-
|
|
45
|
+
_Usage_
|
|
64
46
|
|
|
65
|
-
|
|
66
|
-
-
|
|
67
|
-
|
|
47
|
+
```jsx
|
|
48
|
+
import { ServerSideRender } from '@wordpress/server-side-render';
|
|
49
|
+
// Legacy import for WordPress 6.8 and earlier
|
|
50
|
+
// import { default as ServerSideRender } from '@wordpress/server-side-render';
|
|
51
|
+
|
|
52
|
+
function Example() {
|
|
53
|
+
return (
|
|
54
|
+
<ServerSideRender
|
|
55
|
+
block="core/archives"
|
|
56
|
+
attributes={ { showPostCounts: true } }
|
|
57
|
+
urlQueryArgs={ { customArg: 'value' } }
|
|
58
|
+
className="custom-class"
|
|
59
|
+
/>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
```
|
|
68
63
|
|
|
69
|
-
|
|
64
|
+
_Parameters_
|
|
70
65
|
|
|
71
|
-
|
|
72
|
-
|
|
66
|
+
- _props_ `Object`: Component props.
|
|
67
|
+
- _props.block_ `string`: The identifier of the block to be serverside rendered.
|
|
68
|
+
- _props.attributes_ `Object`: The block attributes to be sent to the server for rendering.
|
|
69
|
+
- _props.className_ `[string]`: Additional classes to apply to the wrapper element.
|
|
70
|
+
- _props.httpMethod_ `[string]`: The HTTP method to use ('GET' or 'POST'). Default is 'GET'
|
|
71
|
+
- _props.urlQueryArgs_ `[Object]`: Additional query arguments to append to the request URL.
|
|
72
|
+
- _props.skipBlockSupportAttributes_ `[boolean]`: Whether to remove block support attributes before sending.
|
|
73
|
+
- _props.EmptyResponsePlaceholder_ `[Function]`: Component rendered when the API response is empty.
|
|
74
|
+
- _props.ErrorResponsePlaceholder_ `[Function]`: Component rendered when the API response is an error.
|
|
75
|
+
- _props.LoadingResponsePlaceholder_ `[Function]`: Component rendered while the API request is loading.
|
|
73
76
|
|
|
74
|
-
|
|
75
|
-
- Required: No
|
|
77
|
+
_Returns_
|
|
76
78
|
|
|
77
|
-
|
|
79
|
+
- `JSX.Element`: The rendered server-side content.
|
|
78
80
|
|
|
79
|
-
|
|
81
|
+
### useServerSideRender
|
|
80
82
|
|
|
81
|
-
-
|
|
82
|
-
- Required: No
|
|
83
|
+
A hook for server-side rendering a preview of dynamic blocks to display in the editor.
|
|
83
84
|
|
|
84
|
-
|
|
85
|
+
Handles fetching server-rendered previews for blocks, managing loading states, and automatically debouncing requests to prevent excessive API calls. It supports both GET and POST requests, with POST requests used for larger attribute payloads.
|
|
85
86
|
|
|
86
|
-
|
|
87
|
+
_Usage_
|
|
87
88
|
|
|
88
|
-
|
|
89
|
-
- Required: No
|
|
89
|
+
Basic usage:
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
```jsx
|
|
92
|
+
import { RawHTML } from '@wordpress/element';
|
|
93
|
+
import { useServerSideRender } from '@wordpress/server-side-render';
|
|
92
94
|
|
|
93
|
-
|
|
95
|
+
function MyServerSideRender( { attributes, block } ) {
|
|
96
|
+
const { content, status, error } = useServerSideRender( {
|
|
97
|
+
attributes,
|
|
98
|
+
block,
|
|
99
|
+
} );
|
|
94
100
|
|
|
95
|
-
|
|
96
|
-
|
|
101
|
+
if ( status === 'loading' ) {
|
|
102
|
+
return <div>Loading...</div>;
|
|
103
|
+
}
|
|
97
104
|
|
|
98
|
-
|
|
105
|
+
if ( status === 'error' ) {
|
|
106
|
+
return <div>Error: { error }</div>;
|
|
107
|
+
}
|
|
99
108
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
<ServerSideRender LoadingResponsePlaceholder={ MyAmazingPlaceholder } />
|
|
103
|
-
);
|
|
109
|
+
return <RawHTML>{ content }</RawHTML>;
|
|
110
|
+
}
|
|
104
111
|
```
|
|
105
112
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
Render core/archives preview.
|
|
113
|
+
_Parameters_
|
|
109
114
|
|
|
110
|
-
|
|
111
|
-
|
|
115
|
+
- _args_ `Object`: The hook configuration object.
|
|
116
|
+
- _args.attributes_ `Object`: The block attributes to be sent to the server for rendering.
|
|
117
|
+
- _args.block_ `string`: The identifier of the block to be serverside rendered. Example: 'core/archives'.
|
|
118
|
+
- _args.skipBlockSupportAttributes_ `[boolean]`: Whether to remove block support attributes before sending.
|
|
119
|
+
- _args.httpMethod_ `[string]`: The HTTP method to use ('GET' or 'POST'). Default is 'GET'.
|
|
120
|
+
- _args.urlQueryArgs_ `[Object]`: Additional query arguments to append to the request URL.
|
|
112
121
|
|
|
113
|
-
|
|
114
|
-
<ServerSideRender
|
|
115
|
-
block="core/archives"
|
|
116
|
-
attributes={ {
|
|
117
|
-
showPostCounts: true,
|
|
118
|
-
displayAsDropdown: false,
|
|
119
|
-
} }
|
|
120
|
-
/>
|
|
121
|
-
);
|
|
122
|
-
```
|
|
122
|
+
_Returns_
|
|
123
123
|
|
|
124
|
-
|
|
124
|
+
- `ServerSideRenderResponse`: The server-side render response object.
|
|
125
125
|
|
|
126
|
-
|
|
127
|
-
const { serverSideRender: ServerSideRender } = wp;
|
|
128
|
-
|
|
129
|
-
const MyServerSideRender = () => (
|
|
130
|
-
<ServerSideRender
|
|
131
|
-
block="core/archives"
|
|
132
|
-
attributes={ {
|
|
133
|
-
showPostCounts: true,
|
|
134
|
-
displayAsDropdown: false,
|
|
135
|
-
} }
|
|
136
|
-
/>
|
|
137
|
-
);
|
|
138
|
-
```
|
|
126
|
+
<!-- END TOKEN(Autogenerated API docs) -->
|
|
139
127
|
|
|
140
128
|
## Output
|
|
141
129
|
|
package/build/hook.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.removeBlockSupportAttributes = removeBlockSupportAttributes;
|
|
8
|
+
exports.rendererPath = rendererPath;
|
|
9
|
+
exports.useServerSideRender = useServerSideRender;
|
|
10
|
+
var _compose = require("@wordpress/compose");
|
|
11
|
+
var _element = require("@wordpress/element");
|
|
12
|
+
var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
|
|
13
|
+
var _url = require("@wordpress/url");
|
|
14
|
+
var _blocks = require("@wordpress/blocks");
|
|
15
|
+
/**
|
|
16
|
+
* WordPress dependencies
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
function rendererPath(block, attributes = null, urlQueryArgs = {}) {
|
|
20
|
+
return (0, _url.addQueryArgs)(`/wp/v2/block-renderer/${block}`, {
|
|
21
|
+
context: 'edit',
|
|
22
|
+
...(null !== attributes ? {
|
|
23
|
+
attributes
|
|
24
|
+
} : {}),
|
|
25
|
+
...urlQueryArgs
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function removeBlockSupportAttributes(attributes) {
|
|
29
|
+
const {
|
|
30
|
+
backgroundColor,
|
|
31
|
+
borderColor,
|
|
32
|
+
fontFamily,
|
|
33
|
+
fontSize,
|
|
34
|
+
gradient,
|
|
35
|
+
textColor,
|
|
36
|
+
className,
|
|
37
|
+
...restAttributes
|
|
38
|
+
} = attributes;
|
|
39
|
+
const {
|
|
40
|
+
border,
|
|
41
|
+
color,
|
|
42
|
+
elements,
|
|
43
|
+
shadow,
|
|
44
|
+
spacing,
|
|
45
|
+
typography,
|
|
46
|
+
...restStyles
|
|
47
|
+
} = attributes?.style || {};
|
|
48
|
+
return {
|
|
49
|
+
...restAttributes,
|
|
50
|
+
style: restStyles
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @typedef {Object} ServerSideRenderResponse
|
|
56
|
+
* @property {string} status - The current request status: 'idle', 'loading', 'success', or 'error'.
|
|
57
|
+
* @property {string} [content] - The rendered block content (available when status is 'success').
|
|
58
|
+
* @property {string} [error] - The error message (available when status is 'error').
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A hook for server-side rendering a preview of dynamic blocks to display in the editor.
|
|
63
|
+
*
|
|
64
|
+
* Handles fetching server-rendered previews for blocks, managing loading states,
|
|
65
|
+
* and automatically debouncing requests to prevent excessive API calls. It supports both
|
|
66
|
+
* GET and POST requests, with POST requests used for larger attribute payloads.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* Basic usage:
|
|
70
|
+
*
|
|
71
|
+
* ```jsx
|
|
72
|
+
* import { RawHTML } from '@wordpress/element';
|
|
73
|
+
* import { useServerSideRender } from '@wordpress/server-side-render';
|
|
74
|
+
*
|
|
75
|
+
* function MyServerSideRender( { attributes, block } ) {
|
|
76
|
+
* const { content, status, error } = useServerSideRender( {
|
|
77
|
+
* attributes,
|
|
78
|
+
* block,
|
|
79
|
+
* } );
|
|
80
|
+
*
|
|
81
|
+
* if ( status === 'loading' ) {
|
|
82
|
+
* return <div>Loading...</div>;
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* if ( status === 'error' ) {
|
|
86
|
+
* return <div>Error: { error }</div>;
|
|
87
|
+
* }
|
|
88
|
+
*
|
|
89
|
+
* return <RawHTML>{ content }</RawHTML>;
|
|
90
|
+
* }
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @param {Object} args The hook configuration object.
|
|
94
|
+
* @param {Object} args.attributes The block attributes to be sent to the server for rendering.
|
|
95
|
+
* @param {string} args.block The identifier of the block to be serverside rendered. Example: 'core/archives'.
|
|
96
|
+
* @param {boolean} [args.skipBlockSupportAttributes=false] Whether to remove block support attributes before sending.
|
|
97
|
+
* @param {string} [args.httpMethod='GET'] The HTTP method to use ('GET' or 'POST'). Default is 'GET'.
|
|
98
|
+
* @param {Object} [args.urlQueryArgs] Additional query arguments to append to the request URL.
|
|
99
|
+
*
|
|
100
|
+
* @return {ServerSideRenderResponse} The server-side render response object.
|
|
101
|
+
*/
|
|
102
|
+
function useServerSideRender(args) {
|
|
103
|
+
var _sanitizedAttributes;
|
|
104
|
+
const [response, setResponse] = (0, _element.useState)({
|
|
105
|
+
status: 'idle'
|
|
106
|
+
});
|
|
107
|
+
const shouldDebounceRef = (0, _element.useRef)(false);
|
|
108
|
+
const {
|
|
109
|
+
attributes,
|
|
110
|
+
block,
|
|
111
|
+
skipBlockSupportAttributes = false,
|
|
112
|
+
httpMethod = 'GET',
|
|
113
|
+
urlQueryArgs
|
|
114
|
+
} = args;
|
|
115
|
+
let sanitizedAttributes = attributes && (0, _blocks.__experimentalSanitizeBlockAttributes)(block, attributes);
|
|
116
|
+
if (skipBlockSupportAttributes) {
|
|
117
|
+
sanitizedAttributes = removeBlockSupportAttributes(sanitizedAttributes);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// If httpMethod is 'POST', send the attributes in the request body instead of the URL.
|
|
121
|
+
// This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
|
|
122
|
+
const isPostRequest = 'POST' === httpMethod;
|
|
123
|
+
const urlAttributes = isPostRequest ? null : sanitizedAttributes;
|
|
124
|
+
const path = rendererPath(block, urlAttributes, urlQueryArgs);
|
|
125
|
+
const body = isPostRequest ? JSON.stringify({
|
|
126
|
+
attributes: (_sanitizedAttributes = sanitizedAttributes) !== null && _sanitizedAttributes !== void 0 ? _sanitizedAttributes : null
|
|
127
|
+
}) : undefined;
|
|
128
|
+
(0, _element.useEffect)(() => {
|
|
129
|
+
const controller = new AbortController();
|
|
130
|
+
const debouncedFetch = (0, _compose.debounce)(function () {
|
|
131
|
+
{
|
|
132
|
+
setResponse({
|
|
133
|
+
status: 'loading'
|
|
134
|
+
});
|
|
135
|
+
(0, _apiFetch.default)({
|
|
136
|
+
path,
|
|
137
|
+
method: isPostRequest ? 'POST' : 'GET',
|
|
138
|
+
body,
|
|
139
|
+
headers: isPostRequest ? {
|
|
140
|
+
'Content-Type': 'application/json'
|
|
141
|
+
} : {},
|
|
142
|
+
signal: controller.signal
|
|
143
|
+
}).then(res => {
|
|
144
|
+
setResponse({
|
|
145
|
+
status: 'success',
|
|
146
|
+
content: res ? res.rendered : ''
|
|
147
|
+
});
|
|
148
|
+
}).catch(error => {
|
|
149
|
+
// The request was aborted, do not update the response.
|
|
150
|
+
if (error.name === 'AbortError') {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
setResponse({
|
|
154
|
+
status: 'error',
|
|
155
|
+
error: error.message
|
|
156
|
+
});
|
|
157
|
+
}).finally(() => {
|
|
158
|
+
// Debounce requests after first fetch.
|
|
159
|
+
shouldDebounceRef.current = true;
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}, shouldDebounceRef.current ? 500 : 0);
|
|
163
|
+
debouncedFetch();
|
|
164
|
+
return () => {
|
|
165
|
+
controller.abort();
|
|
166
|
+
debouncedFetch.cancel();
|
|
167
|
+
};
|
|
168
|
+
}, [path, isPostRequest, body]);
|
|
169
|
+
return response;
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=hook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_compose","require","_element","_apiFetch","_interopRequireDefault","_url","_blocks","rendererPath","block","attributes","urlQueryArgs","addQueryArgs","context","removeBlockSupportAttributes","backgroundColor","borderColor","fontFamily","fontSize","gradient","textColor","className","restAttributes","border","color","elements","shadow","spacing","typography","restStyles","style","useServerSideRender","args","_sanitizedAttributes","response","setResponse","useState","status","shouldDebounceRef","useRef","skipBlockSupportAttributes","httpMethod","sanitizedAttributes","__experimentalSanitizeBlockAttributes","isPostRequest","urlAttributes","path","body","JSON","stringify","undefined","useEffect","controller","AbortController","debouncedFetch","debounce","apiFetch","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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e,MAAM,EAAEP,UAAU,CAACO;QACpB,CAAE,CAAC,CACDC,IAAI,CAAIC,GAAG,IAAM;UACjB1B,WAAW,CAAE;YACZE,MAAM,EAAE,SAAS;YACjByB,OAAO,EAAED,GAAG,GAAGA,GAAG,CAACE,QAAQ,GAAG;UAC/B,CAAE,CAAC;QACJ,CAAE,CAAC,CACFC,KAAK,CAAIC,KAAK,IAAM;UACpB;UACA,IAAKA,KAAK,CAACC,IAAI,KAAK,YAAY,EAAG;YAClC;UACD;UAEA/B,WAAW,CAAE;YACZE,MAAM,EAAE,OAAO;YACf4B,KAAK,EAAEA,KAAK,CAACE;UACd,CAAE,CAAC;QACJ,CAAE,CAAC,CACFC,OAAO,CAAE,MAAM;UACf;UACA9B,iBAAiB,CAAC+B,OAAO,GAAG,IAAI;QACjC,CAAE,CAAC;MACL;IACD,CAAC,EACD/B,iBAAiB,CAAC+B,OAAO,GAAG,GAAG,GAAG,CACnC,CAAC;IAEDf,cAAc,CAAC,CAAC;IAEhB,OAAO,MAAM;MACZF,UAAU,CAACkB,KAAK,CAAC,CAAC;MAClBhB,cAAc,CAACiB,MAAM,CAAC,CAAC;IACxB,CAAC;EACF,CAAC,EAAE,CAAEzB,IAAI,EAAEF,aAAa,EAAEG,IAAI,CAAG,CAAC;EAElC,OAAOb,QAAQ;AAChB","ignoreList":[]}
|
package/build/index.js
CHANGED
|
@@ -1,53 +1,39 @@
|
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
Object.defineProperty(exports, "ServerSideRender", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _serverSideRender.ServerSideRenderWithPostId;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
exports.default = void 0;
|
|
13
|
+
Object.defineProperty(exports, "useServerSideRender", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () {
|
|
16
|
+
return _hook.useServerSideRender;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
var _serverSideRender = require("./server-side-render");
|
|
20
|
+
var _hook = require("./hook");
|
|
16
21
|
/**
|
|
17
22
|
* Internal dependencies
|
|
18
23
|
*/
|
|
19
24
|
|
|
20
25
|
/**
|
|
21
|
-
*
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
// Since they aren't real posts, we don't want to use their ID
|
|
35
|
-
// for server-side rendering. Since they use a string based ID,
|
|
36
|
-
// we can assume real post IDs are numbers.
|
|
37
|
-
return postId && typeof postId === 'number' ? postId : null;
|
|
38
|
-
}, []);
|
|
39
|
-
const newUrlQueryArgs = (0, _element.useMemo)(() => {
|
|
40
|
-
if (!currentPostId) {
|
|
41
|
-
return urlQueryArgs;
|
|
42
|
-
}
|
|
43
|
-
return {
|
|
44
|
-
post_id: currentPostId,
|
|
45
|
-
...urlQueryArgs
|
|
46
|
-
};
|
|
47
|
-
}, [currentPostId, urlQueryArgs]);
|
|
48
|
-
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_serverSideRender.default, {
|
|
49
|
-
urlQueryArgs: newUrlQueryArgs,
|
|
50
|
-
...props
|
|
51
|
-
});
|
|
52
|
-
}
|
|
26
|
+
* A compatibility layer for the `ServerSideRender` component when used with `wp` global namespace.
|
|
27
|
+
*
|
|
28
|
+
* @deprecated Use `ServerSideRender` non-default export instead.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```js
|
|
32
|
+
* import ServerSideRender from '@wordpress/server-side-render';
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
const ServerSideRenderCompat = _serverSideRender.ServerSideRenderWithPostId;
|
|
36
|
+
ServerSideRenderCompat.ServerSideRender = _serverSideRender.ServerSideRenderWithPostId;
|
|
37
|
+
ServerSideRenderCompat.useServerSideRender = _hook.useServerSideRender;
|
|
38
|
+
var _default = exports.default = ServerSideRenderCompat;
|
|
53
39
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["_serverSideRender","require","_hook","ServerSideRenderCompat","ServerSideRenderWithPostId","ServerSideRender","useServerSideRender","_default","exports","default"],"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":";;;;;;;;;;;;;;;;;;AAGA,IAAAA,iBAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AAJA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAME,sBAAsB,GAAGC,4CAA0B;AACzDD,sBAAsB,CAACE,gBAAgB,GAAGD,4CAA0B;AACpED,sBAAsB,CAACG,mBAAmB,GAAGA,yBAAmB;AAAC,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAIlDN,sBAAsB","ignoreList":[]}
|