Skip to content

Why react-dom/server will automatic reorder elements in head #27879

Closed as not planned

Description

@JiangWeixian

React version: 18.3.0-canary-0cdfef19b-20231211

Steps To Reproduce

I try to reorder elements in <head>, but I found react always force push some element at the top. e.g. reverse elements in head, the <link rel="dns-prefetch"> always at the top. Just like the demo code.

The reason why I want to reorder elements is https://github.com/rviscomi/capo.js/blob/main/src/lib/rules.js found

How you order elements in the can have an effect on the (perceived) performance of the page.
This script helps you identify which elements are out of order.

So I want to reorder elements according to this repo

export const ElementWeights = {
  META: 10,
  TITLE: 9,
  PRECONNECT: 8,
  ASYNC_SCRIPT: 7,
  IMPORT_STYLES: 6,
  SYNC_SCRIPT: 5,
  SYNC_STYLES: 4,
  PRELOAD: 3,
  DEFER_SCRIPT: 2,
  PREFETCH_PRERENDER: 1,
  OTHER: 0
};

and open an abtest on my project, but looks like react don't want to me resort the elements. I found some code in react source code:

image

There is any reason why react will reorder elements in during rendering? You can test via the follow code.

import React, { Children, Suspense } from 'react'
import http from 'http'
import { renderToPipeableStream } from 'react-dom/server'

const Header = (props) => {
  return (
    <head>
      {Children.toArray(props.children).reverse()}
    </head>
  )
}

const App = () => {
  return (
    <html>
      <Header>
        <title>header</title>
        <link rel="dns-prefetch" href="https://fonts.googleapis.com" />
        <meta charset="UTF-8" />
        <link rel="icon" type="image/svg+xml" href="/vite.svg" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      </Header>
      <body>
        <div id="root">
          hello
        </div>
      </body>
    </html>
  )
}

const app = http.createServer(async (req, res) => {
  console.log(req.url)
  const { pipe } = renderToPipeableStream(<App />, {
    onShellReady() {
      pipe(res)
    },
  })
})

app.listen(8080, () => {
  console.log('Server start at 8080')
})

app.on('error', (err) => {
  console.log(err);
})

process.on('uncaughtException', function (err) {
  console.log(err);
}); 

The current behavior

image

The expected behavior

<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta charset="UTF-8" />
<link rel="dns-prefetch" href="https://fonts.googleapis.com" />
<title>header</title>

Activity

  1. added
    Status: Unconfirmed
    on Jan 5, 2024
  2. github-actions commented on Apr 5, 2024

    @github-actions

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

  3. added
    Resolution: Stale
    on Apr 5, 2024
  4. Zaid-maker commented on Apr 7, 2024

    @Zaid-maker

    bump

  5. added and removed
    Status: Unconfirmed
    Resolution: Stale
    on Apr 8, 2024
  6. github-actions commented on Jul 7, 2024

    @github-actions

    This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

  7. added
    Resolution: Stale
    on Jul 7, 2024
  8. github-actions commented on Jul 14, 2024

    @github-actions

    Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

  9. JiangWeixian commented on Sep 29, 2024

    @JiangWeixian
    Author

    bump

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Component: DOMReact 19Resolution: Stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions