PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.5.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.5.0
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Admin / package / settings.js

settings.js in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 1.5.0, at src/Performance/Admin/package/settings.js

402 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Solid Performance Settings
3 */
4 import { __ } from '@wordpress/i18n';
5 import { useState, useEffect, useRef } from 'react';
6 import {
7 Card,
8 CardBody,
9 CardHeader,
10 Button,
11 TextareaControl,
12 TextControl,
13 ToggleControl,
14 TabPanel,
15 BaseControl,
16 Flex,
17 FlexItem,
18 } from '@wordpress/components';
19 import { useDispatch, useSelect } from '@wordpress/data';
20 import apiFetch from '@wordpress/api-fetch';
21 import Notices from './notices.js';
22 import { store as noticesStore } from '@wordpress/notices';
23 import { store as preloadStore } from './preload/store';
24 import { PreloadingProgress } from './preload/components/progress';
25 import { PreloadButton } from './preload/components/button';
26 import { CacheCounter } from './preload/components/cache-counter';
27
28 /**
29 * Import Css
30 */
31 import './editor.scss';
32
33
34 const SolidPerformanceSettings = () => {
35 const { createErrorNotice, createSuccessNotice } = useDispatch(noticesStore);
36 const [ performanceSettings, setPerformanceSettings ] = useState(swspParams.settings);
37
38 /** @type {PreloadState} */
39 const preloadStatus = useSelect(( select ) => select(preloadStore).getPreloadStatus(), []);
40 const { isPreloading, cacheCount } = preloadStatus;
41
42 /** @type {PreloadStoreCallables} */
43 const storeDispatch = useDispatch(preloadStore);
44 const { refreshPreloadStatus } = storeDispatch;
45
46 const fetchSettings = async () => {
47 try {
48 const { solid_performance_settings } = await apiFetch({
49 path: '/wp/v2/settings',
50 });
51 setPerformanceSettings(solid_performance_settings);
52 } catch (error) {
53 createErrorNotice(__('Error: Unable to load settings.', 'solid-performance'));
54 console.error(error);
55 }
56 };
57
58 /**
59 * Fetch the current preload status and register any notices.
60 *
61 * @returns {Promise<PreloadState>}
62 */
63 const fetchPreloadStatus = async () => {
64 return refreshPreloadStatus();
65 };
66
67 // Initialize settings and preload status.
68 useEffect(() => {
69 void fetchSettings();
70 void fetchPreloadStatus();
71 }, []);
72
73 // Check preloading status more frequently if we are currently preloading.
74 useEffect(() => {
75 const intervalTime = isPreloading ? 3000 : 8000;
76 const interval = setInterval(fetchPreloadStatus, intervalTime);
77
78 return () => clearInterval(interval);
79 }, [ preloadStatus ]);
80
81 const handleSubmit = async ( event ) => {
82 event.preventDefault();
83 const { solid_performance_settings } = await apiFetch({
84 path: '/wp/v2/settings',
85 method: 'POST',
86 data: {
87 solid_performance_settings: {
88 ...performanceSettings,
89 },
90 },
91 }).catch(( error ) => {
92 createErrorNotice(__('Error Saving Settings', 'solid-performance'), {
93 type: 'snackbar',
94 });
95 console.error(error);
96 });
97 if (solid_performance_settings) {
98 createSuccessNotice(__('Settings Saved', 'solid-performance'), {
99 type: 'snackbar',
100 });
101 setPerformanceSettings(solid_performance_settings);
102 }
103 };
104 const handleClearCache = async () => {
105 await apiFetch({
106 path: '/solid-performance/v1/page/clear',
107 method: 'POST',
108 }).catch(( error ) => {
109 createErrorNotice(error.message, {
110 type: 'snackbar',
111 });
112 console.error(error);
113 });
114 // Handle clear cache
115 createSuccessNotice(__('Cache Cleared', 'solid-performance'), {
116 type: 'snackbar',
117 });
118
119 await fetchPreloadStatus();
120 };
121
122 const handleAdvancedRegenerate = async () => {
123 const regenerated = await apiFetch({
124 path: '/solid-performance/v1/page/regenerate',
125 method: 'POST',
126 }).catch(( error ) => {
127 createErrorNotice(__('Error Regenerating', 'solid-performance'), {
128 type: 'snackbar',
129 });
130 console.error(error);
131 });
132
133 if (regenerated.code !== 'solid_performance_advanced_cache_regenerated') {
134 createErrorNotice(__('Error Regenerating', 'solid-performance'), {
135 type: 'snackbar',
136 });
137 console.error(regenerated);
138 } else {
139 // Handle advanced cache regeneration
140 createSuccessNotice(__('advanced-cache.php Regenerated', 'solid-performance'), {
141 type: 'snackbar',
142 });
143
144 // Remove our WordPress notice, if it's displayed
145 const notice = document.getElementById('solidwp-performance-inactive');
146 if (notice) {
147 notice.remove();
148 }
149 }
150 };
151 const setState = ( newState ) => {
152 setPerformanceSettings({
153 ...performanceSettings,
154 ...newState,
155 page_cache: {
156 ...performanceSettings?.page_cache,
157 ...newState?.page_cache,
158 },
159 });
160 };
161 const tabs = [
162 {
163 name: 'basic',
164 title: __('Basic', 'solid-performance'),
165 className: 'basic-tab',
166 },
167 {
168 name: 'advanced',
169 title: __('Advanced', 'solid-performance'),
170 className: 'advanced-tab',
171 },
172 ];
173 return (
174 <>
175 <Flex align="baseline">
176 <FlexItem>
177 <h1>{__('Performance Settings', 'solid-performance')}</h1>
178 </FlexItem>
179 <FlexItem style={{ paddingRight: '20px' }} className="swpsp-page-count">
180 <CacheCounter count={cacheCount} />
181 </FlexItem>
182 </Flex>
183 <Notices/>
184 <form className={'swpsp-settings-form'} onSubmit={handleSubmit}>
185 <TabPanel
186 className="swpsp-settings-section-tabs"
187 orientation="vertical"
188 tabs={tabs}>
189 {
190 ( tab ) => {
191 switch (tab.name) {
192 case 'basic':
193 return (
194 <Card>
195 <CardHeader>
196 <header>
197 <h2>
198 {__('Basic Settings', 'solid-performance')}
199 </h2>
200 {performanceSettings?.page_cache?.enabled && (
201 <>
202 <Button variant="secondary"
203 onClick={handleClearCache}
204 disabled={isPreloading}>
205 {__('Purge Page Cache', 'solid-performance')}
206 </Button>
207 <PreloadButton
208 isPreloading={isPreloading}
209 text={__('Preload Uncached Pages', 'solid-performance')}
210 force={false}
211 hidden={isPreloading}
212 label={__(
213 'Generates cache files only for URLs that are not already cached',
214 'solid-performance'
215 )}
216 />
217 <PreloadButton
218 isPreloading={isPreloading}
219 text={__('Preload & Refresh All', 'solid-performance')}
220 force={true}
221 label={__(
222 'Cached URLs are served from the existing cache until each is replaced during the site-wide preloading pass',
223 'solid-performance'
224 )}
225 />
226 </>
227 )}
228 <Button
229 variant="text"
230 href="https://go.solidwp.com/performance-page-caching"
231 icon={() => (
232 <svg xmlns="http://www.w3.org/2000/svg"
233 viewBox="0 0 24 24"
234 width="24" height="24" aria-hidden="true"
235 focusable="false">
236 <path
237 d="M12 4.75a7.25 7.25 0 100 14.5 7.25 7.25 0 000-14.5zM3.25 12a8.75 8.75 0 1117.5 0 8.75 8.75 0 01-17.5 0zM12 8.75a1.5 1.5 0 01.167 2.99c-.465.052-.917.44-.917 1.01V14h1.5v-.845A3 3 0 109 10.25h1.5a1.5 1.5 0 011.5-1.5zM11.25 15v1.5h1.5V15h-1.5z"></path>
238 </svg>
239 )}
240 target="_blank"
241 size="small"
242 showTooltip={true}
243 label="View external documentation"
244 />
245 </header>
246 </CardHeader>
247 <CardBody>
248 <PreloadingProgress preloadStatus={preloadStatus} />
249 <ToggleControl
250 label={__('Enable Page Cache', 'solid-performance')}
251 checked={performanceSettings?.page_cache?.enabled ||
252 false}
253 className={'swpsp-large-toggle'}
254 onChange={( value ) => setState(
255 { page_cache: { enabled: value } })}
256 />
257 <p className="submit">
258 <Button variant="primary" type="submit">
259 {__('Save', 'solid-performance')}
260 </Button>
261 </p>
262 </CardBody>
263 </Card>
264 );
265 case 'advanced':
266 return (
267 <>
268 <Card>
269 <CardHeader>
270 <header>
271 <h2>
272 {__('Advanced Settings', 'solid-performance')}
273 </h2>
274 <Button
275 variant="text"
276 href="https://go.solidwp.com/performance-exclusions"
277 icon={() => (
278 <svg xmlns="http://www.w3.org/2000/svg"
279 viewBox="0 0 24 24" width="24"
280 height="24"
281 aria-hidden="true" focusable="false">
282 <path
283 d="M12 4.75a7.25 7.25 0 100 14.5 7.25 7.25 0 000-14.5zM3.25 12a8.75 8.75 0 1117.5 0 8.75 8.75 0 01-17.5 0zM12 8.75a1.5 1.5 0 01.167 2.99c-.465.052-.917.44-.917 1.01V14h1.5v-.845A3 3 0 109 10.25h1.5a1.5 1.5 0 011.5-1.5zM11.25 15v1.5h1.5V15h-1.5z"></path>
284 </svg>
285 )}
286 target="_blank"
287 size="small"
288 showTooltip={true}
289 label="View external documentation"
290 />
291 </header>
292 </CardHeader>
293 <CardBody>
294 <ToggleControl
295 label={__('Lazy Load Images', 'solid-performance')}
296 checked={performanceSettings?.page_cache?.lazy_loading?.enabled ||
297 false}
298 help={__('Automatically lazy load inline CSS background images.', 'solid-performance')}
299 onChange={( value ) => setState(
300 {
301 page_cache: {
302 lazy_loading: {
303 enabled: value
304 }
305 },
306 })}
307 />
308 <TextareaControl
309 label={__('Cache Exclusions', 'solid-performance')}
310 help={__(
311 'Enter URLs for pages, or wildcard exclusion patterns to exclude from the cache, one per line.',
312 'solid-performance')}
313 placeholder={__('/example/*', 'solid-performance')}
314 value={performanceSettings?.page_cache?.exclusions?.join(
315 '\n') || ''}
316 onChange={( value ) => setState(
317 {
318 page_cache: {
319 exclusions: value.split('\n'),
320 },
321 })}
322 />
323 <p className="submit">
324 <Button variant="primary" type="submit">
325 {__('Save', 'solid-performance')}
326 </Button>
327 </p>
328 </CardBody>
329 </Card>
330 <Card>
331 <CardHeader>
332 <header>
333 <h2>
334 {__('Debug', 'solid-performance')}
335 </h2>
336 <Button
337 variant="text"
338 href="https://go.solidwp.com/performance-directory"
339 icon={() => (
340 <svg xmlns="http://www.w3.org/2000/svg"
341 viewBox="0 0 24 24" width="24"
342 height="24"
343 aria-hidden="true" focusable="false">
344 <path
345 d="M12 4.75a7.25 7.25 0 100 14.5 7.25 7.25 0 000-14.5zM3.25 12a8.75 8.75 0 1117.5 0 8.75 8.75 0 01-17.5 0zM12 8.75a1.5 1.5 0 01.167 2.99c-.465.052-.917.44-.917 1.01V14h1.5v-.845A3 3 0 109 10.25h1.5a1.5 1.5 0 011.5-1.5zM11.25 15v1.5h1.5V15h-1.5z"></path>
346 </svg>
347 )}
348 target="_blank"
349 size="small"
350 showTooltip={true}
351 label="View external documentation"
352 />
353 </header>
354 </CardHeader>
355 <CardBody>
356 <ToggleControl
357 label={__('Enable Debug Mode', 'solid-performance')}
358 help={__(
359 'Enable debug mode to log cache status and performance information.',
360 'solid-performance')}
361 checked={performanceSettings?.page_cache?.debug ||
362 false}
363 onChange={( value ) => setState(
364 { page_cache: { debug: value } })}
365 />
366 <TextControl
367 label={__('Cache File Directory',
368 'solid-performance')}
369 help={__(
370 'The directory where cache files are stored on the server.',
371 'solid-performance')}
372 value={swspParams?.cache_path || ''}
373 onChange={() => {
374 }} // Read only
375 readOnly
376 />
377 <BaseControl>
378 <Button variant="secondary"
379 onClick={handleAdvancedRegenerate}>
380 {__('Regenerate the advanced-cache.php file',
381 'solid-performance')}
382 </Button>
383 </BaseControl>
384 <p className="submit">
385 <Button variant="primary" type="submit">
386 {__('Save', 'solid-performance')}
387 </Button>
388 </p>
389 </CardBody>
390 </Card>
391 </>
392 );
393 }
394 }
395 }
396 </TabPanel>
397 </form>
398 </>
399 );
400 };
401 export default SolidPerformanceSettings;
402