Skip to content

Commit

Permalink
Stepper: Move conservative lazy loading (#97326)
Browse files Browse the repository at this point in the history
  • Loading branch information
alshakero authored Dec 12, 2024
1 parent 3cc5f4f commit 10e748a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
20 changes: 17 additions & 3 deletions client/landing/stepper/declarative-flow/internals/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,29 @@ export const FlowRenderer: React.FC< { flow: Flow } > = ( { flow } ) => {
// See https://github.com/Automattic/wp-calypso/pull/82981.
const selectedSite = useSelector( ( state ) => site && getSite( state, siteSlugOrId ) );

// this pre-loads all the lazy steps down the flow.
// this pre-loads the next step in the flow.
useEffect( () => {
const nextStepIndex = flowSteps.findIndex( ( step ) => step.slug === currentStepRoute ) + 1;
const nextStep = flowSteps[ nextStepIndex ];

// 0 implies the findIndex returned -1.
if ( nextStepIndex === 0 || ! nextStep ) {
return;
}

if ( siteSlugOrId && ! selectedSite ) {
// If this step depends on a selected site, only preload after we have the data.
// Otherwise, we're still waiting to render something meaningful, and we don't want to
// potentially slow that down by having the CPU busy initialising future steps.
return;
}
Promise.all( flowSteps.map( ( step ) => 'asyncComponent' in step && step.asyncComponent() ) );
if (
// Don't load anything on user step because the user step will hard-navigate anyways.
currentStepRoute !== 'user' &&
'asyncComponent' in nextStep
) {
nextStep.asyncComponent();
}
// Most flows sadly instantiate a new steps array on every call to `flow.useSteps()`,
// which means that we don't want to depend on `flowSteps` here, or this would end up
// running on every render. We thus depend on `flow` instead.
Expand All @@ -107,7 +121,7 @@ export const FlowRenderer: React.FC< { flow: Flow } > = ( { flow } ) => {
// different points. But even if they do, worst case scenario we only fail to preload
// some steps, and they'll simply be loaded later.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ flow, siteSlugOrId, selectedSite ] );
}, [ siteSlugOrId, selectedSite, currentStepRoute, flow ] );

const stepNavigation = useStepNavigationWithTracking( {
flow,
Expand Down
4 changes: 2 additions & 2 deletions client/landing/stepper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ window.AppBoot = async () => {
reduxStore.dispatch( setCurrentFlowName( flow.name ) );
reduxStore.dispatch( setSelectedSiteId( siteId ) as unknown as AnyAction );

await geolocateCurrencySymbol();
// No need to await this, it's not critical to the boot process and will slow booting down.
geolocateCurrencySymbol();

const root = createRoot( document.getElementById( 'wpcom' ) as HTMLElement );

Expand All @@ -179,7 +180,6 @@ window.AppBoot = async () => {
/>
</BrowserRouter>
<AsyncHelpCenter />

{ 'development' === process.env.NODE_ENV && (
<AsyncLoad require="calypso/components/webpack-build-monitor" placeholder={ null } />
) }
Expand Down
5 changes: 4 additions & 1 deletion client/landing/stepper/utils/enhanceFlowWithAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import type { Flow, StepperStep } from '../declarative-flow/internals/types';

const USER_STEP: StepperStep = {
slug: 'user',
asyncComponent: () => import( '../declarative-flow/internals/steps-repository/__user' ),
asyncComponent: () =>
import(
/* webpackChunkName: "stepper-user-step" */ '../declarative-flow/internals/steps-repository/__user'
),
};

function useInjectUserStepIfNeeded( flow: Flow ): StepperStep[] {
Expand Down

0 comments on commit 10e748a

Please sign in to comment.