@wordpress/server-side-render 4.16.0 → 4.17.1
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 +2 -0
- package/build/index.js +2 -10
- package/build/index.js.map +1 -1
- package/build/server-side-render.js +18 -37
- package/build/server-side-render.js.map +1 -1
- package/build-module/index.js +4 -8
- package/build-module/index.js.map +1 -1
- package/build-module/server-side-render.js +18 -28
- package/build-module/server-side-render.js.map +1 -1
- package/package.json +11 -11
package/CHANGELOG.md
CHANGED
package/build/index.js
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _element = require("@wordpress/element");
|
|
11
|
-
|
|
12
9
|
var _data = require("@wordpress/data");
|
|
13
|
-
|
|
14
10
|
var _serverSideRender = _interopRequireDefault(require("./server-side-render"));
|
|
15
|
-
|
|
16
11
|
/**
|
|
17
12
|
* WordPress dependencies
|
|
18
13
|
*/
|
|
@@ -30,20 +25,18 @@ const ExportedServerSideRender = (0, _data.withSelect)(select => {
|
|
|
30
25
|
// It is used by blocks that can be loaded into a *non-post* block editor.
|
|
31
26
|
// eslint-disable-next-line @wordpress/data-no-store-string-literals
|
|
32
27
|
const coreEditorSelect = select('core/editor');
|
|
33
|
-
|
|
34
28
|
if (coreEditorSelect) {
|
|
35
|
-
const currentPostId = coreEditorSelect.getCurrentPostId();
|
|
29
|
+
const currentPostId = coreEditorSelect.getCurrentPostId();
|
|
30
|
+
// For templates and template parts we use a custom ID format.
|
|
36
31
|
// Since they aren't real posts, we don't want to use their ID
|
|
37
32
|
// for server-side rendering. Since they use a string based ID,
|
|
38
33
|
// we can assume real post IDs are numbers.
|
|
39
|
-
|
|
40
34
|
if (currentPostId && typeof currentPostId === 'number') {
|
|
41
35
|
return {
|
|
42
36
|
currentPostId
|
|
43
37
|
};
|
|
44
38
|
}
|
|
45
39
|
}
|
|
46
|
-
|
|
47
40
|
return EMPTY_OBJECT;
|
|
48
41
|
})(({
|
|
49
42
|
urlQueryArgs = EMPTY_OBJECT,
|
|
@@ -54,7 +47,6 @@ const ExportedServerSideRender = (0, _data.withSelect)(select => {
|
|
|
54
47
|
if (!currentPostId) {
|
|
55
48
|
return urlQueryArgs;
|
|
56
49
|
}
|
|
57
|
-
|
|
58
50
|
return {
|
|
59
51
|
post_id: currentPostId,
|
|
60
52
|
...urlQueryArgs
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["_element","require","_data","_serverSideRender","_interopRequireDefault","EMPTY_OBJECT","ExportedServerSideRender","withSelect","select","coreEditorSelect","currentPostId","getCurrentPostId","urlQueryArgs","props","newUrlQueryArgs","useMemo","post_id","createElement","default","_default","exports"],"sources":["@wordpress/server-side-render/src/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useMemo } from '@wordpress/element';\nimport { withSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport ServerSideRender from './server-side-render';\n\n/**\n * Constants\n */\nconst EMPTY_OBJECT = {};\n\nconst ExportedServerSideRender = withSelect( ( select ) => {\n\t// FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.\n\t// It is used by blocks that can be loaded into a *non-post* block editor.\n\t// eslint-disable-next-line @wordpress/data-no-store-string-literals\n\tconst coreEditorSelect = select( 'core/editor' );\n\tif ( coreEditorSelect ) {\n\t\tconst currentPostId = coreEditorSelect.getCurrentPostId();\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\tif ( currentPostId && typeof currentPostId === 'number' ) {\n\t\t\treturn {\n\t\t\t\tcurrentPostId,\n\t\t\t};\n\t\t}\n\t}\n\treturn EMPTY_OBJECT;\n} )( ( { urlQueryArgs = EMPTY_OBJECT, currentPostId, ...props } ) => {\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\nexport default ExportedServerSideRender;\n"],"mappings":";;;;;;;AAGA,IAAAA,QAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AAKA,IAAAE,iBAAA,GAAAC,sBAAA,CAAAH,OAAA;AATA;AACA;AACA;;AAIA;AACA;AACA;;AAGA;AACA;AACA;AACA,MAAMI,YAAY,GAAG,CAAC,CAAC;AAEvB,MAAMC,wBAAwB,GAAG,IAAAC,gBAAU,EAAIC,MAAM,IAAM;EAC1D;EACA;EACA;EACA,MAAMC,gBAAgB,GAAGD,MAAM,CAAE,aAAc,CAAC;EAChD,IAAKC,gBAAgB,EAAG;IACvB,MAAMC,aAAa,GAAGD,gBAAgB,CAACE,gBAAgB,CAAC,CAAC;IACzD;IACA;IACA;IACA;IACA,IAAKD,aAAa,IAAI,OAAOA,aAAa,KAAK,QAAQ,EAAG;MACzD,OAAO;QACNA;MACD,CAAC;IACF;EACD;EACA,OAAOL,YAAY;AACpB,CAAE,CAAC,CAAE,CAAE;EAAEO,YAAY,GAAGP,YAAY;EAAEK,aAAa;EAAE,GAAGG;AAAM,CAAC,KAAM;EACpE,MAAMC,eAAe,GAAG,IAAAC,gBAAO,EAAE,MAAM;IACtC,IAAK,CAAEL,aAAa,EAAG;MACtB,OAAOE,YAAY;IACpB;IACA,OAAO;MACNI,OAAO,EAAEN,aAAa;MACtB,GAAGE;IACJ,CAAC;EACF,CAAC,EAAE,CAAEF,aAAa,EAAEE,YAAY,CAAG,CAAC;EAEpC,OAAO,IAAAZ,QAAA,CAAAiB,aAAA,EAACd,iBAAA,CAAAe,OAAgB;IAACN,YAAY,EAAGE,eAAiB;IAAA,GAAMD;EAAK,CAAI,CAAC;AAC1E,CAAE,CAAC;AAAC,IAAAM,QAAA,GAEWb,wBAAwB;AAAAc,OAAA,CAAAF,OAAA,GAAAC,QAAA"}
|
|
@@ -1,30 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = ServerSideRender;
|
|
9
8
|
exports.removeBlockSupportAttributes = removeBlockSupportAttributes;
|
|
10
9
|
exports.rendererPath = rendererPath;
|
|
11
|
-
|
|
12
10
|
var _element = require("@wordpress/element");
|
|
13
|
-
|
|
14
11
|
var _es = _interopRequireDefault(require("fast-deep-equal/es6"));
|
|
15
|
-
|
|
16
12
|
var _compose = require("@wordpress/compose");
|
|
17
|
-
|
|
18
13
|
var _i18n = require("@wordpress/i18n");
|
|
19
|
-
|
|
20
14
|
var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
|
|
21
|
-
|
|
22
15
|
var _url = require("@wordpress/url");
|
|
23
|
-
|
|
24
16
|
var _components = require("@wordpress/components");
|
|
25
|
-
|
|
26
17
|
var _blocks = require("@wordpress/blocks");
|
|
27
|
-
|
|
28
18
|
/**
|
|
29
19
|
* External dependencies
|
|
30
20
|
*/
|
|
@@ -32,8 +22,8 @@ var _blocks = require("@wordpress/blocks");
|
|
|
32
22
|
/**
|
|
33
23
|
* WordPress dependencies
|
|
34
24
|
*/
|
|
35
|
-
const EMPTY_OBJECT = {};
|
|
36
25
|
|
|
26
|
+
const EMPTY_OBJECT = {};
|
|
37
27
|
function rendererPath(block, attributes = null, urlQueryArgs = {}) {
|
|
38
28
|
return (0, _url.addQueryArgs)(`/wp/v2/block-renderer/${block}`, {
|
|
39
29
|
context: 'edit',
|
|
@@ -43,7 +33,6 @@ function rendererPath(block, attributes = null, urlQueryArgs = {}) {
|
|
|
43
33
|
...urlQueryArgs
|
|
44
34
|
});
|
|
45
35
|
}
|
|
46
|
-
|
|
47
36
|
function removeBlockSupportAttributes(attributes) {
|
|
48
37
|
const {
|
|
49
38
|
backgroundColor,
|
|
@@ -63,11 +52,11 @@ function removeBlockSupportAttributes(attributes) {
|
|
|
63
52
|
typography,
|
|
64
53
|
...restStyles
|
|
65
54
|
} = attributes?.style || EMPTY_OBJECT;
|
|
66
|
-
return {
|
|
55
|
+
return {
|
|
56
|
+
...restAttributes,
|
|
67
57
|
style: restStyles
|
|
68
58
|
};
|
|
69
59
|
}
|
|
70
|
-
|
|
71
60
|
function DefaultEmptyResponsePlaceholder({
|
|
72
61
|
className
|
|
73
62
|
}) {
|
|
@@ -75,18 +64,17 @@ function DefaultEmptyResponsePlaceholder({
|
|
|
75
64
|
className: className
|
|
76
65
|
}, (0, _i18n.__)('Block rendered as empty.'));
|
|
77
66
|
}
|
|
78
|
-
|
|
79
67
|
function DefaultErrorResponsePlaceholder({
|
|
80
68
|
response,
|
|
81
69
|
className
|
|
82
70
|
}) {
|
|
83
|
-
const errorMessage = (0, _i18n.sprintf)(
|
|
71
|
+
const errorMessage = (0, _i18n.sprintf)(
|
|
72
|
+
// translators: %s: error message describing the problem
|
|
84
73
|
(0, _i18n.__)('Error loading block: %s'), response.errorMsg);
|
|
85
74
|
return (0, _element.createElement)(_components.Placeholder, {
|
|
86
75
|
className: className
|
|
87
76
|
}, errorMessage);
|
|
88
77
|
}
|
|
89
|
-
|
|
90
78
|
function DefaultLoadingResponsePlaceholder({
|
|
91
79
|
children,
|
|
92
80
|
showLoader
|
|
@@ -109,7 +97,6 @@ function DefaultLoadingResponsePlaceholder({
|
|
|
109
97
|
}
|
|
110
98
|
}, children));
|
|
111
99
|
}
|
|
112
|
-
|
|
113
100
|
function ServerSideRender(props) {
|
|
114
101
|
const {
|
|
115
102
|
attributes,
|
|
@@ -128,31 +115,28 @@ function ServerSideRender(props) {
|
|
|
128
115
|
const [response, setResponse] = (0, _element.useState)(null);
|
|
129
116
|
const prevProps = (0, _compose.usePrevious)(props);
|
|
130
117
|
const [isLoading, setIsLoading] = (0, _element.useState)(false);
|
|
131
|
-
|
|
132
118
|
function fetchData() {
|
|
133
119
|
var _sanitizedAttributes, _sanitizedAttributes2;
|
|
134
|
-
|
|
135
120
|
if (!isMountedRef.current) {
|
|
136
121
|
return;
|
|
137
122
|
}
|
|
138
|
-
|
|
139
123
|
setIsLoading(true);
|
|
140
124
|
let sanitizedAttributes = attributes && (0, _blocks.__experimentalSanitizeBlockAttributes)(block, attributes);
|
|
141
|
-
|
|
142
125
|
if (skipBlockSupportAttributes) {
|
|
143
126
|
sanitizedAttributes = removeBlockSupportAttributes(sanitizedAttributes);
|
|
144
|
-
}
|
|
145
|
-
// This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
|
|
146
|
-
|
|
127
|
+
}
|
|
147
128
|
|
|
129
|
+
// If httpMethod is 'POST', send the attributes in the request body instead of the URL.
|
|
130
|
+
// This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
|
|
148
131
|
const isPostRequest = 'POST' === httpMethod;
|
|
149
132
|
const urlAttributes = isPostRequest ? null : (_sanitizedAttributes = sanitizedAttributes) !== null && _sanitizedAttributes !== void 0 ? _sanitizedAttributes : null;
|
|
150
133
|
const path = rendererPath(block, urlAttributes, urlQueryArgs);
|
|
151
134
|
const data = isPostRequest ? {
|
|
152
135
|
attributes: (_sanitizedAttributes2 = sanitizedAttributes) !== null && _sanitizedAttributes2 !== void 0 ? _sanitizedAttributes2 : null
|
|
153
|
-
} : null;
|
|
154
|
-
// check if it is the current request, to avoid race conditions on slow networks.
|
|
136
|
+
} : null;
|
|
155
137
|
|
|
138
|
+
// Store the latest fetch request so that when we process it, we can
|
|
139
|
+
// check if it is the current request, to avoid race conditions on slow networks.
|
|
156
140
|
const fetchRequest = fetchRequestRef.current = (0, _apiFetch.default)({
|
|
157
141
|
path,
|
|
158
142
|
data,
|
|
@@ -175,10 +159,10 @@ function ServerSideRender(props) {
|
|
|
175
159
|
});
|
|
176
160
|
return fetchRequest;
|
|
177
161
|
}
|
|
162
|
+
const debouncedFetchData = (0, _compose.useDebounce)(fetchData, 500);
|
|
178
163
|
|
|
179
|
-
|
|
164
|
+
// When the component unmounts, set isMountedRef to false. This will
|
|
180
165
|
// let the async fetch callbacks know when to stop.
|
|
181
|
-
|
|
182
166
|
(0, _element.useEffect)(() => () => {
|
|
183
167
|
isMountedRef.current = false;
|
|
184
168
|
}, []);
|
|
@@ -191,17 +175,16 @@ function ServerSideRender(props) {
|
|
|
191
175
|
debouncedFetchData();
|
|
192
176
|
}
|
|
193
177
|
});
|
|
178
|
+
|
|
194
179
|
/**
|
|
195
180
|
* Effect to handle showing the loading placeholder.
|
|
196
181
|
* Show it only if there is no previous response or
|
|
197
182
|
* the request takes more than one second.
|
|
198
183
|
*/
|
|
199
|
-
|
|
200
184
|
(0, _element.useEffect)(() => {
|
|
201
185
|
if (!isLoading) {
|
|
202
186
|
return;
|
|
203
187
|
}
|
|
204
|
-
|
|
205
188
|
const timeout = setTimeout(() => {
|
|
206
189
|
setShowLoader(true);
|
|
207
190
|
}, 1000);
|
|
@@ -210,27 +193,25 @@ function ServerSideRender(props) {
|
|
|
210
193
|
const hasResponse = !!response;
|
|
211
194
|
const hasEmptyResponse = response === '';
|
|
212
195
|
const hasError = response?.error;
|
|
213
|
-
|
|
214
196
|
if (isLoading) {
|
|
215
|
-
return (0, _element.createElement)(LoadingResponsePlaceholder, {
|
|
197
|
+
return (0, _element.createElement)(LoadingResponsePlaceholder, {
|
|
198
|
+
...props,
|
|
216
199
|
showLoader: showLoader
|
|
217
200
|
}, hasResponse && (0, _element.createElement)(_element.RawHTML, {
|
|
218
201
|
className: className
|
|
219
202
|
}, response));
|
|
220
203
|
}
|
|
221
|
-
|
|
222
204
|
if (hasEmptyResponse || !hasResponse) {
|
|
223
|
-
return (0, _element.createElement)(EmptyResponsePlaceholder, {
|
|
205
|
+
return (0, _element.createElement)(EmptyResponsePlaceholder, {
|
|
206
|
+
...props
|
|
224
207
|
});
|
|
225
208
|
}
|
|
226
|
-
|
|
227
209
|
if (hasError) {
|
|
228
210
|
return (0, _element.createElement)(ErrorResponsePlaceholder, {
|
|
229
211
|
response: response,
|
|
230
212
|
...props
|
|
231
213
|
});
|
|
232
214
|
}
|
|
233
|
-
|
|
234
215
|
return (0, _element.createElement)(_element.RawHTML, {
|
|
235
216
|
className: className
|
|
236
217
|
}, response);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/server-side-render/src/server-side-render.js"],"names":["EMPTY_OBJECT","rendererPath","block","attributes","urlQueryArgs","context","removeBlockSupportAttributes","backgroundColor","borderColor","fontFamily","fontSize","gradient","textColor","className","restAttributes","border","color","elements","spacing","typography","restStyles","style","DefaultEmptyResponsePlaceholder","DefaultErrorResponsePlaceholder","response","errorMessage","errorMsg","DefaultLoadingResponsePlaceholder","children","showLoader","position","top","left","marginTop","marginLeft","opacity","ServerSideRender","props","httpMethod","skipBlockSupportAttributes","EmptyResponsePlaceholder","ErrorResponsePlaceholder","LoadingResponsePlaceholder","isMountedRef","setShowLoader","fetchRequestRef","setResponse","prevProps","isLoading","setIsLoading","fetchData","current","sanitizedAttributes","isPostRequest","urlAttributes","path","data","fetchRequest","method","then","fetchResponse","rendered","catch","error","message","finally","debouncedFetchData","undefined","timeout","setTimeout","clearTimeout","hasResponse","hasEmptyResponse","hasError"],"mappings":";;;;;;;;;;;AASA;;AANA;;AAKA;;AAEA;;AACA;;AACA;;AACA;;AACA;;AAdA;AACA;AACA;;AAGA;AACA;AACA;AASA,MAAMA,YAAY,GAAG,EAArB;;AAEO,SAASC,YAAT,C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,wBAAD,OAA+BlC;AAA/B,MAAP;AACA;;AAED,MAAKoC,QAAL,EAAgB;AACf,WAAO,4BAAC,wBAAD;AAA0B,MAAA,QAAQ,EAAGjD,QAArC;AAAA,SAAqDa;AAArD,MAAP;AACA;;AAED,SAAO,4BAAC,gBAAD;AAAS,IAAA,SAAS,EAAGxB;AAArB,KAAmCW,QAAnC,CAAP;AACA","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 { RawHTML, useEffect, useRef, useState } 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, showLoader } ) {\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\tattributes,\n\t\tblock,\n\t\tclassName,\n\t\thttpMethod = 'GET',\n\t\turlQueryArgs,\n\t\tskipBlockSupportAttributes = false,\n\t\tEmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,\n\t\tErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,\n\t\tLoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder,\n\t} = props;\n\n\tconst isMountedRef = useRef( true );\n\tconst [ showLoader, setShowLoader ] = useState( false );\n\tconst fetchRequestRef = useRef();\n\tconst [ response, setResponse ] = useState( null );\n\tconst prevProps = usePrevious( props );\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\n\tfunction fetchData() {\n\t\tif ( ! isMountedRef.current ) {\n\t\t\treturn;\n\t\t}\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\t() => () => {\n\t\t\tisMountedRef.current = false;\n\t\t},\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\t/**\n\t * Effect to handle showing the loading placeholder.\n\t * Show it only if there is no previous response or\n\t * the request takes more than one second.\n\t */\n\tuseEffect( () => {\n\t\tif ( ! isLoading ) {\n\t\t\treturn;\n\t\t}\n\t\tconst timeout = setTimeout( () => {\n\t\t\tsetShowLoader( true );\n\t\t}, 1000 );\n\t\treturn () => clearTimeout( timeout );\n\t}, [ isLoading ] );\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 } showLoader={ showLoader }>\n\t\t\t\t{ hasResponse && (\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"]}
|
|
1
|
+
{"version":3,"names":["_element","require","_es","_interopRequireDefault","_compose","_i18n","_apiFetch","_url","_components","_blocks","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","createElement","Placeholder","__","DefaultErrorResponsePlaceholder","response","errorMessage","sprintf","errorMsg","DefaultLoadingResponsePlaceholder","children","showLoader","position","top","left","marginTop","marginLeft","Spinner","opacity","ServerSideRender","props","httpMethod","skipBlockSupportAttributes","EmptyResponsePlaceholder","ErrorResponsePlaceholder","LoadingResponsePlaceholder","isMountedRef","useRef","setShowLoader","useState","fetchRequestRef","setResponse","prevProps","usePrevious","isLoading","setIsLoading","fetchData","_sanitizedAttributes","_sanitizedAttributes2","current","sanitizedAttributes","__experimentalSanitizeBlockAttributes","isPostRequest","urlAttributes","path","data","fetchRequest","apiFetch","method","then","fetchResponse","rendered","catch","error","message","finally","debouncedFetchData","useDebounce","useEffect","undefined","fastDeepEqual","timeout","setTimeout","clearTimeout","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 { RawHTML, useEffect, useRef, useState } 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, showLoader } ) {\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\tattributes,\n\t\tblock,\n\t\tclassName,\n\t\thttpMethod = 'GET',\n\t\turlQueryArgs,\n\t\tskipBlockSupportAttributes = false,\n\t\tEmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,\n\t\tErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,\n\t\tLoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder,\n\t} = props;\n\n\tconst isMountedRef = useRef( true );\n\tconst [ showLoader, setShowLoader ] = useState( false );\n\tconst fetchRequestRef = useRef();\n\tconst [ response, setResponse ] = useState( null );\n\tconst prevProps = usePrevious( props );\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\n\tfunction fetchData() {\n\t\tif ( ! isMountedRef.current ) {\n\t\t\treturn;\n\t\t}\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\t() => () => {\n\t\t\tisMountedRef.current = false;\n\t\t},\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\t/**\n\t * Effect to handle showing the loading placeholder.\n\t * Show it only if there is no previous response or\n\t * the request takes more than one second.\n\t */\n\tuseEffect( () => {\n\t\tif ( ! isLoading ) {\n\t\t\treturn;\n\t\t}\n\t\tconst timeout = setTimeout( () => {\n\t\t\tsetShowLoader( true );\n\t\t}, 1000 );\n\t\treturn () => clearTimeout( timeout );\n\t}, [ isLoading ] );\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 } showLoader={ showLoader }>\n\t\t\t\t{ hasResponse && (\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":";;;;;;;;;AASA,IAAAA,QAAA,GAAAC,OAAA;AANA,IAAAC,GAAA,GAAA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,OAAO,MAAMmC,YAAY,CAAEF,OAAQ,CAAC;EACrC,CAAC,EAAE,CAAE3B,SAAS,CAAG,CAAC;EAElB,MAAM8B,WAAW,GAAG,CAAC,CAAE3D,QAAQ;EAC/B,MAAM4D,gBAAgB,GAAG5D,QAAQ,KAAK,EAAE;EACxC,MAAM6D,QAAQ,GAAG7D,QAAQ,EAAEgD,KAAK;EAEhC,IAAKnB,SAAS,EAAG;IAChB,OACC,IAAAnE,QAAA,CAAAkC,aAAA,EAACwB,0BAA0B;MAAA,GAAML,KAAK;MAAGT,UAAU,EAAGA;IAAY,GAC/DqD,WAAW,IACZ,IAAAjG,QAAA,CAAAkC,aAAA,EAAClC,QAAA,CAAAoG,OAAO;MAAC5E,SAAS,EAAGA;IAAW,GAAGc,QAAmB,CAE5B,CAAC;EAE/B;EAEA,IAAK4D,gBAAgB,IAAI,CAAED,WAAW,EAAG;IACxC,OAAO,IAAAjG,QAAA,CAAAkC,aAAA,EAACsB,wBAAwB;MAAA,GAAMH;IAAK,CAAI,CAAC;EACjD;EAEA,IAAK8C,QAAQ,EAAG;IACf,OAAO,IAAAnG,QAAA,CAAAkC,aAAA,EAACuB,wBAAwB;MAACnB,QAAQ,EAAGA,QAAU;MAAA,GAAMe;IAAK,CAAI,CAAC;EACvE;EAEA,OAAO,IAAArD,QAAA,CAAAkC,aAAA,EAAClC,QAAA,CAAAoG,OAAO;IAAC5E,SAAS,EAAGA;EAAW,GAAGc,QAAmB,CAAC;AAC/D"}
|
package/build-module/index.js
CHANGED
|
@@ -1,39 +1,36 @@
|
|
|
1
1
|
import { createElement } from "@wordpress/element";
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* WordPress dependencies
|
|
5
4
|
*/
|
|
6
5
|
import { useMemo } from '@wordpress/element';
|
|
7
6
|
import { withSelect } from '@wordpress/data';
|
|
7
|
+
|
|
8
8
|
/**
|
|
9
9
|
* Internal dependencies
|
|
10
10
|
*/
|
|
11
|
-
|
|
12
11
|
import ServerSideRender from './server-side-render';
|
|
12
|
+
|
|
13
13
|
/**
|
|
14
14
|
* Constants
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
16
|
const EMPTY_OBJECT = {};
|
|
18
17
|
const ExportedServerSideRender = withSelect(select => {
|
|
19
18
|
// FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.
|
|
20
19
|
// It is used by blocks that can be loaded into a *non-post* block editor.
|
|
21
20
|
// eslint-disable-next-line @wordpress/data-no-store-string-literals
|
|
22
21
|
const coreEditorSelect = select('core/editor');
|
|
23
|
-
|
|
24
22
|
if (coreEditorSelect) {
|
|
25
|
-
const currentPostId = coreEditorSelect.getCurrentPostId();
|
|
23
|
+
const currentPostId = coreEditorSelect.getCurrentPostId();
|
|
24
|
+
// For templates and template parts we use a custom ID format.
|
|
26
25
|
// Since they aren't real posts, we don't want to use their ID
|
|
27
26
|
// for server-side rendering. Since they use a string based ID,
|
|
28
27
|
// we can assume real post IDs are numbers.
|
|
29
|
-
|
|
30
28
|
if (currentPostId && typeof currentPostId === 'number') {
|
|
31
29
|
return {
|
|
32
30
|
currentPostId
|
|
33
31
|
};
|
|
34
32
|
}
|
|
35
33
|
}
|
|
36
|
-
|
|
37
34
|
return EMPTY_OBJECT;
|
|
38
35
|
})(({
|
|
39
36
|
urlQueryArgs = EMPTY_OBJECT,
|
|
@@ -44,7 +41,6 @@ const ExportedServerSideRender = withSelect(select => {
|
|
|
44
41
|
if (!currentPostId) {
|
|
45
42
|
return urlQueryArgs;
|
|
46
43
|
}
|
|
47
|
-
|
|
48
44
|
return {
|
|
49
45
|
post_id: currentPostId,
|
|
50
46
|
...urlQueryArgs
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["useMemo","withSelect","ServerSideRender","EMPTY_OBJECT","ExportedServerSideRender","select","coreEditorSelect","currentPostId","getCurrentPostId","urlQueryArgs","props","newUrlQueryArgs","post_id","createElement"],"sources":["@wordpress/server-side-render/src/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useMemo } from '@wordpress/element';\nimport { withSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport ServerSideRender from './server-side-render';\n\n/**\n * Constants\n */\nconst EMPTY_OBJECT = {};\n\nconst ExportedServerSideRender = withSelect( ( select ) => {\n\t// FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.\n\t// It is used by blocks that can be loaded into a *non-post* block editor.\n\t// eslint-disable-next-line @wordpress/data-no-store-string-literals\n\tconst coreEditorSelect = select( 'core/editor' );\n\tif ( coreEditorSelect ) {\n\t\tconst currentPostId = coreEditorSelect.getCurrentPostId();\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\tif ( currentPostId && typeof currentPostId === 'number' ) {\n\t\t\treturn {\n\t\t\t\tcurrentPostId,\n\t\t\t};\n\t\t}\n\t}\n\treturn EMPTY_OBJECT;\n} )( ( { urlQueryArgs = EMPTY_OBJECT, currentPostId, ...props } ) => {\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\nexport default ExportedServerSideRender;\n"],"mappings":";AAAA;AACA;AACA;AACA,SAASA,OAAO,QAAQ,oBAAoB;AAC5C,SAASC,UAAU,QAAQ,iBAAiB;;AAE5C;AACA;AACA;AACA,OAAOC,gBAAgB,MAAM,sBAAsB;;AAEnD;AACA;AACA;AACA,MAAMC,YAAY,GAAG,CAAC,CAAC;AAEvB,MAAMC,wBAAwB,GAAGH,UAAU,CAAII,MAAM,IAAM;EAC1D;EACA;EACA;EACA,MAAMC,gBAAgB,GAAGD,MAAM,CAAE,aAAc,CAAC;EAChD,IAAKC,gBAAgB,EAAG;IACvB,MAAMC,aAAa,GAAGD,gBAAgB,CAACE,gBAAgB,CAAC,CAAC;IACzD;IACA;IACA;IACA;IACA,IAAKD,aAAa,IAAI,OAAOA,aAAa,KAAK,QAAQ,EAAG;MACzD,OAAO;QACNA;MACD,CAAC;IACF;EACD;EACA,OAAOJ,YAAY;AACpB,CAAE,CAAC,CAAE,CAAE;EAAEM,YAAY,GAAGN,YAAY;EAAEI,aAAa;EAAE,GAAGG;AAAM,CAAC,KAAM;EACpE,MAAMC,eAAe,GAAGX,OAAO,CAAE,MAAM;IACtC,IAAK,CAAEO,aAAa,EAAG;MACtB,OAAOE,YAAY;IACpB;IACA,OAAO;MACNG,OAAO,EAAEL,aAAa;MACtB,GAAGE;IACJ,CAAC;EACF,CAAC,EAAE,CAAEF,aAAa,EAAEE,YAAY,CAAG,CAAC;EAEpC,OAAOI,aAAA,CAACX,gBAAgB;IAACO,YAAY,EAAGE,eAAiB;IAAA,GAAMD;EAAK,CAAI,CAAC;AAC1E,CAAE,CAAC;AAEH,eAAeN,wBAAwB"}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { createElement } from "@wordpress/element";
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* External dependencies
|
|
5
4
|
*/
|
|
6
5
|
import fastDeepEqual from 'fast-deep-equal/es6';
|
|
6
|
+
|
|
7
7
|
/**
|
|
8
8
|
* WordPress dependencies
|
|
9
9
|
*/
|
|
10
|
-
|
|
11
10
|
import { useDebounce, usePrevious } from '@wordpress/compose';
|
|
12
11
|
import { RawHTML, useEffect, useRef, useState } from '@wordpress/element';
|
|
13
12
|
import { __, sprintf } from '@wordpress/i18n';
|
|
@@ -44,11 +43,11 @@ export function removeBlockSupportAttributes(attributes) {
|
|
|
44
43
|
typography,
|
|
45
44
|
...restStyles
|
|
46
45
|
} = attributes?.style || EMPTY_OBJECT;
|
|
47
|
-
return {
|
|
46
|
+
return {
|
|
47
|
+
...restAttributes,
|
|
48
48
|
style: restStyles
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
|
-
|
|
52
51
|
function DefaultEmptyResponsePlaceholder({
|
|
53
52
|
className
|
|
54
53
|
}) {
|
|
@@ -56,18 +55,17 @@ function DefaultEmptyResponsePlaceholder({
|
|
|
56
55
|
className: className
|
|
57
56
|
}, __('Block rendered as empty.'));
|
|
58
57
|
}
|
|
59
|
-
|
|
60
58
|
function DefaultErrorResponsePlaceholder({
|
|
61
59
|
response,
|
|
62
60
|
className
|
|
63
61
|
}) {
|
|
64
|
-
const errorMessage = sprintf(
|
|
62
|
+
const errorMessage = sprintf(
|
|
63
|
+
// translators: %s: error message describing the problem
|
|
65
64
|
__('Error loading block: %s'), response.errorMsg);
|
|
66
65
|
return createElement(Placeholder, {
|
|
67
66
|
className: className
|
|
68
67
|
}, errorMessage);
|
|
69
68
|
}
|
|
70
|
-
|
|
71
69
|
function DefaultLoadingResponsePlaceholder({
|
|
72
70
|
children,
|
|
73
71
|
showLoader
|
|
@@ -90,7 +88,6 @@ function DefaultLoadingResponsePlaceholder({
|
|
|
90
88
|
}
|
|
91
89
|
}, children));
|
|
92
90
|
}
|
|
93
|
-
|
|
94
91
|
export default function ServerSideRender(props) {
|
|
95
92
|
const {
|
|
96
93
|
attributes,
|
|
@@ -109,32 +106,28 @@ export default function ServerSideRender(props) {
|
|
|
109
106
|
const [response, setResponse] = useState(null);
|
|
110
107
|
const prevProps = usePrevious(props);
|
|
111
108
|
const [isLoading, setIsLoading] = useState(false);
|
|
112
|
-
|
|
113
109
|
function fetchData() {
|
|
114
110
|
var _sanitizedAttributes, _sanitizedAttributes2;
|
|
115
|
-
|
|
116
111
|
if (!isMountedRef.current) {
|
|
117
112
|
return;
|
|
118
113
|
}
|
|
119
|
-
|
|
120
114
|
setIsLoading(true);
|
|
121
|
-
|
|
122
115
|
let sanitizedAttributes = attributes && __experimentalSanitizeBlockAttributes(block, attributes);
|
|
123
|
-
|
|
124
116
|
if (skipBlockSupportAttributes) {
|
|
125
117
|
sanitizedAttributes = removeBlockSupportAttributes(sanitizedAttributes);
|
|
126
|
-
}
|
|
127
|
-
// This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
|
|
128
|
-
|
|
118
|
+
}
|
|
129
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.
|
|
130
122
|
const isPostRequest = 'POST' === httpMethod;
|
|
131
123
|
const urlAttributes = isPostRequest ? null : (_sanitizedAttributes = sanitizedAttributes) !== null && _sanitizedAttributes !== void 0 ? _sanitizedAttributes : null;
|
|
132
124
|
const path = rendererPath(block, urlAttributes, urlQueryArgs);
|
|
133
125
|
const data = isPostRequest ? {
|
|
134
126
|
attributes: (_sanitizedAttributes2 = sanitizedAttributes) !== null && _sanitizedAttributes2 !== void 0 ? _sanitizedAttributes2 : null
|
|
135
|
-
} : null;
|
|
136
|
-
// check if it is the current request, to avoid race conditions on slow networks.
|
|
127
|
+
} : null;
|
|
137
128
|
|
|
129
|
+
// Store the latest fetch request so that when we process it, we can
|
|
130
|
+
// check if it is the current request, to avoid race conditions on slow networks.
|
|
138
131
|
const fetchRequest = fetchRequestRef.current = apiFetch({
|
|
139
132
|
path,
|
|
140
133
|
data,
|
|
@@ -157,10 +150,10 @@ export default function ServerSideRender(props) {
|
|
|
157
150
|
});
|
|
158
151
|
return fetchRequest;
|
|
159
152
|
}
|
|
153
|
+
const debouncedFetchData = useDebounce(fetchData, 500);
|
|
160
154
|
|
|
161
|
-
|
|
155
|
+
// When the component unmounts, set isMountedRef to false. This will
|
|
162
156
|
// let the async fetch callbacks know when to stop.
|
|
163
|
-
|
|
164
157
|
useEffect(() => () => {
|
|
165
158
|
isMountedRef.current = false;
|
|
166
159
|
}, []);
|
|
@@ -173,17 +166,16 @@ export default function ServerSideRender(props) {
|
|
|
173
166
|
debouncedFetchData();
|
|
174
167
|
}
|
|
175
168
|
});
|
|
169
|
+
|
|
176
170
|
/**
|
|
177
171
|
* Effect to handle showing the loading placeholder.
|
|
178
172
|
* Show it only if there is no previous response or
|
|
179
173
|
* the request takes more than one second.
|
|
180
174
|
*/
|
|
181
|
-
|
|
182
175
|
useEffect(() => {
|
|
183
176
|
if (!isLoading) {
|
|
184
177
|
return;
|
|
185
178
|
}
|
|
186
|
-
|
|
187
179
|
const timeout = setTimeout(() => {
|
|
188
180
|
setShowLoader(true);
|
|
189
181
|
}, 1000);
|
|
@@ -192,27 +184,25 @@ export default function ServerSideRender(props) {
|
|
|
192
184
|
const hasResponse = !!response;
|
|
193
185
|
const hasEmptyResponse = response === '';
|
|
194
186
|
const hasError = response?.error;
|
|
195
|
-
|
|
196
187
|
if (isLoading) {
|
|
197
|
-
return createElement(LoadingResponsePlaceholder, {
|
|
188
|
+
return createElement(LoadingResponsePlaceholder, {
|
|
189
|
+
...props,
|
|
198
190
|
showLoader: showLoader
|
|
199
191
|
}, hasResponse && createElement(RawHTML, {
|
|
200
192
|
className: className
|
|
201
193
|
}, response));
|
|
202
194
|
}
|
|
203
|
-
|
|
204
195
|
if (hasEmptyResponse || !hasResponse) {
|
|
205
|
-
return createElement(EmptyResponsePlaceholder, {
|
|
196
|
+
return createElement(EmptyResponsePlaceholder, {
|
|
197
|
+
...props
|
|
206
198
|
});
|
|
207
199
|
}
|
|
208
|
-
|
|
209
200
|
if (hasError) {
|
|
210
201
|
return createElement(ErrorResponsePlaceholder, {
|
|
211
202
|
response: response,
|
|
212
203
|
...props
|
|
213
204
|
});
|
|
214
205
|
}
|
|
215
|
-
|
|
216
206
|
return createElement(RawHTML, {
|
|
217
207
|
className: className
|
|
218
208
|
}, response);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/server-side-render/src/server-side-render.js"],"names":["fastDeepEqual","useDebounce","usePrevious","RawHTML","useEffect","useRef","useState","__","sprintf","apiFetch","addQueryArgs","Placeholder","Spinner","__experimentalSanitizeBlockAttributes","EMPTY_OBJECT","rendererPath","block","attributes","urlQueryArgs","context","removeBlockSupportAttributes","backgroundColor","borderColor","fontFamily","fontSize","gradient","textColor","className","restAttributes","border","color","elements","spacing","typography","restStyles","style","DefaultEmptyResponsePlaceholder","DefaultErrorResponsePlaceholder","response","errorMessage","errorMsg","DefaultLoadingResponsePlaceholder","children","showLoader","position","top","left","marginTop","marginLeft","opacity","ServerSideRender","props","httpMethod","skipBlockSupportAttributes","EmptyResponsePlaceholder","ErrorResponsePlaceholder","LoadingResponsePlaceholder","isMountedRef","setShowLoader","fetchRequestRef","setResponse","prevProps","isLoading","setIsLoading","fetchData","current","sanitizedAttributes","isPostRequest","urlAttributes","path","data","fetchRequest","method","then","fetchResponse","rendered","catch","error","message","finally","debouncedFetchData","undefined","timeout","setTimeout","clearTimeout","hasResponse","hasEmptyResponse","hasError"],"mappings":";;AAAA;AACA;AACA;AACA,OAAOA,aAAP,MAA0B,qBAA1B;AAEA;AACA;AACA;;AACA,SAASC,WAAT,EAAsBC,WAAtB,QAAyC,oBAAzC;AACA,SAASC,OAAT,EAAkBC,SAAlB,EAA6BC,MAA7B,EAAqCC,QAArC,QAAqD,oBAArD;AACA,SAASC,EAAT,EAAaC,OAAb,QAA4B,iBAA5B;AACA,OAAOC,QAAP,MAAqB,sBAArB;AACA,SAASC,YAAT,QAA6B,gBAA7B;AACA,SAASC,WAAT,EAAsBC,OAAtB,QAAqC,uBAArC;AACA,SAASC,qCAAT,QAAsD,mBAAtD;AAEA,MAAMC,YAAY,GAAG,EAArB;AAEA,OAAO,SAASC,YAAT,CAAuBC,KAAvB,EAA8BC,UAAU,GAAG,IAA3C,EAAiDC,YAAY,GAAG,EAAhE,EAAqE;AAC3E,SAAOR,YAAY,CAAG,yBAAyBM,KAAO,EAAnC,EAAsC;AACxDG,IAAAA,OAAO,EAAE,MAD+C;AAExD,QAAK,SAASF,UAAT,GAAsB;AAAEA,MAAAA;AAAF,KAAtB,GAAuC,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AED,MAAKoC,QAAL,EAAgB;AACf,WAAO,cAAC,wBAAD;AAA0B,MAAA,QAAQ,EAAGjD,QAArC;AAAA,SAAqDa;AAArD,MAAP;AACA;;AAED,SAAO,cAAC,OAAD;AAAS,IAAA,SAAS,EAAGxB;AAArB,KAAmCW,QAAnC,CAAP;AACA","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 { RawHTML, useEffect, useRef, useState } 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, showLoader } ) {\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\tattributes,\n\t\tblock,\n\t\tclassName,\n\t\thttpMethod = 'GET',\n\t\turlQueryArgs,\n\t\tskipBlockSupportAttributes = false,\n\t\tEmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,\n\t\tErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,\n\t\tLoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder,\n\t} = props;\n\n\tconst isMountedRef = useRef( true );\n\tconst [ showLoader, setShowLoader ] = useState( false );\n\tconst fetchRequestRef = useRef();\n\tconst [ response, setResponse ] = useState( null );\n\tconst prevProps = usePrevious( props );\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\n\tfunction fetchData() {\n\t\tif ( ! isMountedRef.current ) {\n\t\t\treturn;\n\t\t}\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\t() => () => {\n\t\t\tisMountedRef.current = false;\n\t\t},\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\t/**\n\t * Effect to handle showing the loading placeholder.\n\t * Show it only if there is no previous response or\n\t * the request takes more than one second.\n\t */\n\tuseEffect( () => {\n\t\tif ( ! isLoading ) {\n\t\t\treturn;\n\t\t}\n\t\tconst timeout = setTimeout( () => {\n\t\t\tsetShowLoader( true );\n\t\t}, 1000 );\n\t\treturn () => clearTimeout( timeout );\n\t}, [ isLoading ] );\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 } showLoader={ showLoader }>\n\t\t\t\t{ hasResponse && (\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"]}
|
|
1
|
+
{"version":3,"names":["fastDeepEqual","useDebounce","usePrevious","RawHTML","useEffect","useRef","useState","__","sprintf","apiFetch","addQueryArgs","Placeholder","Spinner","__experimentalSanitizeBlockAttributes","EMPTY_OBJECT","rendererPath","block","attributes","urlQueryArgs","context","removeBlockSupportAttributes","backgroundColor","borderColor","fontFamily","fontSize","gradient","textColor","className","restAttributes","border","color","elements","spacing","typography","restStyles","style","DefaultEmptyResponsePlaceholder","createElement","DefaultErrorResponsePlaceholder","response","errorMessage","errorMsg","DefaultLoadingResponsePlaceholder","children","showLoader","position","top","left","marginTop","marginLeft","opacity","ServerSideRender","props","httpMethod","skipBlockSupportAttributes","EmptyResponsePlaceholder","ErrorResponsePlaceholder","LoadingResponsePlaceholder","isMountedRef","setShowLoader","fetchRequestRef","setResponse","prevProps","isLoading","setIsLoading","fetchData","_sanitizedAttributes","_sanitizedAttributes2","current","sanitizedAttributes","isPostRequest","urlAttributes","path","data","fetchRequest","method","then","fetchResponse","rendered","catch","error","message","finally","debouncedFetchData","undefined","timeout","setTimeout","clearTimeout","hasResponse","hasEmptyResponse","hasError"],"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 { RawHTML, useEffect, useRef, useState } 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, showLoader } ) {\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\tattributes,\n\t\tblock,\n\t\tclassName,\n\t\thttpMethod = 'GET',\n\t\turlQueryArgs,\n\t\tskipBlockSupportAttributes = false,\n\t\tEmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,\n\t\tErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,\n\t\tLoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder,\n\t} = props;\n\n\tconst isMountedRef = useRef( true );\n\tconst [ showLoader, setShowLoader ] = useState( false );\n\tconst fetchRequestRef = useRef();\n\tconst [ response, setResponse ] = useState( null );\n\tconst prevProps = usePrevious( props );\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\n\tfunction fetchData() {\n\t\tif ( ! isMountedRef.current ) {\n\t\t\treturn;\n\t\t}\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\t() => () => {\n\t\t\tisMountedRef.current = false;\n\t\t},\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\t/**\n\t * Effect to handle showing the loading placeholder.\n\t * Show it only if there is no previous response or\n\t * the request takes more than one second.\n\t */\n\tuseEffect( () => {\n\t\tif ( ! isLoading ) {\n\t\t\treturn;\n\t\t}\n\t\tconst timeout = setTimeout( () => {\n\t\t\tsetShowLoader( true );\n\t\t}, 1000 );\n\t\treturn () => clearTimeout( timeout );\n\t}, [ isLoading ] );\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 } showLoader={ showLoader }>\n\t\t\t\t{ hasResponse && (\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":";AAAA;AACA;AACA;AACA,OAAOA,aAAa,MAAM,qBAAqB;;AAE/C;AACA;AACA;AACA,SAASC,WAAW,EAAEC,WAAW,QAAQ,oBAAoB;AAC7D,SAASC,OAAO,EAAEC,SAAS,EAAEC,MAAM,EA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B,aAAA,CAACoB,0BAA0B;MAAA,GAAML,KAAK;MAAGR,UAAU,EAAGA;IAAY,GAC/D4C,WAAW,IACZnD,aAAA,CAAClC,OAAO;MAACwB,SAAS,EAAGA;IAAW,GAAGY,QAAmB,CAE5B,CAAC;EAE/B;EAEA,IAAKkD,gBAAgB,IAAI,CAAED,WAAW,EAAG;IACxC,OAAOnD,aAAA,CAACkB,wBAAwB;MAAA,GAAMH;IAAK,CAAI,CAAC;EACjD;EAEA,IAAKsC,QAAQ,EAAG;IACf,OAAOrD,aAAA,CAACmB,wBAAwB;MAACjB,QAAQ,EAAGA,QAAU;MAAA,GAAMa;IAAK,CAAI,CAAC;EACvE;EAEA,OAAOf,aAAA,CAAClC,OAAO;IAACwB,SAAS,EAAGA;EAAW,GAAGY,QAAmB,CAAC;AAC/D"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/server-side-render",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.17.1",
|
|
4
4
|
"description": "The component used with WordPress to server-side render a preview of dynamic blocks to display in the editor.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -27,15 +27,15 @@
|
|
|
27
27
|
"react-native": "src/index",
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@babel/runtime": "^7.16.0",
|
|
30
|
-
"@wordpress/api-fetch": "^6.
|
|
31
|
-
"@wordpress/blocks": "^12.
|
|
32
|
-
"@wordpress/components": "^25.
|
|
33
|
-
"@wordpress/compose": "^6.
|
|
34
|
-
"@wordpress/data": "^9.
|
|
35
|
-
"@wordpress/deprecated": "^3.
|
|
36
|
-
"@wordpress/element": "^5.
|
|
37
|
-
"@wordpress/i18n": "^4.
|
|
38
|
-
"@wordpress/url": "^3.
|
|
30
|
+
"@wordpress/api-fetch": "^6.37.1",
|
|
31
|
+
"@wordpress/blocks": "^12.17.1",
|
|
32
|
+
"@wordpress/components": "^25.6.1",
|
|
33
|
+
"@wordpress/compose": "^6.17.1",
|
|
34
|
+
"@wordpress/data": "^9.10.1",
|
|
35
|
+
"@wordpress/deprecated": "^3.40.1",
|
|
36
|
+
"@wordpress/element": "^5.17.1",
|
|
37
|
+
"@wordpress/i18n": "^4.40.1",
|
|
38
|
+
"@wordpress/url": "^3.41.1",
|
|
39
39
|
"fast-deep-equal": "^3.1.3"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"publishConfig": {
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "bb1fbf87bb0f451744530fc6a85de2dff1263bed"
|
|
49
49
|
}
|