Skip to content

Add Suspensey Images behind a Flag #32819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 4, 2025

Conversation

sebmarkbage
Copy link
Collaborator

We've known we've wanted this for many years and most of the implementation was already done for Suspensey CSS. This waits to commit until images have decoded by default or up to 500ms timeout (same as suspensey fonts).

It only applies to Transitions, Retries (Suspense), Gesture Transitions (flag) and Idle (doesn't exist). Sync updates just commit immediately.

<img loading="lazy" src="..." /> opts out since you explicitly want it to load lazily in that case.

<img onLoad={...} src="..." /> also opts out since that implies you're ok with managing your own reveal.

In the future, we may add an opt in e.g. <img blocking="render" src="..." /> that opts into longer timeouts and re-suspends even sync updates. Perhaps also triggering error boundaries on errors.

The rollout for this would have to go in a major and we may have to relax the default timeout to not delay too much by default. However, we can also make this part of enableViewTransition so that if you opt-in by using View Transitions then those animations will suspend on images. That we could ship in a minor.

Since we no longer diff all props in the render phase we might still get
here when nothing has changed. So this adds a small optimization to avoid
resuspending on every update.
Timeout at 500ms which is the same timeout we use for fonts in transitions
but maybe we should wait less by default.
…nd Idle

Meaning the blocking ones and Offscreen doesn't suspend on the images.

We should allow an opt-in to suspend blocking too in the future.

Resources still suspend blocking.
@react-sizebot
Copy link

react-sizebot commented Apr 4, 2025

Comparing: 540cd65...21eed10

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.68 kB 6.68 kB = 1.83 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js +0.11% 515.56 kB 516.12 kB +0.03% 91.84 kB 91.87 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB = 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js +0.52% 616.30 kB 619.49 kB +0.44% 109.03 kB 109.51 kB
facebook-www/ReactDOM-prod.classic.js +0.09% 650.16 kB 650.72 kB +0.04% 114.89 kB 114.93 kB
facebook-www/ReactDOM-prod.modern.js +0.09% 640.44 kB 641.00 kB +0.04% 113.33 kB 113.38 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer.production.js +0.98% 38.02 kB 38.39 kB +0.75% 7.03 kB 7.09 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer.production.js +0.98% 38.05 kB 38.42 kB +0.69% 7.06 kB 7.11 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer.production.js +0.98% 38.05 kB 38.42 kB +0.69% 7.07 kB 7.12 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-persistent.production.js +0.98% 38.15 kB 38.52 kB +0.75% 7.05 kB 7.11 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-persistent.production.js +0.97% 38.17 kB 38.55 kB +0.69% 7.08 kB 7.13 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-persistent.production.js +0.97% 38.18 kB 38.55 kB +0.69% 7.09 kB 7.14 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer.development.js +0.93% 42.31 kB 42.70 kB +0.71% 7.64 kB 7.69 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer.development.js +0.93% 42.33 kB 42.73 kB +0.67% 7.67 kB 7.72 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer.development.js +0.93% 42.34 kB 42.73 kB +0.66% 7.67 kB 7.72 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-persistent.development.js +0.92% 42.45 kB 42.84 kB +0.72% 7.66 kB 7.71 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-persistent.development.js +0.92% 42.47 kB 42.87 kB +0.66% 7.69 kB 7.74 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-persistent.development.js +0.92% 42.48 kB 42.87 kB +0.66% 7.69 kB 7.74 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js +0.52% 616.30 kB 619.49 kB +0.44% 109.03 kB 109.51 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.production.js +0.51% 630.71 kB 633.90 kB +0.45% 112.59 kB 113.09 kB
oss-experimental/react-dom/cjs/react-dom-profiling.profiling.js +0.47% 681.00 kB 684.20 kB +0.39% 118.83 kB 119.29 kB
oss-experimental/react-dom/cjs/react-dom-client.development.js +0.32% 1,117.69 kB 1,121.32 kB +0.25% 186.39 kB 186.85 kB
oss-experimental/react-dom/cjs/react-dom-profiling.development.js +0.32% 1,134.09 kB 1,137.71 kB +0.25% 189.22 kB 189.68 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.development.js +0.32% 1,134.24 kB 1,137.86 kB +0.25% 190.06 kB 190.54 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler.production.js +0.23% 391.19 kB 392.07 kB +0.37% 63.54 kB 63.77 kB
oss-stable/react-reconciler/cjs/react-reconciler.production.js +0.23% 391.21 kB 392.09 kB +0.37% 63.56 kB 63.79 kB

Generated by 🚫 dangerJS against 21eed10

Copy link
Collaborator

@acdlite acdlite left a comment

Choose a reason for hiding this comment

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

LGTM! Very easy to review, exciting when features like this land in such relatively small diffs

Since we have tests for this in noop.
// We indicate that all images are not yet loaded and if they're able
// to hit cache we let the decode() do that. Even if we did maintain
// our own cache to know this, it's not a guarantee that the browser
// keeps it in decoded memory.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The consequence of this is that we end up showing fallbacks for a Suspense boundary in a Transition even if we've preloaded it before revealing it (which its is throttled in its reveal).

We might want to add a heuristic here in case it is synchronously available to let it commit anyway. Even though that can lead to flashes due to images not being decoded.

The Suspense optimization could help here because we could yield and see if we can eagerly solve it in a micro-task however that could force decoding a bit too early which gives opportunity for the decoding cache to get dropped before commit.

I think instead, what might be ideal is to detect if an image is already loaded and if so, only mark the commit as might suspend but not suspend the Suspense boundary. I think I'll do that.

…mplete

We will still visit this in the commit phase to decode() it in case it's not
decoded already.
@sebmarkbage sebmarkbage merged commit efb22d8 into facebook:main Apr 4, 2025
241 checks passed
github-actions bot pushed a commit that referenced this pull request Apr 4, 2025
We've known we've wanted this for many years and most of the
implementation was already done for Suspensey CSS. This waits to commit
until images have decoded by default or up to 500ms timeout (same as
suspensey fonts).

It only applies to Transitions, Retries (Suspense), Gesture Transitions
(flag) and Idle (doesn't exist). Sync updates just commit immediately.

`<img loading="lazy" src="..." />` opts out since you explicitly want it
to load lazily in that case.

`<img onLoad={...} src="..." />` also opts out since that implies you're
ok with managing your own reveal.

In the future, we may add an opt in e.g. `<img blocking="render"
src="..." />` that opts into longer timeouts and re-suspends even sync
updates. Perhaps also triggering error boundaries on errors.

The rollout for this would have to go in a major and we may have to
relax the default timeout to not delay too much by default. However, we
can also make this part of `enableViewTransition` so that if you opt-in
by using View Transitions then those animations will suspend on images.
That we could ship in a minor.

DiffTrain build for [efb22d8](efb22d8)
github-actions bot pushed a commit that referenced this pull request Apr 4, 2025
We've known we've wanted this for many years and most of the
implementation was already done for Suspensey CSS. This waits to commit
until images have decoded by default or up to 500ms timeout (same as
suspensey fonts).

It only applies to Transitions, Retries (Suspense), Gesture Transitions
(flag) and Idle (doesn't exist). Sync updates just commit immediately.

`<img loading="lazy" src="..." />` opts out since you explicitly want it
to load lazily in that case.

`<img onLoad={...} src="..." />` also opts out since that implies you're
ok with managing your own reveal.

In the future, we may add an opt in e.g. `<img blocking="render"
src="..." />` that opts into longer timeouts and re-suspends even sync
updates. Perhaps also triggering error boundaries on errors.

The rollout for this would have to go in a major and we may have to
relax the default timeout to not delay too much by default. However, we
can also make this part of `enableViewTransition` so that if you opt-in
by using View Transitions then those animations will suspend on images.
That we could ship in a minor.

DiffTrain build for [efb22d8](efb22d8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
4 participants