Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stepper: Move conservative lazy loading #97326

Merged
merged 15 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code style nit: let's clearly identify the condition when the next step is not found, i.e., when findIndex() === -1 of nextStep === undefined, and return early. Adding 1 to the -1 and then checking >0 is too convoluted IMO.


// 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, the main Calypso boot function also doesn't awat this.


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'
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why the step has such a name with the __ prefix. So that it always comes up first in alphabetical order?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hopefully an additional reminder, to the one in README, to prevent people from using it directly.

};

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