From 753c548f2983cb92859dbde6431328ccbf408c81 Mon Sep 17 00:00:00 2001
From: Adnan Haque <3737780+haqadn@users.noreply.github.com>
Date: Fri, 20 Dec 2024 17:38:43 -0500
Subject: [PATCH] Boost: Improve responsiveness during CCSS generation retry
(#40675)
* Move C.CSS retry down the element chain
* Optimistic update critical CSS regen
* Only start generation if there is any provider in the list
* changelog
* Change the order of state update
* Fix showstopper behavior for cloud CSS
* Use CCSS context also for cloud and move retried state to it
* Rename variable for clarity
* Update retried state after regeneration has started
---
.../cloud-css-meta/cloud-css-meta.tsx | 4 --
.../critical-css-context-provider.tsx} | 40 ++++++++++++++-----
.../critical-css-meta/critical-css-meta.tsx | 8 +---
.../critical-css/lib/critical-css-errors.ts | 5 +++
.../lib/stores/critical-css-state.ts | 38 +++++++++++++++---
.../critical-css/lib/use-retry-regenerate.ts | 9 ++---
.../show-stopper-error/show-stopper-error.tsx | 20 ++++------
.../features/critical-css/status/status.tsx | 6 ---
.../js/features/speed-score/speed-score.tsx | 2 +-
.../js/layout/settings-page/settings-page.tsx | 6 +--
.../boost/changelog/fix-critical-css-status | 4 ++
11 files changed, 87 insertions(+), 55 deletions(-)
rename projects/plugins/boost/app/assets/src/js/features/critical-css/{local-generator/local-generator-provider.tsx => critical-css-context/critical-css-context-provider.tsx} (73%)
create mode 100644 projects/plugins/boost/changelog/fix-critical-css-status
diff --git a/projects/plugins/boost/app/assets/src/js/features/critical-css/cloud-css-meta/cloud-css-meta.tsx b/projects/plugins/boost/app/assets/src/js/features/critical-css/cloud-css-meta/cloud-css-meta.tsx
index 8f1f3ae65660c..7dc510ff0e5ce 100644
--- a/projects/plugins/boost/app/assets/src/js/features/critical-css/cloud-css-meta/cloud-css-meta.tsx
+++ b/projects/plugins/boost/app/assets/src/js/features/critical-css/cloud-css-meta/cloud-css-meta.tsx
@@ -1,12 +1,10 @@
import { __ } from '@wordpress/i18n';
import Status from '../status/status';
import { useCriticalCssState } from '../lib/stores/critical-css-state';
-import { useRetryRegenerate } from '../lib/use-retry-regenerate';
import { isFatalError } from '../lib/critical-css-errors';
export default function CloudCssMetaProps() {
const [ cssState ] = useCriticalCssState();
- const [ hasRetried, retry ] = useRetryRegenerate();
const isPending = cssState.status === 'pending';
const hasCompletedSome = cssState.providers.some( provider => provider.status !== 'pending' );
@@ -29,8 +27,6 @@ export default function CloudCssMetaProps() {
cssState={ cssState }
isCloud={ true }
showFatalError={ isFatalError( cssState ) }
- hasRetried={ hasRetried }
- retry={ retry }
extraText={ extraText || undefined }
overrideText={ overrideText || undefined }
/>
diff --git a/projects/plugins/boost/app/assets/src/js/features/critical-css/local-generator/local-generator-provider.tsx b/projects/plugins/boost/app/assets/src/js/features/critical-css/critical-css-context/critical-css-context-provider.tsx
similarity index 73%
rename from projects/plugins/boost/app/assets/src/js/features/critical-css/local-generator/local-generator-provider.tsx
rename to projects/plugins/boost/app/assets/src/js/features/critical-css/critical-css-context/critical-css-context-provider.tsx
index deb0e59881b6e..0b2d52bffddfd 100644
--- a/projects/plugins/boost/app/assets/src/js/features/critical-css/local-generator/local-generator-provider.tsx
+++ b/projects/plugins/boost/app/assets/src/js/features/critical-css/critical-css-context/critical-css-context-provider.tsx
@@ -11,19 +11,23 @@ import {
import { runLocalGenerator } from '../lib/generate-critical-css';
import { CriticalCssErrorDetails } from '../lib/stores/critical-css-state-types';
-type LocalGeneratorContext = {
+type CriticalCssContextValues = {
isGenerating: boolean;
setGenerating: ( generating: boolean ) => void;
providerProgress: number;
setProviderProgress: ( progress: number ) => void;
+
+ // Whether we've retried generating critical CSS after an error.
+ hasRetriedAfterError: boolean;
+ setHasRetriedAfterError: ( hasRetried: boolean ) => void;
};
type ProviderProps = {
children: ReactNode;
};
-const CssGeneratorContext = createContext< LocalGeneratorContext | null >( null );
+const CriticalCssContext = createContext< CriticalCssContextValues | null >( null );
/**
* Local Critical CSS Context Provider component - provides context for any descendants that want to
@@ -31,28 +35,34 @@ const CssGeneratorContext = createContext< LocalGeneratorContext | null >( null
*
* @param {ProviderProps} props - Component props.
*/
-export default function LocalCriticalCssGeneratorProvider( { children }: ProviderProps ) {
+export default function CriticalCssProvider( { children }: ProviderProps ) {
const [ isGenerating, setGenerating ] = useState< boolean >( false );
const [ providerProgress, setProviderProgress ] = useState< number >( 0 );
+ const [ hasRetriedAfterError, setHasRetriedAfterError ] = useState< boolean >( false );
const value = {
+ // Local Generator status.
isGenerating,
setGenerating,
providerProgress,
setProviderProgress,
+
+ // Whether we've retried generating critical CSS after an error.
+ hasRetriedAfterError,
+ setHasRetriedAfterError,
};
- return
{ showRetry ? firstTimeError : secondTimeError }
+{ ! hasRetried ? firstTimeError : secondTimeError }
{ sprintf( /* translators: %s: error message */ @@ -192,7 +186,7 @@ const OtherErrors = ( { cssState, retry, showRetry, supportLink }: ShowStopperEr cssState.status_error ) }
- { showRetry ? ( + { ! hasRetried ? (