Blog>
Snippets

Preloading Important Images to Improve Visual Readiness

Use Next.js's <link> component to preload key images that will be in the initial viewport, ensuring they are loaded as soon as possible.
import Head from 'next/head';

export default function HomePage() {
  return (
    <>
      <Head>
        {/* Preload important images */}
        <link rel="preload" href="/path/to/important-image1.jpg" as="image" />
        <link rel="preload" href="/path/to/important-image2.jpg" as="image" />
        {/* More images can be preloaded the same way */}
      </Head>
      {/* Your page content goes here */}
    </>
  );
}
This code imports the 'Head' component from Next.js and uses it to include 'link' elements within the 'head' section of the HTML document, specifically to preload important images. The 'href' attribute specifies the URL of the image that needs to be preloaded, while the 'as' attribute is set to 'image' to indicate the type of content being preloaded. This ensures that these images are loaded with a higher priority, improving visual readiness of images used in the initial viewport.