Next.js Image priority preloading when the image lives inside a next/dynamic component
0 reputation · 21 Jun 2022, 19:39 UTC
0 reputation · 21 Jun 2022, 19:39 UTC
I want a critical above-the-fold image rendered by the Next.js Image component to be preloaded immediately on page load, using the documented priority prop, even though the component that contains it is code-split via next/dynamic (or React.lazy).
The priority prop is documented to make the browser preload the image right away, which matters for LCP and CLS. However, when the Image element only exists inside a lazily loaded component, that component is not mounted until its chunk loads or it becomes visible. It is unclear whether the preload hint is emitted before the lazy component is instantiated, or whether preloading silently becomes a no-op until mount. The documentation does not define this ordering, and the behavior may differ between browsers and across Next.js 13/14 and later releases.
Assume the App Router on a current Next.js release, with the image served through the built-in optimization pipeline.
priority on an Image inside a next/dynamic component trigger a preload before that component mounts, or only after?34775 reputation · 22 Jun 2022, 05:08 UTC
The priority prop on next/image only adds a <link rel="preload"> tag when the Image component is rendered. If the Image lives inside a component loaded with next/dynamic (or React.lazy), the hint is inserted after the dynamic chunk has been fetched and the component mounts, not before. Therefore the image download starts only after the lazy JavaScript executes.
Because the Image component is not present in the initial HTML, the browser cannot receive the preload hint until the lazy component’s JavaScript runs, which delays LCP for above‑the‑fold images.
next/image would produce for the image (same width, quality, format). You can obtain it by:getImageProps from next/image on the server (e.g., in getServerSideProps or a server component) to extract the src value.<link rel="preload" as="image" href="{optimizedUrl}"> tag in the page’s <Head> (via next/head) or a custom Head component that runs on every render.next/dynamic wrapper with priority (optional; it will not hurt).<link rel="preload"> (shown as “Preload” in the Initiator column).priority prop did not preload before mount. If it appears before or at the same time as the chunk, the preload happened earlier (which would only be the case with a manual page‑level preload).?v=test) to the optimized URL to bust any server‑side caching and confirm the observed request matches the hint you injected.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.