-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
server-side-render.js
167 lines (153 loc) · 3.89 KB
/
server-side-render.js
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
/**
* External dependencies
*/
import { isEqual, debounce } from 'lodash';
/**
* WordPress dependencies
*/
import { Component, RawHTML } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
import { Placeholder, Spinner } from '@wordpress/components';
export function rendererPath( block, attributes = null, urlQueryArgs = {} ) {
return addQueryArgs( `/wp/v2/block-renderer/${ block }`, {
context: 'edit',
...( null !== attributes ? { attributes } : {} ),
...urlQueryArgs,
} );
}
export class ServerSideRender extends Component {
constructor( props ) {
super( props );
this.state = {
response: null,
};
}
componentDidMount() {
this.isStillMounted = true;
this.fetch( this.props );
// Only debounce once the initial fetch occurs to ensure that the first
// renders show data as soon as possible.
this.fetch = debounce( this.fetch, 500 );
}
componentWillUnmount() {
this.isStillMounted = false;
}
componentDidUpdate( prevProps ) {
if ( ! isEqual( prevProps, this.props ) ) {
this.fetch( this.props );
}
}
fetch( props ) {
if ( ! this.isStillMounted ) {
return;
}
if ( null !== this.state.response ) {
this.setState( { response: null } );
}
const {
block,
attributes = null,
httpMethod = 'GET',
urlQueryArgs = {},
} = props;
// 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 : attributes;
const path = rendererPath( block, urlAttributes, urlQueryArgs );
const data = isPostRequest ? { attributes } : 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 = ( this.currentFetchRequest = apiFetch( {
path,
data,
method: isPostRequest ? 'POST' : 'GET',
} )
.then( ( response ) => {
if (
this.isStillMounted &&
fetchRequest === this.currentFetchRequest &&
response
) {
this.setState( { response: response.rendered } );
}
} )
.catch( ( error ) => {
if (
this.isStillMounted &&
fetchRequest === this.currentFetchRequest
) {
this.setState( {
response: {
error: true,
errorMsg: error.message,
},
} );
}
} ) );
return fetchRequest;
}
render() {
const response = this.state.response;
const {
className,
EmptyResponsePlaceholder,
ErrorResponsePlaceholder,
LoadingResponsePlaceholder,
} = this.props;
if ( response === '' ) {
return (
<EmptyResponsePlaceholder
response={ response }
{ ...this.props }
/>
);
} else if ( ! response ) {
return (
<LoadingResponsePlaceholder
response={ response }
{ ...this.props }
/>
);
} else if ( response.error ) {
return (
<ErrorResponsePlaceholder
response={ response }
{ ...this.props }
/>
);
}
return (
<RawHTML key="html" className={ className }>
{ response }
</RawHTML>
);
}
}
ServerSideRender.defaultProps = {
EmptyResponsePlaceholder: ( { className } ) => (
<Placeholder className={ className }>
{ __( 'Block rendered as empty.' ) }
</Placeholder>
),
ErrorResponsePlaceholder: ( { response, className } ) => {
const errorMessage = sprintf(
// translators: %s: error message describing the problem
__( 'Error loading block: %s' ),
response.errorMsg
);
return (
<Placeholder className={ className }>{ errorMessage }</Placeholder>
);
},
LoadingResponsePlaceholder: ( { className } ) => {
return (
<Placeholder className={ className }>
<Spinner />
</Placeholder>
);
},
};
export default ServerSideRender;