Skip to content

Feature Request: Lazy Loading / On-Demand Toggle Fetching #260

@juliomariano-hotmart

Description

@juliomariano-hotmart

Describe the feature request

Summary

Add support for lazy loading toggles instead of fetching all toggles on initialization and refresh cycles to reduce bundle size and improve performance metrics.

Problem Description

Currently, the unleash-proxy-client loads all available toggles during:

  • Client initialization
  • Automatic refresh cycles

This behavior is causing several issues in our frontend application:

  • Increased bundle size: All toggle data is loaded upfront, even for toggles that may never be used
  • Performance degradation: Large payload on initial load impacts metrics like First Contentful Paint (FCP) and Largest Contentful Paint (LCP)
  • Network overhead: Unnecessary data transfer for unused feature flags
  • Memory consumption: Storing toggle data that might not be accessed during the session

We are using unleash-edge as our proxy and would like more granular control over when toggle data is fetched.

Background

Use Cases

Single Page Applications: Load only the toggles needed for the current route
Micro-frontends: Each micro-frontend loads only its relevant toggles
Performance-critical applications: Reduce initial bundle size and improve loading metrics
Limited bandwidth environments: Minimize data transfer

Benefits

Reduced initial payload size
Improved performance metrics (FCP, LCP)
Lower memory usage
Better network efficiency
Maintains backward compatibility
Flexible caching strategy

Solution suggestions

Proposed Solution

Add configuration options to enable lazy loading with the following behavior:

1. Configuration Option

const unleash = new UnleashClient({
  url: 'https://unleash-edge.example.com/proxy',
  clientKey: 'your-client-key',
  appName: 'my-app',
  // New configuration options
  fetchStrategy: 'lazy', // 'eager' (default) | 'lazy'
});

### 2. On-Demand Toggle Fetching
When fetchStrategy: 'lazy' is enabled:

No toggles are fetched during initialization
Toggles are fetched individually when isEnabled() is called for the first time
Implement caching to avoid redundant requests for the same toggle

```javascript
// This would trigger a network request only for 'my-feature' toggle
const isEnabled = await unleash.isEnabled('my-feature');

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions