Skip to content

Conversation

sebmarkbage
Copy link
Collaborator

Stacked on #34478.

In general we don't like to deal with timeouts in suspense world. We've had that in the past but in general it doesn't work well because if you have a timeout and then give up you made everything wait longer for no benefit at the end. That's why the recommendation is to remove a Suspense boundary if you expect it to be fast and add one if you expect it to be slow. You have to estimate as the developer.

Suspensey images suffer from this same problem. We want to apply suspensey images to as much as possible so that it's the default to avoid flashing because if just a few images flash it's still almost as bad as all of them. However, we do know that it's also very common to use images and on a slow connection or many images, it's not worth it so we have the timeout to eventually give up.

However, this means that in cases that are always slow or connections that are always slow, you're always punished for no reason.

Suspensey images is mainly a polish feature to make high end experiences on high end connections better but we don't want to unnecessarily punish all slow connections in the process or things like lots of images below the viewport.

This PR adds an estimate for whether or not we'll likely be able to load all the images within the timeout on a high end enough connection. If not, we'll still do a short suspend (unless we've already exceeded the wait time adjusted for #34478) to allow loading from cache if available.

This estimate is based on two heuristics:

  1. We compute an estimated bandwidth available on the current device in mbps. This is computed from performance entries that have loaded static resources already on the site. E.g. this can be other images, css, or scripts. We see how long they took. If we don't have any entries (or if they're all cross-origin in Safari) we fallback to navigator.connection.downlink in Chrome or a 5mbps default in Firefox/Safari.
  2. To estimate how many bytes we'll have to download we use the width/height props of the img tag if available (or a 100 pixel default) times the device pixel ratio. We assume that a good img implementation downloads proper resolution image for the device and defines a width/height up front to avoid layout trash. Then we estimate that it takes about 0.25 bytes per pixel which is somewhat conservative estimate.

This is somewhat conservative given that the image could've been preloaded and be better compressed.

So it really only kicks in for high end connections that are known to load fast.

In a follow up, we can add an additional wait for View Transitions that does the same estimate but only for the images that turn out to be in viewport.

@meta-cla meta-cla bot added the CLA Signed label Sep 13, 2025
@github-actions github-actions bot added the React Core Team Opened by a member of the React Core Team label Sep 13, 2025
@react-sizebot
Copy link

react-sizebot commented Sep 13, 2025

