-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathserver-side-render.js
152 lines (139 loc) · 3.44 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
/**
* 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, urlQueryArgs = {} } = props;
const path = rendererPath( block, attributes, urlQueryArgs );
// 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 } )
.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;