server-side-render.js
12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/******/ (function() { // webpackBootstrap
/******/ "use strict";
/******/ // The require scope
/******/ var __webpack_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ !function() {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function() { return module['default']; } :
/******/ function() { return module; };
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ }();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ !function() {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = function(exports, definition) {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ }();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ !function() {
/******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
/******/ }();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// EXPORTS
__webpack_require__.d(__webpack_exports__, {
"default": function() { return /* binding */ build_module; }
});
;// CONCATENATED MODULE: ./node_modules/@babel/runtime/helpers/esm/extends.js
function _extends() {
_extends = Object.assign ? Object.assign.bind() : function (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
return _extends.apply(this, arguments);
}
;// CONCATENATED MODULE: external ["wp","element"]
var external_wp_element_namespaceObject = window["wp"]["element"];
;// CONCATENATED MODULE: external ["wp","data"]
var external_wp_data_namespaceObject = window["wp"]["data"];
;// CONCATENATED MODULE: external ["wp","deprecated"]
var external_wp_deprecated_namespaceObject = window["wp"]["deprecated"];
var external_wp_deprecated_default = /*#__PURE__*/__webpack_require__.n(external_wp_deprecated_namespaceObject);
;// CONCATENATED MODULE: external "lodash"
var external_lodash_namespaceObject = window["lodash"];
;// CONCATENATED MODULE: external ["wp","compose"]
var external_wp_compose_namespaceObject = window["wp"]["compose"];
;// CONCATENATED MODULE: external ["wp","i18n"]
var external_wp_i18n_namespaceObject = window["wp"]["i18n"];
;// CONCATENATED MODULE: external ["wp","apiFetch"]
var external_wp_apiFetch_namespaceObject = window["wp"]["apiFetch"];
var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_namespaceObject);
;// CONCATENATED MODULE: external ["wp","url"]
var external_wp_url_namespaceObject = window["wp"]["url"];
;// CONCATENATED MODULE: external ["wp","components"]
var external_wp_components_namespaceObject = window["wp"]["components"];
;// CONCATENATED MODULE: external ["wp","blocks"]
var external_wp_blocks_namespaceObject = window["wp"]["blocks"];
;// CONCATENATED MODULE: ./node_modules/@wordpress/server-side-render/build-module/server-side-render.js
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
function rendererPath(block) {
let attributes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
let urlQueryArgs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return (0,external_wp_url_namespaceObject.addQueryArgs)(`/wp/v2/block-renderer/${block}`, {
context: 'edit',
...(null !== attributes ? {
attributes
} : {}),
...urlQueryArgs
});
}
function DefaultEmptyResponsePlaceholder(_ref) {
let {
className
} = _ref;
return (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.Placeholder, {
className: className
}, (0,external_wp_i18n_namespaceObject.__)('Block rendered as empty.'));
}
function DefaultErrorResponsePlaceholder(_ref2) {
let {
response,
className
} = _ref2;
const errorMessage = (0,external_wp_i18n_namespaceObject.sprintf)( // translators: %s: error message describing the problem
(0,external_wp_i18n_namespaceObject.__)('Error loading block: %s'), response.errorMsg);
return (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.Placeholder, {
className: className
}, errorMessage);
}
function DefaultLoadingResponsePlaceholder(_ref3) {
let {
children,
showLoader
} = _ref3;
return (0,external_wp_element_namespaceObject.createElement)("div", {
style: {
position: 'relative'
}
}, showLoader && (0,external_wp_element_namespaceObject.createElement)("div", {
style: {
position: 'absolute',
top: '50%',
left: '50%',
marginTop: '-9px',
marginLeft: '-9px'
}
}, (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.Spinner, null)), (0,external_wp_element_namespaceObject.createElement)("div", {
style: {
opacity: showLoader ? '0.3' : 1
}
}, children));
}
function ServerSideRender(props) {
const {
attributes,
block,
className,
httpMethod = 'GET',
urlQueryArgs,
EmptyResponsePlaceholder = DefaultEmptyResponsePlaceholder,
ErrorResponsePlaceholder = DefaultErrorResponsePlaceholder,
LoadingResponsePlaceholder = DefaultLoadingResponsePlaceholder
} = props;
const isMountedRef = (0,external_wp_element_namespaceObject.useRef)(true);
const [showLoader, setShowLoader] = (0,external_wp_element_namespaceObject.useState)(false);
const fetchRequestRef = (0,external_wp_element_namespaceObject.useRef)();
const [response, setResponse] = (0,external_wp_element_namespaceObject.useState)(null);
const prevProps = (0,external_wp_compose_namespaceObject.usePrevious)(props);
const [isLoading, setIsLoading] = (0,external_wp_element_namespaceObject.useState)(false);
function fetchData() {
if (!isMountedRef.current) {
return;
}
setIsLoading(true);
const sanitizedAttributes = attributes && (0,external_wp_blocks_namespaceObject.__experimentalSanitizeBlockAttributes)(block, attributes); // If httpMethod is 'POST', send the attributes in the request body instead of the URL.
// This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
const isPostRequest = 'POST' === httpMethod;
const urlAttributes = isPostRequest ? null : sanitizedAttributes !== null && sanitizedAttributes !== void 0 ? sanitizedAttributes : null;
const path = rendererPath(block, urlAttributes, urlQueryArgs);
const data = isPostRequest ? {
attributes: sanitizedAttributes !== null && sanitizedAttributes !== void 0 ? sanitizedAttributes : null
} : null; // Store the latest fetch request so that when we process it, we can
// check if it is the current request, to avoid race conditions on slow networks.
const fetchRequest = fetchRequestRef.current = external_wp_apiFetch_default()({
path,
data,
method: isPostRequest ? 'POST' : 'GET'
}).then(fetchResponse => {
if (isMountedRef.current && fetchRequest === fetchRequestRef.current && fetchResponse) {
setResponse(fetchResponse.rendered);
}
}).catch(error => {
if (isMountedRef.current && fetchRequest === fetchRequestRef.current) {
setResponse({
error: true,
errorMsg: error.message
});
}
}).finally(() => {
if (isMountedRef.current && fetchRequest === fetchRequestRef.current) {
setIsLoading(false);
}
});
return fetchRequest;
}
const debouncedFetchData = (0,external_wp_compose_namespaceObject.useDebounce)(fetchData, 500); // When the component unmounts, set isMountedRef to false. This will
// let the async fetch callbacks know when to stop.
(0,external_wp_element_namespaceObject.useEffect)(() => () => {
isMountedRef.current = false;
}, []);
(0,external_wp_element_namespaceObject.useEffect)(() => {
// Don't debounce the first fetch. This ensures that the first render
// shows data as soon as possible.
if (prevProps === undefined) {
fetchData();
} else if (!(0,external_lodash_namespaceObject.isEqual)(prevProps, props)) {
debouncedFetchData();
}
});
/**
* Effect to handle showing the loading placeholder.
* Show it only if there is no previous response or
* the request takes more than one second.
*/
(0,external_wp_element_namespaceObject.useEffect)(() => {
if (!isLoading) {
return;
}
const timeout = setTimeout(() => {
setShowLoader(true);
}, 1000);
return () => clearTimeout(timeout);
}, [isLoading]);
const hasResponse = !!response;
const hasEmptyResponse = response === '';
const hasError = response === null || response === void 0 ? void 0 : response.error;
if (isLoading) {
return (0,external_wp_element_namespaceObject.createElement)(LoadingResponsePlaceholder, _extends({}, props, {
showLoader: showLoader
}), hasResponse && (0,external_wp_element_namespaceObject.createElement)(external_wp_element_namespaceObject.RawHTML, {
className: className
}, response));
}
if (hasEmptyResponse || !hasResponse) {
return (0,external_wp_element_namespaceObject.createElement)(EmptyResponsePlaceholder, props);
}
if (hasError) {
return (0,external_wp_element_namespaceObject.createElement)(ErrorResponsePlaceholder, _extends({
response: response
}, props));
}
return (0,external_wp_element_namespaceObject.createElement)(external_wp_element_namespaceObject.RawHTML, {
className: className
}, response);
}
;// CONCATENATED MODULE: ./node_modules/@wordpress/server-side-render/build-module/index.js
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
/**
* Constants
*/
const EMPTY_OBJECT = {};
const ExportedServerSideRender = (0,external_wp_data_namespaceObject.withSelect)(select => {
// FIXME: @wordpress/server-side-render should not depend on @wordpress/editor.
// It is used by blocks that can be loaded into a *non-post* block editor.
// eslint-disable-next-line @wordpress/data-no-store-string-literals
const coreEditorSelect = select('core/editor');
if (coreEditorSelect) {
const currentPostId = coreEditorSelect.getCurrentPostId(); // For templates and template parts we use a custom ID format.
// Since they aren't real posts, we don't want to use their ID
// for server-side rendering. Since they use a string based ID,
// we can assume real post IDs are numbers.
if (currentPostId && typeof currentPostId === 'number') {
return {
currentPostId
};
}
}
return EMPTY_OBJECT;
})(_ref => {
let {
urlQueryArgs = EMPTY_OBJECT,
currentPostId,
...props
} = _ref;
const newUrlQueryArgs = (0,external_wp_element_namespaceObject.useMemo)(() => {
if (!currentPostId) {
return urlQueryArgs;
}
return {
post_id: currentPostId,
...urlQueryArgs
};
}, [currentPostId, urlQueryArgs]);
return (0,external_wp_element_namespaceObject.createElement)(ServerSideRender, _extends({
urlQueryArgs: newUrlQueryArgs
}, props));
});
if (window && window.wp && window.wp.components) {
window.wp.components.ServerSideRender = (0,external_wp_element_namespaceObject.forwardRef)((props, ref) => {
external_wp_deprecated_default()('wp.components.ServerSideRender', {
version: '6.2',
since: '5.3',
alternative: 'wp.serverSideRender'
});
return (0,external_wp_element_namespaceObject.createElement)(ExportedServerSideRender, _extends({}, props, {
ref: ref
}));
});
}
/* harmony default export */ var build_module = (ExportedServerSideRender);
(window.wp = window.wp || {}).serverSideRender = __webpack_exports__["default"];
/******/ })()
;