Comparing: e3f1918...abcb8d1

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.41% 531.68 kB 533.85 kB +0.61% 93.69 kB 94.27 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.36% 659.27 kB 661.64 kB +0.54% 115.99 kB 116.62 kB
facebook-www/ReactDOM-prod.classic.js +0.35% 683.42 kB 685.80 kB +0.57% 120.00 kB 120.69 kB
facebook-www/ReactDOM-prod.modern.js +0.35% 673.84 kB 676.23 kB +0.59% 118.33 kB 119.04 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-dom/cjs/react-dom-client.production.js +0.41% 531.56 kB 533.73 kB +0.61% 93.67 kB 94.24 kB
oss-stable/react-dom/cjs/react-dom-client.production.js +0.41% 531.68 kB 533.85 kB +0.61% 93.69 kB 94.27 kB
oss-stable-semver/react-dom/cjs/react-dom-profiling.profiling.js +0.38% 565.17 kB 567.34 kB +0.59% 98.60 kB 99.18 kB
oss-stable/react-dom/cjs/react-dom-profiling.profiling.js +0.38% 565.30 kB 567.47 kB +0.59% 98.63 kB 99.21 kB
facebook-react-native/react-dom/cjs/ReactDOMClient-prod.js +0.37% 587.68 kB 589.85 kB +0.56% 103.26 kB 103.84 kB
facebook-react-native/react-dom/cjs/ReactDOMProfiling-prod.js +0.37% 593.18 kB 595.35 kB +0.56% 104.33 kB 104.92 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js +0.36% 659.27 kB 661.64 kB +0.54% 115.99 kB 116.62 kB
facebook-www/ReactDOM-prod.modern.js +0.35% 673.84 kB 676.23 kB +0.59% 118.33 kB 119.04 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.production.js +0.35% 673.68 kB 676.05 kB +0.52% 119.55 kB 120.18 kB
facebook-www/ReactDOM-prod.classic.js +0.35% 683.42 kB 685.80 kB +0.57% 120.00 kB 120.69 kB
facebook-www/ReactDOMTesting-prod.modern.js +0.35% 688.25 kB 690.63 kB +0.57% 121.95 kB 122.64 kB
facebook-www/ReactDOMTesting-prod.classic.js +0.34% 697.82 kB 700.20 kB +0.57% 123.57 kB 124.28 kB
facebook-react-native/react-dom/cjs/ReactDOMClient-profiling.js +0.33% 659.60 kB 661.77 kB +0.52% 112.90 kB 113.49 kB
oss-experimental/react-dom/cjs/react-dom-profiling.profiling.js +0.33% 724.85 kB 727.23 kB +0.52% 125.64 kB 126.29 kB
facebook-react-native/react-dom/cjs/ReactDOMProfiling-profiling.js +0.33% 665.53 kB 667.70 kB +0.53% 114.05 kB 114.65 kB
facebook-www/ReactDOM-profiling.modern.js +0.32% 748.48 kB 750.86 kB +0.54% 128.47 kB 129.16 kB
facebook-www/ReactDOM-profiling.classic.js +0.32% 756.58 kB 758.97 kB +0.53% 129.85 kB 130.54 kB
oss-stable-semver/react-dom/cjs/react-dom-client.development.js +0.27% 982.58 kB 985.19 kB +0.37% 164.91 kB 165.52 kB
oss-stable/react-dom/cjs/react-dom-client.development.js +0.27% 982.71 kB 985.32 kB +0.36% 164.94 kB 165.54 kB
oss-stable-semver/react-dom/cjs/react-dom-profiling.development.js +0.26% 998.96 kB 1,001.57 kB +0.36% 167.73 kB 168.33 kB
oss-stable/react-dom/cjs/react-dom-profiling.development.js +0.26% 999.09 kB 1,001.70 kB +0.36% 167.76 kB 168.36 kB
oss-experimental/react-dom/cjs/react-dom-client.development.js +0.23% 1,210.93 kB 1,213.77 kB +0.34% 201.70 kB 202.38 kB
oss-experimental/react-dom/cjs/react-dom-profiling.development.js +0.23% 1,227.32 kB 1,230.16 kB +0.33% 204.51 kB 205.18 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.development.js +0.23% 1,227.48 kB 1,230.32 kB +0.33% 205.32 kB 205.99 kB
facebook-react-native/react-dom/cjs/ReactDOMClient-dev.js +0.23% 1,131.88 kB 1,134.49 kB +0.33% 186.87 kB 187.49 kB
facebook-www/ReactDOM-dev.modern.js +0.23% 1,250.96 kB 1,253.81 kB +0.34% 206.28 kB 206.98 kB
facebook-react-native/react-dom/cjs/ReactDOMProfiling-dev.js +0.23% 1,148.15 kB 1,150.76 kB +0.32% 189.66 kB 190.28 kB
facebook-www/ReactDOM-dev.classic.js +0.23% 1,260.13 kB 1,262.98 kB +0.34% 208.00 kB 208.71 kB
facebook-www/ReactDOMTesting-dev.modern.js +0.22% 1,267.50 kB 1,270.35 kB +0.34% 209.99 kB 210.71 kB
facebook-www/ReactDOMTesting-dev.classic.js +0.22% 1,276.67 kB 1,279.52 kB +0.34% 211.75 kB 212.47 kB

Generated by 🚫 dangerJS against abcb8d1

