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 API: add initialize method and deprecate useSteps #97999

Merged
merged 33 commits into from
Jan 22, 2025

Conversation

alshakero
Copy link
Member

@alshakero alshakero commented Jan 6, 2025

Proposal

This implements a new method to Stepper framework API called initialize . This method is an asynchronous method that is called once per flow to boot the flow. It should do any prep actions (fetch experiments, assert conditions, redirect to other flows, etc...), and most importantly return the steps.

This also deprecated the useSteps hook because it has a few flaws.

Why

Issues with useSteps hook

The useSteps hook gives the impression that steps can be dynamic, which is incorrect for a few reasons:

  • Stepper redirects to the first step in the flow on load. So if the first step changes, a race condition occurs and the user may be stuck on an incorrect first step.
  • The steps array was created over and over with a new identity every time. This can trigger excess re-renders and fetches. Because steppers fetches steps asynchronously, having the array regenerated can affect performance. @jsnajdr nails the explanation here.
  • This forced us to introduce useAssertConditions hooks. Which blocks rendering the flow until it returns SUCCESS. The real purpose of this hook is to turn the repeating pattern of React renders into a classic procedural asynchronous function.

Enter async initialize

  • This method, as the name suggests, will run once per page load. It can be asynchronous. So you can load your experiments, assert some checks, etc. Then return the steps that you want. This is much more robust than the React hook.
    • Because if you, for example, redirect, the app won't keep re-rendering until the browser kills the main thread. This hook runs before React is engaged at all.
    • You can handle errors with a good old try and catch block.
    • You can issue network requests, or any async tasks, without any complexity.

useSideEffects and useAssertConditions are no longer needed

Since initialize runs once and first, these two hooks are not needed in most cases.

Example without useSideEffects

Before

image

After

image

Example without useAssertConditions

Before

image

After

image

Testing steps

This change is completely backwards compatible and shouldn't change anything for the existing flows. But we should test to make sure we don't break anything.

Test a flow

  1. While incognito, go to /setup/onboarding
  2. Signup and create a site. Keep the site URL noted.
  3. All should go well.
  4. Go through the flow again while logged in.
  5. All should go well.

Test a flow with non-Stepper auth

  1. While incognito, go to /setup/newsletter.
  2. Signup and create a site.
  3. All should go well.
  4. Go through the flow again while logged in.
  5. All should go well.

