Developer Advisory: Lazy Loading of WooCommerce Analytics REST API

The details

WooCommerce 10.4 introduces performance improvements through lazy loading of the WooCommerce Analytics REST API namespace (wc-analytics). This change significantly reduces the Time to First Byte (TTFB) for REST API requests that don’t require analytics endpoints.

Performance improvements

Local testing demonstrates substantial performance gains:

  • Up to 100ms improvement for REST API requests that don’t use WooCommerce Analytics endpoints, with most endpoints seeing significant reductions in response time

These improvements are achieved by deferring the registration of wc-analytics namespace controllers until they’re actually needed. Previously, over 100ms could be spent loading the WooCommerce Admin API on every REST request, even when those endpoints weren’t being used.

The lazy loading mechanism works by:

  1. Checking if the current REST route matches the wc-analytics namespace
  2. Registering controllers on-demand via the rest_pre_dispatch hook
  3. Supporting internal API calls made through rest_do_request()

How can I tell if this affects me?

Backward Compatibility Considerations

While this change is designed to be backward compatible, developers of extensions that extend the wc-analytics REST namespace should be aware of the following behavioral changes:

Controller Loading Order

What Changed: Analytics controllers now register after other REST routes, rather than during the standard rest_api_init sequence.

Potential Impact:

  • Extensions that depend on a specific controller registration order may encounter issues
  • Route iteration performance characteristics may change slightly (the WooCommerce Analytics API makes internal requests during registration that now iterate through more routes)
  • Priority-sensitive route handlers may behave differently

Note: This is unlikely to affect most extensions, but developers with order-dependent code should test their integrations.

What action should I take?

Disabling Lazy Loading

If your extension encounters compatibility issues with lazy loading, you can disable it using the woocommerce_rest_should_lazy_load_namespace filter:

Disable lazy loading for all namespaces:

add_filter( 'woocommerce_rest_should_lazy_load_namespace', '__return_false' );

Disable lazy loading for specific namespaces only:

add_filter( 'woocommerce_rest_should_lazy_load_namespace', function( $should_lazy_load, $namespace ) { // Disable lazy loading only for wc-analytics if ( 'wc-analytics' === $namespace ) { return false; } return $should_lazy_load; }, 10, 2 );

Important: Disabling lazy loading will revert to the previous behavior where all controllers load on every REST request, eliminating the performance benefits. This should only be used as a temporary compatibility measure while you update your extension.


Leave a Reply

Your email address will not be published. Required fields are marked *