react: Bug: Random preloads added for images

React version: 18.3.0-canary-c29ca23af-20231205

Steps To Reproduce

  1. Render SSR app that has images in its tree using renderToString or renderToPipeableStream

Link to code example:

The current behavior

image

The expected behavior

To not insert preloads randomly.

Note that there is difference between renderToString and renderToPipeableStream, renderToString the preloads appear inside root and renderToPipeableStream they appear in head.

Sorry this is a bit of a question rather than bug report maybe, I have searched around and cannot see any mention of auto-preloading images but I assume there must be something going on that I have missed.

I was able to fix by using 18.3.0-canary-493f72b0a-20230727.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 15 (1 by maintainers)

Commits related to this issue

Most upvoted comments

I agree with @jamesgpearce,

I also encountered this issue while testing the parsing of a simple MDX content that only contains an image syntax.

I saw <link rel="preload" as="image" href="image.png"/> in the result . Then, while digging where does it come from, then I dropped here.

I just used a raw renderToStaticMarkup in react-dom@19, react-dom/server in my test. I confirm react-dom@18 doesn’t have this behavior.

Now, I want to know how to disable it using an option.

This is very surprising behavior. Worse that it’s not documented nor optional.

Especially since if you have already explicitly added the preload link tags to the head yourself and then boom… now you have two of them in the DOM.

No framework; just a raw renderToStaticMarkup in React 19 will do this.

Thanks for the info regarding loading=lazy, that could solve it. 🙏

If this is really a “feature”, I have to say, I don’t think it should be React’s business to put stuff into my <head> automagically…

I think this is actually now expected behaviour, after digging the source a bit I found https://github.com/facebook/react/commit/f359f9b41ac1a8127f5ba505e0c04675eee0d310

Though I could not find any docs/release notes that mention this which is a little odd as now you need to explicitly think about the loading strategy of every img tag you use. For me I was able to “fix” this by making sure all our img tags have either loading=lazy or fetchPriority=low when they are not to be preloaded, this gave me more or less only the LCP images preloaded.

@nickluger For your case you could either add loading=lazy or fetchPriority=low to the fallback images, though depending on your target browsers you could get rid of the noscript parts these days as loading=lazy has pretty wide browser support now.

Would be good to have this documented somewhere and also maybe add this to the react linter.