-
Notifications
You must be signed in to change notification settings - Fork 49.2k
Fix auto preload <img> issue for renderToString #34219
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
base: main
Are you sure you want to change the base?
Conversation
Hi @mimosapudical! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
There was a problem hiding this 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:
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.
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 |
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. |
@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 |
Fix for React 19 renderToString() preload injection bug (#34219)
Changes
disableAutoPreload
flag tocreateRenderState
renderState
torequest
objectflushCompletedQueues
to check flag before injecting preload linksSummary
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 adisableAutoPreload
flag increateRenderState
and checking it influshCompletedQueues
, developers can now opt out of automatic preload injection when needed, restoring the expected SSR output.How did you test this change?
renderToString(<img src="my/image.png" />)
disableAutoPreload = true
, the output is: