Skip to content

Conversation

mimosapudical
Copy link

@mimosapudical mimosapudical commented Aug 16, 2025

Fix for React 19 renderToString() preload injection bug (#34219)

Changes

  • Added disableAutoPreload flag to createRenderState
  • Attached renderState to request object
  • Modified flushCompletedQueues to check flag before injecting preload links

Summary

This PR fixes the unexpected behavior in React 19 where renderToString(<img />) automatically injects <link rel="preload"> for every image. React 18 did not do this. By adding a disableAutoPreload flag in createRenderState and checking it in flushCompletedQueues, developers can now opt out of automatic preload injection when needed, restoring the expected SSR output.

How did you test this change?

  • Created a minimal test case with renderToString(<img src="my/image.png" />)
  • Verified that with disableAutoPreload = true, the output is:
<img src="my/image.png"/>

Copy link

meta-cla bot commented Aug 16, 2025

Hi @mimosapudical!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@meta-cla meta-cla bot added the CLA Signed label Aug 16, 2025
Copy link

meta-cla bot commented Aug 16, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Copy link
Collaborator

@sebmarkbage sebmarkbage left a comment

Choose a reason for hiding this comment

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

I'm not sure if we want to do this at all given that this is a feature of suspensey images. The way to opt out of it is to either make the image loading="lazy" or add an onLoad event listener.

But regardless, the fix is in the wrong place since it's way too broad and violates the contract by reaching into the type that varies by context. It would go into an override in:

https://github.com/facebook/react/blob/main/packages/react-dom-bindings/src/server/ReactFizzConfigDOMLegacy.js

The more interesting one might be the markup renderer since that's not expected to be rendered in the same way it might not need a preload in the same way. E.g. you don't preload from e-mails. That's also likely to be the replacement when renderToString gets deprecated anyway.

@alexanderson1993
Copy link

I'm not sure if we want to do this at all given that this is a feature of suspensey images. The way to opt out of it is to either make the image loading="lazy" or add an onLoad event listener.

This behavior doesn't seem to be documented on react.dev. It seems like this would be a reasonable section for such a doc to go: https://react.dev/reference/react-dom/components

@mimosapudical
Copy link
Author

mimosapudical commented Aug 20, 2025

I'm not sure if we want to do this at all given that this is a feature of suspensey images. The way to opt out of it is to either make the image loading="lazy" or add an onLoad event listener.

But regardless, the fix is in the wrong place since it's way too broad and violates the contract by reaching into the type that varies by context. It would go into an override in:

https://github.com/facebook/react/blob/main/packages/react-dom-bindings/src/server/ReactFizzConfigDOMLegacy.js

The more interesting one might be the markup renderer since that's not expected to be rendered in the same way it might not need a preload in the same way. E.g. you don't preload from e-mails. That's also likely to be the replacement when renderToString gets deprecated anyway.

The attempted fix adds an empty preloadInstance override in ReactFizzConfigDOMLegacy.js, but it does not pass.

Testing: On Windows, the Jest test only passes if the else if block in pushImg within ReactFizzConfigDOM.js is removed, showing that renderToString doesn’t fully pick up the ...DOMLegacy.js config.

@kurtextrem
Copy link

@sebmarkbage I appreciate that React tries to be smart here, but adding preloads by default is maybe a bit too smart. A sensible suggestion seems to this one: #34217 (comment). As an explicit loading="eager" is likely a great developer intent something is important (and thus might be smart to be preloaded). Preloading everything that has no explicit intent seems an easy pitfall for performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants