Skip to content
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

[React 19] Different behaviors with preload method #30056

Open
ahce opened this issue Jun 23, 2024 · 2 comments
Open

[React 19] Different behaviors with preload method #30056

ahce opened this issue Jun 23, 2024 · 2 comments

Comments

@ahce
Copy link

ahce commented Jun 23, 2024

Summary

  • react & react-dom version: 19.0.0-rc-3563387fe3-20240621
  • node version: 20.14.0

When using react's preload method, the preload link tag is not created in certain cases.
It works correctly with two link tags (OnlyLinkTags component).

Code example

const fs = require('node:fs');
const { createElement } = require('react');
const { preload, preinit } = require('react-dom');
const { renderToString } = require('react-dom/server');

const CSS = '/some.css';
const LINK_PROPS = {
  rel: 'stylesheet',
  href: CSS,
};

/**
 * Doesn't create the preload link tag
 */
const PreloadAndPreinit = () => {
  preload(CSS, { as: 'style' });
  preinit(CSS, { as: 'style' });

  return null;
};

/**
 * Doesn't create the preload link tag
 */
const PreloadAndLinkWithPrecedence = () => {
  preload(CSS, { as: 'style' });

  return createElement('link', { ...LINK_PROPS, precedence: 'custom' });
};

/**
 * Creates the preload link tag correctly into the head
 * The stylesheet link tag stays in the body, because it doesn't have precedence
 */
const PreloadAndLinkWithoutPrecedence = () => {
  preload(CSS, { as: 'style' });

  return createElement('link', LINK_PROPS);
};

/**
 * Moves both tags into the head
 */
const OnlyLinkTags = () => [
  createElement('link', {
    key: 'preload',
    rel: 'preload',
    as: 'style',
    href: CSS,
  }),
  createElement('link', { ...LINK_PROPS, key: 'link', precedence: 'custom' }),
];

[
  PreloadAndPreinit,
  PreloadAndLinkWithPrecedence,
  PreloadAndLinkWithoutPrecedence,
  OnlyLinkTags,
].forEach((Component) => {
  fs.writeFileSync(
    `_${Component.name}.html`,
    renderToString(
      createElement(
        'html',
        null,
        createElement('head'),
        createElement('body', null, createElement(Component)),
      ),
    ),
  );
});

Outputs:

PreloadAndPreinit:

Current:

<html>
  <head>
    <link rel="stylesheet" href="/some.css" data-precedence="default" />
  </head>
  <body></body>
</html>

Expected:

<html>
  <head>
    <link rel="stylesheet" href="/some.css" data-precedence="default" />
    <link rel="preload" as="style" href="/some.css" />
  </head>
  <body></body>
</html>

PreloadAndLinkWithPrecedence:

Current:

<html>
  <head>
    <link rel="stylesheet" href="/some.css" data-precedence="custom" />
  </head>
  <body></body>
</html>

Expected:

<html>
  <head>
    <link rel="stylesheet" href="/some.css" data-precedence="default" />
    <link rel="preload" as="style" href="/some.css" />
  </head>
  <body></body>
</html>

PreloadAndLinkWithoutPrecedence:

Current:

<html>
  <head>
    <link rel="preload" href="/some.css" as="style" />
  </head>
  <body>
    <link rel="stylesheet" href="/some.css" />
  </body>
</html>

Expected:

The same as current.

OnlyLinkTags:

Current:

<html>
  <head>
    <link rel="stylesheet" href="/some.css" data-precedence="custom" />
    <link rel="preload" as="style" href="/some.css" />
  </head>
  <body></body>
</html>

Expected:

The same as current.

@ahce ahce added the React 19 label Jun 23, 2024
@eps1lon
Copy link
Collaborator

eps1lon commented Jun 25, 2024

Thank you for the details. Could you include the expected behavior for each case? It's not clear at a glance which cases work as expected and which don't.

@ahce
Copy link
Author

ahce commented Jun 25, 2024

Thank you for the details. Could you include the expected behavior for each case? It's not clear at a glance which cases work as expected and which don't.

Added expected behavior! 😃

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

No branches or pull requests

2 participants