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: 47664de...ae59661

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.60% 530.67 kB 533.85 kB +0.83% 93.49 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.52% 658.22 kB 661.64 kB +0.71% 115.79 kB 116.62 kB
facebook-www/ReactDOM-prod.classic.js +0.50% 682.38 kB 685.80 kB +0.72% 119.83 kB 120.69 kB
facebook-www/ReactDOM-prod.modern.js +0.51% 672.81 kB 676.23 kB +0.77% 118.14 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.60% 530.54 kB 533.73 kB +0.83% 93.46 kB 94.24 kB
oss-stable/react-dom/cjs/react-dom-client.production.js +0.60% 530.67 kB 533.85 kB +0.83% 93.49 kB 94.27 kB
oss-stable-semver/react-dom/cjs/react-dom-profiling.profiling.js +0.57% 564.15 kB 567.34 kB +0.79% 98.41 kB 99.18 kB
oss-stable/react-dom/cjs/react-dom-profiling.profiling.js +0.57% 564.28 kB 567.47 kB +0.79% 98.43 kB 99.21 kB
facebook-react-native/react-dom/cjs/ReactDOMClient-prod.js +0.54% 586.67 kB 589.85 kB +0.77% 103.05 kB 103.84 kB
facebook-react-native/react-dom/cjs/ReactDOMProfiling-prod.js +0.54% 592.17 kB 595.35 kB +0.75% 104.14 kB 104.92 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js +0.52% 658.22 kB 661.64 kB +0.71% 115.79 kB 116.62 kB
facebook-www/ReactDOM-prod.modern.js +0.51% 672.81 kB 676.23 kB +0.77% 118.14 kB 119.04 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.production.js +0.51% 672.63 kB 676.05 kB +0.68% 119.37 kB 120.18 kB
facebook-www/ReactDOM-prod.classic.js +0.50% 682.38 kB 685.80 kB +0.72% 119.83 kB 120.69 kB
facebook-www/ReactDOMTesting-prod.modern.js +0.50% 687.21 kB 690.63 kB +0.71% 121.78 kB 122.64 kB
facebook-www/ReactDOMTesting-prod.classic.js +0.49% 696.78 kB 700.20 kB +0.71% 123.41 kB 124.28 kB
facebook-react-native/react-dom/cjs/ReactDOMClient-profiling.js +0.48% 658.60 kB 661.77 kB +0.68% 112.72 kB 113.49 kB
facebook-react-native/react-dom/cjs/ReactDOMProfiling-profiling.js +0.48% 664.53 kB 667.70 kB +0.69% 113.86 kB 114.65 kB
oss-experimental/react-dom/cjs/react-dom-profiling.profiling.js +0.47% 723.82 kB 727.23 kB +0.66% 125.46 kB 126.29 kB
facebook-www/ReactDOM-profiling.modern.js +0.46% 747.45 kB 750.86 kB +0.67% 128.30 kB 129.16 kB
facebook-www/ReactDOM-profiling.classic.js +0.45% 755.56 kB 758.97 kB +0.67% 129.68 kB 130.54 kB
oss-stable-semver/react-dom/cjs/react-dom-client.development.js +0.39% 981.35 kB 985.19 kB +0.50% 164.68 kB 165.52 kB
oss-stable/react-dom/cjs/react-dom-client.development.js +0.39% 981.47 kB 985.32 kB +0.50% 164.71 kB 165.54 kB
oss-stable-semver/react-dom/cjs/react-dom-profiling.development.js +0.39% 997.73 kB 1,001.57 kB +0.49% 167.50 kB 168.33 kB
oss-stable/react-dom/cjs/react-dom-profiling.development.js +0.39% 997.85 kB 1,001.70 kB +0.49% 167.53 kB 168.36 kB
oss-experimental/react-dom/cjs/react-dom-client.development.js +0.34% 1,209.69 kB 1,213.77 kB +0.43% 201.51 kB 202.38 kB
facebook-react-native/react-dom/cjs/ReactDOMClient-dev.js +0.34% 1,130.69 kB 1,134.49 kB +0.46% 186.64 kB 187.49 kB
oss-experimental/react-dom/cjs/react-dom-profiling.development.js +0.33% 1,226.07 kB 1,230.16 kB +0.42% 204.31 kB 205.18 kB
oss-experimental/react-dom/cjs/react-dom-unstable_testing.development.js +0.33% 1,226.24 kB 1,230.32 kB +0.43% 205.11 kB 205.99 kB
facebook-react-native/react-dom/cjs/ReactDOMProfiling-dev.js +0.33% 1,146.96 kB 1,150.76 kB +0.46% 189.41 kB 190.28 kB
facebook-www/ReactDOM-dev.modern.js +0.33% 1,249.73 kB 1,253.81 kB +0.44% 206.08 kB 206.98 kB
facebook-www/ReactDOM-dev.classic.js +0.32% 1,258.90 kB 1,262.98 kB +0.44% 207.80 kB 208.71 kB
facebook-www/ReactDOMTesting-dev.modern.js +0.32% 1,266.26 kB 1,270.35 kB +0.44% 209.78 kB 210.71 kB
facebook-www/ReactDOMTesting-dev.classic.js +0.32% 1,275.43 kB 1,279.52 kB +0.44% 211.54 kB 212.47 kB

Generated by 🚫 dangerJS against ae59661

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.

…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.
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.

2 participants