DEV Community

Jen C.
Jen C.

Posted on

Optimize Core Web Vitals - FCP and LCP: Handle Offscreen <iframe> (3) - Use memo, startTransition, and setTimeout

Pre-study

Background

Previously, I tried implementing lazy loading for the <iframe> or updating the URL of the <iframe> (src attribute) on the window load event, as suggested in the posts below. However, when users interact with the <iframe> before all the resources are fully loaded (since the URL is still an empty string), it results in a significant delay while waiting to load the iframe’s resources and perform callback actions.

Solution: Step-by-Step Guide

Use setTimeout to delay URL state update:

When the LazyIframe component mounts, use setTimeout to delay updating the iframe's URL state variable. This ensures the resources load with lower priority but still before the user interacts with the iframe.

Check if iframe URL is already the target URL:

To avoid unnecessary timer creation and state updates, check if the current iframe URL is already equal to the target URL. If they are the same, return early to prevent further action.

Wrap state update with startTransition:

Since the iframe should load with lower priority than user interactions, wrap the state update inside startTransition to ensure that it doesn’t block active user interactions on the page.

Wrap the LazyIframe component with memo:

Use memo to wrap the LazyIframe component. This prevents unnecessary re-renders of the component, improving performance.

Example Implementation of LazyIframe Component

import { useEffect, useState, memo, startTransition } from 'react';

import { MY_AUTH_URL } from '../configs';

const iframeSrc = `${MY_AUTH_URL}/sso/iframePage.xhtml`;

const LazyIframe = memo(function LazyIframe() {
  const [src, setSrc] = useState('');
  useEffect(() => {
    if (src === iframeSrc) {
      return;
    }
    const timer = setTimeout(() => {
      startTransition(() => {
        setSrc(iframeSrc);
      });
    }, 100);
    return () => clearTimeout(timer);
  }, [src]);

  return (
    <iframe
      src={src}
      style={{ display: 'none' }}
      id='pidiframe'
      title='log out iframe'
    />
  );
});
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

GenAI LIVE! | June 4, 2025

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️