10

We are using NextJS 9.3.2 Framework with Static Site Generator i.e SSG with the introduction of Google Lighthouse 6 Largest Contentful paint (LCP)is affecting my Site performance score . Most of sites have a Hero Image in above the fold content.

SO i am looking to Preload the image to cut down the time of LCP. Can you guys guide how can i preload big Hero image in NextJS with SSG.

6
  • 3
    You may check out next-optimized-images and lqip-loader. In case you want to have more control over the build process, I wrote this article Create a Lazy-Loading Image Component with React Hooks which I briefly shared how you may create your own LQIPs(Low-Quality Image Placeholder) with the help of sharp in Next's getStaticProps and pass to the page component via props. Commented Jul 12, 2020 at 15:52
  • Thanks for reply but our client didn't agree to Low-Quality Image image to placed as a placeholder until main image gets loaded we need to prerender the main hero image to cut down LCP Commented Jul 13, 2020 at 4:44
  • Prerender the main hero means to turn it into inline base64? Commented Jul 13, 2020 at 5:27
  • You mean to say that use sharp to generate webp image format during build time as Site is SSG and then inline image by using base64 image. Please correct me if i am wrong. Commented Jul 13, 2020 at 5:58
  • I was just asking what you do mean by "prerendering the main hero image"? Commented Jul 13, 2020 at 6:26

4 Answers 4

9

As HawaiiFi suggests, you can preload the image.

In Next.js you can achieve this by adding a <link rel="preload" ...> to next.js 's<Head> component.

import Head from "next/head";

// Inside one of the components that loads on your page:
<Head>
  <link
    rel="preload"
    href="/path/to/image.ext"
    as="image"
  />
</Head>
Sign up to request clarification or add additional context in comments.

1 Comment

It seems like this method only works for the pages router, is there a method with the same functionality for the app router ?
5

You should upgrade your Next.js and use Image Component. It will do the following great things -

  1. Lazy load
  2. new Webp format
  3. Resize image on the fly based on the device sizes
  4. Compress images
  5. Set prirotiy true in props to preload above the fold images.

Implement it to see the magnificent rise in lighthouse score.

Though it has some limitations like static export and placeholder images is not currently available, it still is great to use. For placeholder images, you can use some extra library like https://github.com/joe-bell/plaiceholder

6 Comments

The lack of SSG-support / next export makes it unusable in this scenario
Unfortunately there’s a poor cache policy that is unable to change yet: github.com/vercel/next.js/issues/19914, so for quite a while you might wanna stick to plain <img >
To clarify, only the default loader is not supported when using next export- using a cloud provider or a custom loader still works. The image optimization caching has also been improved since then, and should no longer be a deterrent to not use next/image.
@juliomalves Do you use a custom loader that build\creates image sizes\webp on export time? twitter.com/rauchg/status/1402613436081528836
next/image build-time optimization is not yet supported.
|
1

In the component that includes the hero image that needs to be preloaded, use next/head and simply pre-load that image.

I wouldn't use next/image. It's slower than other solutions to server image sizes dynamically.

That's another thing. You should load images based on the browser size using built-in HTML functionality.

1 Comment

How do you preload using head? I'm using next export. Thanks.
0

You need to use priority attribute for that image so Next.js will automatically preload it.

 <Image
   src="/image.png"
   alt="hero"
   priority
 />

Explanation from the Next.js documentation:

priority={false} // {false} | {true}

When true, the image will be considered high priority and preload. Lazy loading is automatically disabled for images using priority.

You should use the priority property on any image detected as the Largest Contentful Paint (LCP) element. It may be appropriate to have multiple priority images, as different images may be the LCP element for different viewport sizes.

Should only be used when the image is visible above the fold. Defaults to false.

https://nextjs.org/docs/pages/api-reference/components/image#priority

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.