Test a variant flow

  1. Create a Jurassic site (https://jurassic.ninja/).
  2. Go to /setup/hosted-site-migration.
  3. Import the Jurassic site into the site you noted above.
  4. All should work.

@matticbot
Copy link
Contributor

matticbot commented Jan 6, 2025

Here is how your PR affects size of JS and CSS bundles shipped to the user's browser:

App Entrypoints (~467 bytes added 📈 [gzipped])

name                   parsed_size           gzip_size
entry-stepper              +4768 B  (+0.4%)     +498 B  (+0.1%)
entry-main                  -233 B  (-0.0%)      +27 B  (+0.0%)
entry-login                 -127 B  (-0.0%)      +56 B  (+0.0%)
entry-domains-landing       -127 B  (-0.0%)      +56 B  (+0.0%)
entry-browsehappy           -127 B  (-0.1%)      +56 B  (+0.1%)
entry-subscriptions         -125 B  (-0.0%)      +54 B  (+0.0%)

Common code that is always downloaded and parsed every time the app is loaded, no matter which route is used.

Sections (~8384 bytes added 📈 [gzipped])

name                               parsed_size           gzip_size
example-flow                          +19243 B    (new)    +6527 B    (new)
media                                   +297 B  (+0.0%)     -313 B  (-0.1%)
theme                                   +244 B  (+0.0%)      +53 B  (+0.0%)
site-settings                           +244 B  (+0.0%)       +8 B  (+0.0%)
site-overview                           +244 B  (+0.0%)       +9 B  (+0.0%)
hosting                                 +244 B  (+0.0%)       +9 B  (+0.0%)
themes                                  +231 B  (+0.0%)      +33 B  (+0.0%)
site-purchases                          +231 B  (+0.0%)      +32 B  (+0.0%)
security                                +231 B  (+0.0%)      +30 B  (+0.0%)
purchases                               +231 B  (+0.0%)      +29 B  (+0.0%)
jetpack-connect                         +231 B  (+0.0%)      +30 B  (+0.0%)
checkout                                +231 B  (+0.0%)      +35 B  (+0.0%)
devdocs                                 -228 B  (-0.1%)      -32 B  (-0.1%)
staging-site                            +221 B  (+0.0%)       -5 B  (-0.0%)
sites-dashboard                         +221 B  (+0.0%)       -5 B  (-0.0%)
site-tools                              +221 B  (+0.0%)       -5 B  (-0.0%)
site-performance                        +221 B  (+0.0%)       -5 B  (-0.0%)
site-monitoring                         +221 B  (+0.0%)       -5 B  (-0.0%)
site-marketing                          +221 B  (+0.0%)       -5 B  (-0.0%)
site-logs                               +221 B  (+0.0%)       -5 B  (-0.0%)
hosting-features                        +221 B  (+0.0%)       -5 B  (-0.0%)
github-deployments                      +221 B  (+0.0%)       -5 B  (-0.0%)
site-migration-flow                     -214 B  (-0.4%)      -44 B  (-0.2%)
migration-flow                          -214 B  (-0.5%)      -44 B  (-0.3%)
hosted-site-migration-flow              -214 B  (-0.4%)      -44 B  (-0.2%)
transferring-hosted-site-flow           +208 B  (+0.1%)      +18 B  (+0.0%)
switch-site                             +208 B  (+0.1%)      +18 B  (+0.0%)
subscribers                             +208 B  (+0.0%)      +18 B  (+0.0%)
reader                                  +208 B  (+0.0%)      +18 B  (+0.0%)
performance-profiler                    +208 B  (+0.1%)      +17 B  (+0.0%)
migrate                                 +208 B  (+0.1%)      +21 B  (+0.0%)
jetpack-cloud-plugin-management         +208 B  (+0.0%)      +18 B  (+0.0%)
jetpack-cloud-partner-portal            +208 B  (+0.0%)      +19 B  (+0.0%)
jetpack-cloud-overview                  +208 B  (+0.0%)      +17 B  (+0.0%)
jetpack-cloud-agency-dashboard          +208 B  (+0.0%)      +20 B  (+0.0%)
jetpack-cloud                           +208 B  (+0.1%)      +18 B  (+0.0%)
a8c-for-agencies-team                   +208 B  (+0.0%)      +18 B  (+0.0%)
a8c-for-agencies-sites                  +208 B  (+0.0%)      +18 B  (+0.0%)
a8c-for-agencies-settings               +208 B  (+0.1%)      +19 B  (+0.0%)
a8c-for-agencies-referrals              +208 B  (+0.0%)      +18 B  (+0.0%)
a8c-for-agencies-purchases              +208 B  (+0.0%)      +19 B  (+0.0%)
a8c-for-agencies-plugins                +208 B  (+0.1%)      +19 B  (+0.0%)
a8c-for-agencies-overview               +208 B  (+0.0%)      +18 B  (+0.0%)
a8c-for-agencies-migrations             +208 B  (+0.0%)      +18 B  (+0.0%)
a8c-for-agencies-marketplace            +208 B  (+0.0%)      +18 B  (+0.0%)
a8c-for-agencies-landing                +208 B  (+0.1%)      +19 B  (+0.0%)
a8c-for-agencies-client                 +208 B  (+0.0%)      +18 B  (+0.0%)
a8c-for-agencies-agency-tier            +208 B  (+0.1%)      +19 B  (+0.0%)
a8c-for-agencies                        +208 B  (+0.2%)      +19 B  (+0.1%)
write-flow                              -178 B  (-0.0%)     -484 B  (-0.1%)
build-flow                              -178 B  (-0.0%)     -469 B  (-0.1%)
import-hosted-site-flow                 -162 B  (-0.0%)      -87 B  (-0.0%)
hundred-year-plan                       -148 B  (-0.1%)      -29 B  (-0.1%)
hundred-year-domain                     -148 B  (-0.1%)      -21 B  (-0.0%)
settings                                +141 B  (+0.0%)      -17 B  (-0.0%)
promote-post-i2                         +105 B  (+0.0%)      +12 B  (+0.0%)
backup                                  +105 B  (+0.0%)      +14 B  (+0.0%)
update-design-flow                       +97 B  (+0.0%)      -47 B  (-0.0%)
link-in-bio-tld-flow                     -86 B  (-0.0%)     -519 B  (-0.1%)
start-writing-flow                       +66 B  (+0.2%)       +7 B  (+0.1%)
newsletter-flow                          +66 B  (+0.2%)       +5 B  (+0.1%)
design-first-flow                        +66 B  (+0.2%)       +5 B  (+0.1%)
plugins                                  +48 B  (+0.0%)     -680 B  (-0.1%)
newsletter-post-setup-flow               -32 B  (-0.0%)      -21 B  (-0.0%)
link-in-bio-post-setup-flow              -32 B  (-0.0%)      -21 B  (-0.0%)
site-blocks                              +23 B  (+0.0%)      +11 B  (+0.0%)
signup                                   +23 B  (+0.0%)      +13 B  (+0.0%)
privacy                                  +23 B  (+0.0%)      +11 B  (+0.0%)
notification-settings                    +23 B  (+0.0%)      +11 B  (+0.0%)
me                                       +23 B  (+0.0%)      +11 B  (+0.0%)
marketplace                              +23 B  (+0.0%)      +12 B  (+0.0%)
jetpack-cloud-settings                   +23 B  (+0.0%)      +14 B  (+0.0%)
jetpack-cloud-pricing                    +23 B  (+0.0%)      +14 B  (+0.0%)
jetpack-cloud-features-comparison        +23 B  (+0.0%)      +14 B  (+0.0%)
home                                     +23 B  (+0.0%)      +14 B  (+0.0%)
help                                     +23 B  (+0.0%)      +11 B  (+0.0%)
email                                    +23 B  (+0.0%)      +14 B  (+0.0%)
domains                                  +23 B  (+0.0%)      +14 B  (+0.0%)
developer                                +23 B  (+0.0%)      +11 B  (+0.0%)
account-close                            +23 B  (+0.0%)      +11 B  (+0.0%)
account                                  +23 B  (+0.0%)      +11 B  (+0.0%)

Sections contain code specific for a given set of routes. Is downloaded and parsed only when a particular route is navigated to.

Async-loaded Components (~4760 bytes removed 📉 [gzipped])

name                                                                              parsed_size           gzip_size
async-load-design-blocks                                                               +254 B  (+0.0%)     -856 B  (-0.1%)
async-load-signup-steps-theme-selection                                                +208 B  (+0.1%)      +21 B  (+0.0%)
async-load-design-wordpress-components-gallery                                         +208 B  (+0.0%)      +15 B  (+0.0%)
async-load-comment-block-editor                                                        +208 B  (+0.0%)      +17 B  (+0.0%)
async-load-calypso-lib-account-settings-helper                                         -208 B  (-0.1%)      -23 B  (-0.0%)
async-load-calypso-layout-command-palette                                              +208 B  (+0.1%)      +18 B  (+0.0%)
async-load-calypso-jetpack-cloud-sections-sidebar-navigation-manage-selected-...       +208 B  (+0.2%)      +18 B  (+0.0%)
async-load-calypso-blocks-jitm-templates-modal                                         +208 B  (+0.4%)      +15 B  (+0.1%)
async-load-automattic-whats-new                                                        +208 B  (+0.6%)      +18 B  (+0.1%)
async-load-automattic-help-center-stepper                                              +208 B  (+0.0%)      +15 B  (+0.0%)
async-load-automattic-help-center                                                      +208 B  (+0.0%)      +15 B  (+0.0%)
async-load-calypso-post-editor-editor-media-modal                                      -173 B  (-0.0%)     -315 B  (-0.1%)
async-load-calypso-post-editor-media-modal                                             -117 B  (-0.0%)     -284 B  (-0.1%)
async-load-purchase-modal-wrapper                                                       +23 B  (+0.0%)      +13 B  (+0.0%)
async-load-my-sites-checkout-purchase-modal-is-eligible-for-one-click-checkou...        +23 B  (+0.0%)      +12 B  (+0.0%)
async-load-calypso-my-sites-current-site-stale-cart-items-notice                        +23 B  (+0.1%)      +13 B  (+0.2%)
async-load-calypso-layout-masterbar-checkout                                            +23 B  (+0.0%)      +15 B  (+0.1%)
async-load-calypso-blocks-editor-checkout-modal                                         +23 B  (+0.0%)      +14 B  (+0.0%)
async-load-signup-steps-plans-theme-preselected                                         -13 B  (-0.0%)      -58 B  (-0.0%)
async-load-signup-steps-plans                                                           -13 B  (-0.0%)      -58 B  (-0.0%)
async-load-design-playground                                                            +13 B  (+0.0%)     -139 B  (-0.0%)
async-load-design                                                                       +13 B  (+0.0%)     -139 B  (-0.0%)

React components that are loaded lazily, when a certain part of UI is displayed for the first time.

Legend

What is parsed and gzip size?

Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory.
Gzip Size: Compressed size of the JS and CSS files. This much data needs to be downloaded over network.

Generated by performance advisor bot at iscalypsofastyet.com.

@alshakero alshakero changed the title Stepper v2: Implement new API - add bootFlow method Stepper API: add bootFlow method Jan 6, 2025
@alshakero alshakero marked this pull request as ready for review January 7, 2025 17:00
@alshakero alshakero requested a review from a team as a code owner January 7, 2025 17:00
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jan 7, 2025
@alshakero alshakero changed the title Stepper API: add bootFlow method Stepper API: add initialize method and deprecate useSteps Jan 9, 2025
@escapemanuele escapemanuele requested a review from a team January 13, 2025 17:30
__experimentalUseBuiltinAuth?: boolean;

/**
* The steps of the flow. **Please don't use this variable unless absolutely necessary**. It's means to be used internally by the Stepper.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* The steps of the flow. **Please don't use this variable unless absolutely necessary**. It's means to be used internally by the Stepper.
* The steps of the flow. **Please don't use this variable unless absolutely necessary**. It's meant to be used internally by the Stepper.

return;
}

// Checking for initialize implies this a V2 flow.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Checking for initialize implies this a V2 flow.
// Checking for initialize implies this is a V2 flow.

// Checking for initialize implies this a V2 flow.
// CLEAN UP: once the `onboarding` flow is migrated to V2, this can be cleaned up to only support V2
// The `onboarding` flow is the only flow that uses in-stepper auth so far, so all the auth logic catering V1 can be deleted.
if ( 'initialize' in flow && flowSteps ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to check for initialize here? If we have flowSteps then we already checked in
let flowSteps = 'initialize' in flow ? await flow.initialize() : null;
right?

Copy link
Member Author

@alshakero alshakero Jan 14, 2025

Choose a reason for hiding this comment

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

Good catch. I thought TS wouldn't guess the version!

TS complains if I don't check for initialize, because __flowSteps doesn't exist in V1 flows.

@@ -135,6 +136,8 @@ const availableFlows: Record< string, () => Promise< { default: Flow } > > = {
),
[ MIGRATION_FLOW ]: () =>
import( /* webpackChunkName: "migration-flow" */ '../declarative-flow/migration' ),
[ EXAMPLE_FLOW ]: () =>
import( /* webpackChunkName: "migration-flow" */ '../declarative-flow/example' ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
import( /* webpackChunkName: "migration-flow" */ '../declarative-flow/example' ),
import( /* webpackChunkName: "example-flow" */ '../declarative-flow/example' ),

@@ -36,7 +36,7 @@ const getNextStep = ( currentStep: string, steps: StepperStep[] ): string | unde

const SiteIntent = Onboard.SiteIntent;

const pluginBundleFlow: Flow = {
const pluginBundleFlow: FlowV1 = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a particular reason we chose these flows and upgraded their type, or can it be done in a following PR if needed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes because these flows depend on useAssertConditions which doesn't exist in V2.

@agrullon95 agrullon95 self-requested a review January 15, 2025 06:40
@agrullon95
Copy link
Contributor

agrullon95 commented Jan 15, 2025

Testing steps

Test a flow

/setup/onboarding

  • Signup and create a site. Keep the site URL noted. All should go well.
  • Go through the flow again while logged in. All should go well.

Test a flow with non-Stepper auth

/setup/newsletter

  • Signup and create a site. All should go well.
  • Go through the flow again while logged in. All should go well.

Test a variant flow

jurassic ninja - hosted-site-migration

  • Go to /setup/hosted-site-migration. Import the Jurassic site into the site you noted above. All should work.

@agrullon95 agrullon95 self-requested a review January 15, 2025 07:26
Copy link
Contributor

@agrullon95 agrullon95 left a comment

Choose a reason for hiding this comment

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

I followed the testing instructions and encountered no issues with the flows. Additionally, the README explanation for the new initialize method and example flow is clear and provides a solid starting point.

@matticbot
Copy link
Contributor

This PR modifies the release build for the following Calypso Apps:

For info about this notification, see here: PCYsg-OT6-p2

  • notifications
  • wpcom-block-editor

To test WordPress.com changes, run install-plugin.sh $pluginSlug fix/use-steps-hook on your sandbox.

@escapemanuele
Copy link
Contributor

@alshakero this is still on hold to better work with the new proposal?

@alshakero
Copy link
Member Author

No it's good to go. I'll merge soon.

@alshakero alshakero merged commit d0c13e7 into trunk Jan 22, 2025
13 checks passed
@alshakero alshakero deleted the fix/use-steps-hook branch January 22, 2025 15:24
@github-actions github-actions bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Jan 22, 2025
@a8ci18n
Copy link

a8ci18n commented Jan 22, 2025

This Pull Request is now available for translation here: https://translate.wordpress.com/deliverables/17203594

Some locales (Hebrew, Japanese) have been temporarily machine-translated due to translator availability. All other translations are usually ready within a few days. Untranslated and machine-translated strings will be sent for translation next Monday and are expected to be completed by the following Friday.

Thank you @alshakero for including a screenshot in the description! This is really helpful for our translators.

@a8ci18n
Copy link

a8ci18n commented Jan 26, 2025

Translation for this Pull Request has now been finished.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants