Skip to content

Conversation

@ricardo-devis-agullo
Copy link
Collaborator

@ricardo-devis-agullo ricardo-devis-agullo commented Sep 5, 2025

Add Lazy Loading Support for OpenComponents

Overview

This PR introduces lazy loading functionality for OpenComponents using the Intersection Observer API. Components marked with loading="lazy" will only render when they enter the viewport, improving initial page load performance and reducing unnecessary network requests.

Changes

  • Modified src/oc-client.js: Enhanced the connectedCallback method in the custom element class to support lazy loading
  • Added conditional logic to check for loading="lazy" attribute
  • Implemented IntersectionObserver to monitor component visibility
  • Components are rendered only when they become visible in the viewport
  • Observer is automatically disconnected after first intersection to prevent memory leaks

Implementation Details

The lazy loading mechanism works by:

  1. Checking if the component has loading="lazy" attribute during connectedCallback
  2. If lazy loading is enabled, creating an IntersectionObserver instead of immediately calling renderNestedComponent
  3. The observer monitors the component's visibility and triggers rendering when it enters the viewport
  4. The observer is disconnected after the first intersection to optimize performance

Usage

<!-- Standard component (renders immediately) -->
<oc-component href="https://example.com/component"></oc-component>

<!-- Lazy component (renders when visible) -->
<oc-component href="https://example.com/component" loading="lazy"></oc-component>

Benefits

  • Performance: Reduces initial page load time by deferring component rendering
  • Bandwidth: Prevents unnecessary network requests for off-screen components
  • User Experience: Components load seamlessly as users scroll
  • Backward Compatibility: Existing components continue to work without changes

Technical Notes

  • Uses native IntersectionObserver API for optimal performance
  • Maintains existing lifecycle management behavior for non-lazy components
  • Observer cleanup is handled automatically to prevent memory leaks
  • Compatible with existing disable-lifecycle attribute functionality

@ricardo-devis-agullo ricardo-devis-agullo merged commit 4e02b49 into master Sep 5, 2025
3 checks passed
@ricardo-devis-agullo ricardo-devis-agullo deleted the add-lazy-ocs branch September 5, 2025 18:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants