Skip to content

Demo Site: Load mobile image in BasicStageBlock with priority#3743

Closed
stekalt wants to merge 1 commit intomainfrom
load-stage-image-with-priority
Closed

Demo Site: Load mobile image in BasicStageBlock with priority#3743
stekalt wants to merge 1 commit intomainfrom
load-stage-image-with-priority

Conversation

@stekalt
Copy link
Copy Markdown
Contributor

@stekalt stekalt commented Mar 31, 2025

Description

The image of the BasicStageBlock displayed at the mobile breakpoint is loaded with priority. This allows Next.js to preload the image and therefore improve the Largest Contentful Paint (LCP) metric.

Acceptance criteria

Screenshots/screencasts

Before After
https://github.com/user-attachments/assets/8c41a19a-3e81-4c56-8c5a-18d5a49354b5 https://github.com/user-attachments/assets/493f26f9-c5de-4cd5-9d7c-7256e89cc4a0

Further information

@johnnyomair johnnyomair added the needs-starter-pr Change in Demo that needs to be changed in Starter as well. label Apr 1, 2025
@johnnyomair johnnyomair changed the title Load mobile image in BasicStageBlock with priority Demo Site: Load mobile image in BasicStageBlock with priority Apr 1, 2025
<Root>
<MediaPhone>
<MediaBlock data={media} aspectRatio="1x2" fill />
<MediaBlock data={media} aspectRatio="1x2" fill priority />
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only sets priority for MediaPhone breakpoint, and I'm assuming it does that for all device widths?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is: If you set priority for all four device widths, all four image versions are always loaded. This results in three unnecessary image loads (especially problematic on mobile).

I think the idea here is to just preload the mobile version to be fast on mobile devices. And on desktop it's usually no problem to also load the mobile image.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(But it's kind of confusing, you are right)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(But it's kind of confusing, you are right)

that is not my problem at all

we could do better:
preload link supports media queries. Does react-dom preload work for us?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And on desktop it's usually no problem to also load the mobile image.

I'm not sure about this. This will show up on all SEO reports.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preload link supports media queries. Does react-dom preload work for us?

I just had a look at the next/image priority implementation and I believe it's possible, since it's already using react-dom preload. We'd need to specify priority and sizes for all viewports, but then it should work.

Copy link
Copy Markdown
Member

@thomasdax98 thomasdax98 Apr 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about the media attribute in link. That's cool and would solve our problem 👍

Unfortunately, react preload only supports media starting in v19 (facebook/react#28635 (comment))

To date, Next doesn't support media at all (not even in v15 as it looks). So we can't use it.


Regarding a solution with only srcset and sizes: I don't think this always works.

In our example, the stage image always takes up 100vw. Meaning, specifying sizes as 100vw for all breakpoints is correct.

I'm not an expert in defining responsive images and didn't experiment, but according to ChatGPT, Desktop and Mobile can have an overlap in the image sizes they request because the device pixel ratio can be > 1

  • "Mobile" ≠ Small physical size
    A modern phone can have a viewport width of 430px but a devicePixelRatio of 3, so it needs a 1290px-wide image!

  • "Desktop" ≠ Large image area
    If your desktop layout only gives an image 400px of screen space (e.g. in a sidebar), it’ll download an image similar in size to what mobile might use.

✅ So YES: Mobile and desktop can easily request the same image size, depending on:

  • How big the image appears in your CSS layout
  • The device’s pixel density (Retina vs non-Retina)

So only defining srcset and using small sizes for the mobile images and big sizes for Desktop won't work correctly, to my understanding.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can build our own solution that adds a link with the correct media. But I didn't look into that

@manuelblum manuelblum removed their request for review May 5, 2025 08:00
@thomasdax98
Copy link
Copy Markdown
Member

We close this because it's a half-solution. Instead we will fix priority loading for stages in v9 when we update to React 19 so we can use preload as detailed in #3743 (comment)

@thomasdax98 thomasdax98 closed this May 7, 2025
@johnnyomair johnnyomair deleted the load-stage-image-with-priority branch May 7, 2025 07:42
@stekalt stekalt removed the needs-starter-pr Change in Demo that needs to be changed in Starter as well. label Sep 22, 2025
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.

5 participants