export function waitForCommitToBeReady(): null | ((() => void) => () => void) {
const SUSPENSEY_STYLESHEET_TIMEOUT = 60000;

const SUSPENSEY_IMAGE_TIMEOUT = 800;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because we now estimate before the timeout, I extended the actual timeout so that if we get the estimate wrong it's worth waiting a bit longer to at least get some benefit from having waited a bit already.

if (state.imgBytes > 0 && estimatedBytesWithinLimit === 0) {
// Estimate how many bytes we can download in 500ms.
const mbps = estimateBandwidth();
estimatedBytesWithinLimit = mbps * 125 * SUSPENSEY_IMAGE_TIME_ESTIMATE;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why a 500ms constant vs the remaining time: imgTimeout + timeoutOffset

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because we have already started the download before we get here. The download could've started before the render even started or it could've started right before commit if the image is the last sibling we discovered and it didn't start downloading earlier. So if we say that it started sometime within the render, then the start time of when we started is really the same thing.

Basically the question isn't "If we started downloading now will we have time to download this image?", it's more like "Will we have time to download the images within the span we're willing to have waited in total?"

…ad all images within 500ms

Since we're more conservative about starting to wait now, we now wait longer
if we have started waiting so that we didn't wait for no good reason.
@sebmarkbage sebmarkbage merged commit ae22247 into facebook:main Sep 15, 2025
14 of 16 checks passed
sebmarkbage added a commit that referenced this pull request Sep 15, 2025
Stacked on #34481.

We currently track the suspended state temporarily with a global which
is safe as long as we always read it during a sync pass. However, we
sometimes read it in closures and then we have to be carefully to pass
the right one since it's possible another commit on a different root has
started at that point. This avoids this footgun.

Another reason to do this is that I want to read it in
`startViewTransition` which is in an async gap after which point it's no
longer safe. So I have to pass that through the `commitRoot` bound
function.
github-actions bot pushed a commit that referenced this pull request Sep 15, 2025
… them all in time anyway (#34481)

Stacked on #34478.

In general we don't like to deal with timeouts in suspense world. We've
had that in the past but in general it doesn't work well because if you
have a timeout and then give up you made everything wait longer for no
benefit at the end. That's why the recommendation is to remove a
Suspense boundary if you expect it to be fast and add one if you expect
it to be slow. You have to estimate as the developer.

Suspensey images suffer from this same problem. We want to apply
suspensey images to as much as possible so that it's the default to
avoid flashing because if just a few images flash it's still almost as
bad as all of them. However, we do know that it's also very common to
use images and on a slow connection or many images, it's not worth it so
we have the timeout to eventually give up.

However, this means that in cases that are always slow or connections
that are always slow, you're always punished for no reason.

Suspensey images is mainly a polish feature to make high end experiences
on high end connections better but we don't want to unnecessarily punish
all slow connections in the process or things like lots of images below
the viewport.

This PR adds an estimate for whether or not we'll likely be able to load
all the images within the timeout on a high end enough connection. If
not, we'll still do a short suspend (unless we've already exceeded the
wait time adjusted for #34478) to allow loading from cache if available.

This estimate is based on two heuristics:

1) We compute an estimated bandwidth available on the current device in
mbps. This is computed from performance entries that have loaded static
resources already on the site. E.g. this can be other images, css, or
scripts. We see how long they took. If we don't have any entries (or if
they're all cross-origin in Safari) we fallback to
`navigator.connection.downlink` in Chrome or a 5mbps default in
Firefox/Safari.
2) To estimate how many bytes we'll have to download we use the
width/height props of the img tag if available (or a 100 pixel default)
times the device pixel ratio. We assume that a good img implementation
downloads proper resolution image for the device and defines a
width/height up front to avoid layout trash. Then we estimate that it
takes about 0.25 bytes per pixel which is somewhat conservative
estimate.

This is somewhat conservative given that the image could've been
preloaded and be better compressed.

So it really only kicks in for high end connections that are known to
load fast.

In a follow up, we can add an additional wait for View Transitions that
does the same estimate but only for the images that turn out to be in
viewport.

DiffTrain build for [ae22247](ae22247)
github-actions bot pushed a commit that referenced this pull request Sep 15, 2025
… them all in time anyway (#34481)

Stacked on #34478.

In general we don't like to deal with timeouts in suspense world. We've
had that in the past but in general it doesn't work well because if you
have a timeout and then give up you made everything wait longer for no
benefit at the end. That's why the recommendation is to remove a
Suspense boundary if you expect it to be fast and add one if you expect
it to be slow. You have to estimate as the developer.

Suspensey images suffer from this same problem. We want to apply
suspensey images to as much as possible so that it's the default to
avoid flashing because if just a few images flash it's still almost as
bad as all of them. However, we do know that it's also very common to
use images and on a slow connection or many images, it's not worth it so
we have the timeout to eventually give up.

However, this means that in cases that are always slow or connections
that are always slow, you're always punished for no reason.

Suspensey images is mainly a polish feature to make high end experiences
on high end connections better but we don't want to unnecessarily punish
all slow connections in the process or things like lots of images below
the viewport.

This PR adds an estimate for whether or not we'll likely be able to load
all the images within the timeout on a high end enough connection. If
not, we'll still do a short suspend (unless we've already exceeded the
wait time adjusted for #34478) to allow loading from cache if available.

This estimate is based on two heuristics:

1) We compute an estimated bandwidth available on the current device in
mbps. This is computed from performance entries that have loaded static
resources already on the site. E.g. this can be other images, css, or
scripts. We see how long they took. If we don't have any entries (or if
they're all cross-origin in Safari) we fallback to
`navigator.connection.downlink` in Chrome or a 5mbps default in
Firefox/Safari.
2) To estimate how many bytes we'll have to download we use the
width/height props of the img tag if available (or a 100 pixel default)
times the device pixel ratio. We assume that a good img implementation
downloads proper resolution image for the device and defines a
width/height up front to avoid layout trash. Then we estimate that it
takes about 0.25 bytes per pixel which is somewhat conservative
estimate.

