[React 19]预加载方法的不同行为

rmbxnbpk  于 8个月前  发布在  React
关注(0)|答案(2)|浏览(115)

摘要

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

当使用 react 的 preload 方法时,在某些情况下不会创建 preload link 标签。它与两个 link 标签(OnlyLinkTags 组件)一起工作正常。

代码示例

  1. const fs = require('node:fs');
  2. const { createElement } = require('react');
  3. const { preload, preinit } = require('react-dom');
  4. const { renderToString } = require('react-dom/server');
  5. const CSS = '/some.css';
  6. const LINK_PROPS = {
  7. rel: 'stylesheet',
  8. href: CSS,
  9. };
  10. /**
  11. * Doesn't create the preload link tag
  12. */
  13. const PreloadAndPreinit = () => {
  14. preload(CSS, { as: 'style' });
  15. preinit(CSS, { as: 'style' });
  16. return null;
  17. };
  18. /**
  19. * Doesn't create the preload link tag
  20. */
  21. const PreloadAndLinkWithPrecedence = () => {
  22. preload(CSS, { as: 'style' });
  23. return createElement('link', { ...LINK_PROPS, precedence: 'custom' });
  24. };
  25. /**
  26. * Creates the preload link tag correctly into the head
  27. * The stylesheet link tag stays in the body, because it doesn't have precedence
  28. */
  29. const PreloadAndLinkWithoutPrecedence = () => {
  30. preload(CSS, { as: 'style' });
  31. return createElement('link', LINK_PROPS);
  32. };
  33. /**
  34. * Moves both tags into the head
  35. */
  36. const OnlyLinkTags = () => [
  37. createElement('link', {
  38. key: 'preload',
  39. rel: 'preload',
  40. as: 'style',
  41. href: CSS,
  42. }),
  43. createElement('link', { ...LINK_PROPS, key: 'link', precedence: 'custom' }),
  44. ];
  45. [
  46. PreloadAndPreinit,
  47. PreloadAndLinkWithPrecedence,
  48. PreloadAndLinkWithoutPrecedence,
  49. OnlyLinkTags,
  50. ].forEach((Component) => {
  51. fs.writeFileSync(
  52. `_${Component.name}.html`,
  53. renderToString(
  54. createElement(
  55. 'html',
  56. null,
  57. createElement('head'),
  58. createElement('body', null, createElement(Component)),
  59. ),
  60. ),
  61. );
  62. });

输出:

PreloadAndPreinit:

当前:

  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="/some.css" data-precedence="default" />
  4. </head>
  5. <body></body>
  6. </html>

预期:

  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="/some.css" data-precedence="default" />
  4. <link rel="preload" as="style" href="/some.css" />
  5. </head>
  6. <body></body>
  7. </html>
PreloadAndLinkWithPrecedence:

当前:

  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="/some.css" data-precedence="custom" />
  4. </head>
  5. <body></body>
  6. </html>

预期:

  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="/some.css" data-precedence="default" />
  4. <link rel="preload" as="style" href="/some.css" />
  5. </head>
  6. <body></body>
  7. </html>
PreloadAndLinkWithoutPrecedence:

当前:

  1. <html>
  2. <head>
  3. <link rel="preload" href="/some.css" as="style" />
  4. </head>
  5. <body>
  6. <link rel="stylesheet" href="/some.css" />
  7. </body>
  8. </html>

预期:

The same as current.

OnlyLinkTags:

当前:

  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="/some.css" data-precedence="custom" />
  4. <link rel="preload" as="style" href="/some.css" />
  5. </head>
  6. <body></body>
  7. </html>

预期:

The same as current.

9w11ddsr

9w11ddsr1#

感谢您提供详细信息。您能否包括每个案例的预期行为?从表面上看,不清楚哪些案例按预期工作,哪些没有。

mrwjdhj3

mrwjdhj32#

感谢您提供的详细信息。您能包括每个案例的预期行为吗?从表面上看,不清楚哪些案例按预期工作,哪些没有。
已添加预期行为!😃

相关问题