This is somewhat conservative given that the image could've been
preloaded and be better compressed.

So it really only kicks in for high end connections that are known to
load fast.

In a follow up, we can add an additional wait for View Transitions that
does the same estimate but only for the images that turn out to be in
viewport.

DiffTrain build for [ae22247](ae22247)
github-actions bot pushed a commit that referenced this pull request Sep 15, 2025
Stacked on #34481.

We currently track the suspended state temporarily with a global which
is safe as long as we always read it during a sync pass. However, we
sometimes read it in closures and then we have to be carefully to pass
the right one since it's possible another commit on a different root has
started at that point. This avoids this footgun.

Another reason to do this is that I want to read it in
`startViewTransition` which is in an async gap after which point it's no
longer safe. So I have to pass that through the `commitRoot` bound
function.

DiffTrain build for [5d49b2b](5d49b2b)
sebmarkbage added a commit that referenced this pull request Sep 15, 2025
…nimation (#34500)

Stacked on #34486.

If we gave up on loading suspensey images for blocking the commit (e.g.
due to #34481), we can still block the view transition from committing
to allow an animation to include the image from the start.

At this point we have more information about the layout so we can
include only the images that are within viewport in the calculation
which may end up with a different answer.

This only applies when we attempt to run an animation (e.g. something
mutated inside a `<ViewTransition>` in a Transition). We could attempt a
`startViewTransition` if we gave up on the suspensey images just so that
we could block it even if no animation would be running.

However, this point the screen is frozen and you can no longer have sync
updates interrupt so ideally we would have already blocked the commit
from happening in the first place.

The reason to have two points where we block is that ideally we leave
the UI responsive while blocking, which blocking the commit does. In the
simple case of all images or a single image being within the viewport,
that's favorable. By combining the techniques we only end up freezing
the screen in the special case that we had a lot of images added outside
the viewport and started an animation with some image inside the
viewport (which presumably is about to finish anyway).
github-actions bot pushed a commit that referenced this pull request Sep 15, 2025
…nimation (#34500)

Stacked on #34486.

If we gave up on loading suspensey images for blocking the commit (e.g.
due to #34481), we can still block the view transition from committing
to allow an animation to include the image from the start.

At this point we have more information about the layout so we can
include only the images that are within viewport in the calculation
which may end up with a different answer.

This only applies when we attempt to run an animation (e.g. something
mutated inside a `<ViewTransition>` in a Transition). We could attempt a
`startViewTransition` if we gave up on the suspensey images just so that
we could block it even if no animation would be running.

However, this point the screen is frozen and you can no longer have sync
updates interrupt so ideally we would have already blocked the commit
from happening in the first place.

The reason to have two points where we block is that ideally we leave
the UI responsive while blocking, which blocking the commit does. In the
simple case of all images or a single image being within the viewport,
that's favorable. By combining the techniques we only end up freezing
the screen in the special case that we had a lot of images added outside
the viewport and started an animation with some image inside the
viewport (which presumably is about to finish anyway).

DiffTrain build for [348a4e2](348a4e2)
github-actions bot pushed a commit that referenced this pull request Sep 15, 2025
…nimation (#34500)

Stacked on #34486.

If we gave up on loading suspensey images for blocking the commit (e.g.
due to #34481), we can still block the view transition from committing
to allow an animation to include the image from the start.

At this point we have more information about the layout so we can
include only the images that are within viewport in the calculation
which may end up with a different answer.

This only applies when we attempt to run an animation (e.g. something
mutated inside a `<ViewTransition>` in a Transition). We could attempt a
`startViewTransition` if we gave up on the suspensey images just so that
we could block it even if no animation would be running.

However, this point the screen is frozen and you can no longer have sync
updates interrupt so ideally we would have already blocked the commit
from happening in the first place.

The reason to have two points where we block is that ideally we leave
the UI responsive while blocking, which blocking the commit does. In the
simple case of all images or a single image being within the viewport,
that's favorable. By combining the techniques we only end up freezing
the screen in the special case that we had a lot of images added outside
the viewport and started an animation with some image inside the
viewport (which presumably is about to finish anyway).

DiffTrain build for [348a4e2](348a4e2)
github-actions bot pushed a commit to code/lib-react that referenced this pull request Sep 20, 2025
…nimation (facebook#34500)

Stacked on facebook#34486.

If we gave up on loading suspensey images for blocking the commit (e.g.
due to facebook#34481), we can still block the view transition from committing
to allow an animation to include the image from the start.

At this point we have more information about the layout so we can
include only the images that are within viewport in the calculation
which may end up with a different answer.

This only applies when we attempt to run an animation (e.g. something
mutated inside a `<ViewTransition>` in a Transition). We could attempt a
`startViewTransition` if we gave up on the suspensey images just so that
we could block it even if no animation would be running.

However, this point the screen is frozen and you can no longer have sync
updates interrupt so ideally we would have already blocked the commit
from happening in the first place.

The reason to have two points where we block is that ideally we leave
the UI responsive while blocking, which blocking the commit does. In the
simple case of all images or a single image being within the viewport,
that's favorable. By combining the techniques we only end up freezing
the screen in the special case that we had a lot of images added outside
the viewport and started an animation with some image inside the
viewport (which presumably is about to finish anyway).

DiffTrain build for [348a4e2](facebook@348a4e2)
github-actions bot pushed a commit to code/lib-react that referenced this pull request Sep 20, 2025
…nimation (facebook#34500)

Stacked on facebook#34486.

If we gave up on loading suspensey images for blocking the commit (e.g.
due to facebook#34481), we can still block the view transition from committing
to allow an animation to include the image from the start.

At this point we have more information about the layout so we can
include only the images that are within viewport in the calculation
which may end up with a different answer.

This only applies when we attempt to run an animation (e.g. something
mutated inside a `<ViewTransition>` in a Transition). We could attempt a
`startViewTransition` if we gave up on the suspensey images just so that
we could block it even if no animation would be running.

However, this point the screen is frozen and you can no longer have sync
updates interrupt so ideally we would have already blocked the commit
from happening in the first place.

The reason to have two points where we block is that ideally we leave
the UI responsive while blocking, which blocking the commit does. In the
simple case of all images or a single image being within the viewport,
that's favorable. By combining the techniques we only end up freezing
the screen in the special case that we had a lot of images added outside
the viewport and started an animation with some image inside the
viewport (which presumably is about to finish anyway).

DiffTrain build for [348a4e2](facebook@348a4e2)
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants