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

[Fizz] implement onHeaders and headersLengthHint options #27641

Merged
merged 3 commits into from
Nov 7, 2023

Conversation

gnoff
Copy link
Collaborator

@gnoff gnoff commented Nov 3, 2023

Adds a new option to react-dom/server entrypoints.

onHeaders: (headers: Headers) => void (non node envs)
onHeaders: (headers: { Link?: string }) => void (node envs)

When any renderTo... or prerender... function is called and this option is provided the supplied function will be called sometime on or before completion of the render with some preload link headers.

When provided during a renderTo... the callback will usually be called after the first pass at work. The idea here is we want to get a set of headers to start the browser loading well before the shell is ready. We don't wait for the shell because if we did we may as well send the preloads as tags in the HTML.

When provided during a prerender... the callback will be called after the entire prerender is complete. The idea here is we are not responding to a live request and it is preferable to capture as much as possible for preloading as Headers in case the prerender was unable to finish the shell.

Currently the following resources are always preloaded as headers when the option is provided

  1. prefetchDNS and preconnects
  2. font preloads
  3. high priority image preloads

Additionally if we are providing headers when the shell is incomplete (regardless of whether it is render or prerender) we will also include any stylesheet Resources (ones with a precedence prop)

There is a second option maxHeadersLength?: number which allows you to specify the maximum length of the header content in unicode code units. This is what you get when you read the length property of a string in javascript. It's improtant to note that this is not the same as the utf-8 byte length when these headers are serialized in a Response. The utf8 representation may be the same size, or larger but it will never be smaller.

If you do not supply a maxHeadersLength we defaul to 2000. This was chosen as half the value of the max headers length supported by commonly known web servers and CDNs. many browser and web server can support significantly more headers than this so you can use this option to increase the headers limit. You can also of course use it to be even more conservative. Again it is important to keep in mind there is no direct translation between the max length and the bytelength and so if you want to stay under a certain byte length you need to be potentially more aggressive in the maxHeadersLength you choose.

Conceptually onHeaders could be called more than once as new headers are discovered however if we haven't started flushing yet but since most APIs for the server including the web standard Response only allow you to set headers once the current implementation will only call it one time

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Nov 3, 2023
@react-sizebot
Copy link

react-sizebot commented Nov 4, 2023

Comparing: c897260...4140b11

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 175.24 kB 175.24 kB = 54.53 kB 54.53 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 177.37 kB 177.37 kB = 55.19 kB 55.19 kB
facebook-www/ReactDOM-prod.classic.js = 567.70 kB 567.70 kB = 99.96 kB 99.96 kB
facebook-www/ReactDOM-prod.modern.js = 551.56 kB 551.56 kB = 97.06 kB 97.06 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.development.js +5.72% 390.85 kB 413.22 kB +6.54% 86.61 kB 92.27 kB
oss-stable/react-dom/cjs/react-dom-server.node.development.js +5.72% 390.88 kB 413.25 kB +6.53% 86.64 kB 92.29 kB
oss-experimental/react-dom/cjs/react-dom-server.node.development.js +5.49% 410.70 kB 433.27 kB +5.99% 90.27 kB 95.68 kB
oss-stable-semver/react-dom/cjs/react-dom-server.bun.development.js +5.49% 387.12 kB 408.39 kB +6.24% 85.83 kB 91.19 kB
oss-stable/react-dom/cjs/react-dom-server.bun.development.js +5.49% 387.14 kB 408.41 kB +6.24% 85.86 kB 91.22 kB
facebook-www/ReactDOMServer-dev.modern.js +5.49% 396.41 kB 418.18 kB +5.88% 87.74 kB 92.89 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.development.js +5.45% 393.37 kB 414.81 kB +6.06% 88.12 kB 93.46 kB
oss-stable/react-dom/cjs/react-dom-server.edge.development.js +5.45% 393.40 kB 414.84 kB +6.05% 88.15 kB 93.49 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.development.js +5.41% 392.96 kB 414.23 kB +6.05% 88.01 kB 93.33 kB
oss-stable/react-dom/cjs/react-dom-server.browser.development.js +5.41% 392.99 kB 414.25 kB +6.05% 88.03 kB 93.36 kB
facebook-www/ReactDOMServer-dev.classic.js +5.39% 403.83 kB 425.60 kB +5.71% 89.41 kB 94.51 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.development.js +5.36% 393.72 kB 414.82 kB +6.04% 87.87 kB 93.18 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.development.js +5.36% 393.75 kB 414.84 kB +6.04% 87.90 kB 93.21 kB
oss-stable-semver/react-dom/umd/react-dom-server.browser.development.js +5.36% 411.63 kB 433.67 kB +5.77% 89.20 kB 94.35 kB
oss-stable/react-dom/umd/react-dom-server.browser.development.js +5.35% 411.66 kB 433.70 kB +5.77% 89.23 kB 94.38 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.development.js +5.33% 398.67 kB 419.94 kB +5.78% 88.27 kB 93.37 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.development.js +5.33% 395.58 kB 416.67 kB +6.02% 88.33 kB 93.64 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.development.js +5.33% 395.60 kB 416.70 kB +6.02% 88.35 kB 93.67 kB
oss-stable-semver/react-dom/umd/react-dom-server-legacy.browser.development.js +5.30% 412.42 kB 434.28 kB +5.73% 89.08 kB 94.18 kB
oss-stable/react-dom/umd/react-dom-server-legacy.browser.development.js +5.30% 412.44 kB 434.30 kB +5.72% 89.11 kB 94.21 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.development.js +5.28% 413.56 kB 435.40 kB +5.56% 91.50 kB 96.59 kB
facebook-www/ReactDOMServerStreaming-dev.modern.js +5.27% 390.94 kB 411.54 kB +5.65% 86.43 kB 91.32 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.development.js +5.24% 413.15 kB 434.81 kB +5.56% 91.38 kB 96.45 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.development.js +5.20% 405.27 kB 426.37 kB +5.54% 90.35 kB 95.36 kB
oss-experimental/react-dom/umd/react-dom-server.browser.development.js +5.19% 432.68 kB 455.13 kB +5.54% 92.36 kB 97.47 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.development.js +5.18% 407.13 kB 428.22 kB +5.52% 90.81 kB 95.82 kB
oss-experimental/react-dom/umd/react-dom-server-legacy.browser.development.js +5.15% 424.45 kB 446.31 kB +5.49% 91.35 kB 96.36 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.production.min.js +5.09% 77.06 kB 80.99 kB +4.47% 23.85 kB 24.91 kB
oss-stable/react-dom/cjs/react-dom-server.node.production.min.js +5.09% 77.09 kB 81.01 kB +4.46% 23.87 kB 24.94 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.production.min.js +5.05% 74.01 kB 77.75 kB +4.36% 23.14 kB 24.15 kB
oss-stable/react-dom/cjs/react-dom-server.browser.production.min.js +5.05% 74.03 kB 77.77 kB +4.35% 23.16 kB 24.17 kB
facebook-www/ReactDOMServer-prod.modern.js +5.03% 176.92 kB 185.83 kB +4.71% 32.03 kB 33.54 kB
oss-stable-semver/react-dom/umd/react-dom-server.browser.production.min.js +5.03% 74.14 kB 77.87 kB +4.68% 23.50 kB 24.60 kB
oss-stable/react-dom/umd/react-dom-server.browser.production.min.js +5.03% 74.17 kB 77.90 kB +4.68% 23.53 kB 24.63 kB
facebook-www/ReactDOMServer-prod.classic.js +4.98% 178.79 kB 187.70 kB +4.68% 32.37 kB 33.88 kB
oss-stable-semver/react-dom/cjs/react-dom-server.bun.production.min.js +4.94% 75.63 kB 79.36 kB +4.36% 23.00 kB 24.00 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +4.94% 71.83 kB 75.38 kB +4.36% 21.90 kB 22.86 kB
oss-stable/react-dom/cjs/react-dom-server.bun.production.min.js +4.94% 75.65 kB 79.39 kB +4.36% 23.02 kB 24.03 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +4.93% 71.86 kB 75.40 kB +4.35% 21.93 kB 22.88 kB
oss-stable-semver/react-dom/umd/react-dom-server-legacy.browser.production.min.js +4.92% 71.98 kB 75.52 kB +4.46% 22.32 kB 23.31 kB
oss-stable/react-dom/umd/react-dom-server-legacy.browser.production.min.js +4.91% 72.01 kB 75.55 kB +4.45% 22.34 kB 23.34 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.production.min.js +4.87% 79.89 kB 83.78 kB +4.61% 24.50 kB 25.63 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.production.min.js +4.87% 78.98 kB 82.82 kB +4.43% 24.72 kB 25.82 kB
oss-stable/react-dom/cjs/react-dom-server.edge.production.min.js +4.87% 79.01 kB 82.85 kB +4.42% 24.75 kB 25.84 kB
oss-experimental/react-dom/umd/react-dom-server.browser.production.min.js +4.85% 80.02 kB 83.89 kB +4.74% 24.87 kB 26.04 kB
oss-experimental/react-dom/cjs/react-dom-server.node.production.min.js +4.83% 83.42 kB 87.45 kB +4.26% 25.42 kB 26.51 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +4.75% 74.47 kB 78.01 kB +4.15% 22.70 kB 23.64 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.production.min.js +4.74% 78.62 kB 82.35 kB +4.19% 23.82 kB 24.82 kB
oss-experimental/react-dom/umd/react-dom-server-legacy.browser.production.min.js +4.73% 74.61 kB 78.14 kB +4.35% 23.05 kB 24.05 kB
facebook-www/ReactDOMServerStreaming-prod.modern.js +4.71% 184.85 kB 193.56 kB +4.18% 33.99 kB 35.41 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.production.min.js +4.69% 85.20 kB 89.20 kB +4.38% 26.19 kB 27.33 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.production.min.js +4.50% 77.51 kB 81.00 kB +3.94% 23.76 kB 24.70 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.production.min.js +4.50% 77.53 kB 81.02 kB +3.93% 23.79 kB 24.72 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.production.min.js +4.38% 80.46 kB 83.98 kB +3.96% 24.57 kB 25.54 kB
oss-stable-semver/react-server/cjs/react-server.development.js +2.02% 190.62 kB 194.47 kB +2.43% 44.64 kB 45.73 kB
oss-stable/react-server/cjs/react-server.development.js +2.02% 190.62 kB 194.47 kB +2.43% 44.64 kB 45.73 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable-semver/react-dom/cjs/react-dom-server.node.development.js +5.72% 390.85 kB 413.22 kB +6.54% 86.61 kB 92.27 kB
oss-stable/react-dom/cjs/react-dom-server.node.development.js +5.72% 390.88 kB 413.25 kB +6.53% 86.64 kB 92.29 kB
oss-experimental/react-dom/cjs/react-dom-server.node.development.js +5.49% 410.70 kB 433.27 kB +5.99% 90.27 kB 95.68 kB
oss-stable-semver/react-dom/cjs/react-dom-server.bun.development.js +5.49% 387.12 kB 408.39 kB +6.24% 85.83 kB 91.19 kB
oss-stable/react-dom/cjs/react-dom-server.bun.development.js +5.49% 387.14 kB 408.41 kB +6.24% 85.86 kB 91.22 kB
facebook-www/ReactDOMServer-dev.modern.js +5.49% 396.41 kB 418.18 kB +5.88% 87.74 kB 92.89 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.development.js +5.45% 393.37 kB 414.81 kB +6.06% 88.12 kB 93.46 kB
oss-stable/react-dom/cjs/react-dom-server.edge.development.js +5.45% 393.40 kB 414.84 kB +6.05% 88.15 kB 93.49 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.development.js +5.41% 392.96 kB 414.23 kB +6.05% 88.01 kB 93.33 kB
oss-stable/react-dom/cjs/react-dom-server.browser.development.js +5.41% 392.99 kB 414.25 kB +6.05% 88.03 kB 93.36 kB
facebook-www/ReactDOMServer-dev.classic.js +5.39% 403.83 kB 425.60 kB +5.71% 89.41 kB 94.51 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.development.js +5.36% 393.72 kB 414.82 kB +6.04% 87.87 kB 93.18 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.development.js +5.36% 393.75 kB 414.84 kB +6.04% 87.90 kB 93.21 kB
oss-stable-semver/react-dom/umd/react-dom-server.browser.development.js +5.36% 411.63 kB 433.67 kB +5.77% 89.20 kB 94.35 kB
oss-stable/react-dom/umd/react-dom-server.browser.development.js +5.35% 411.66 kB 433.70 kB +5.77% 89.23 kB 94.38 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.development.js +5.33% 398.67 kB 419.94 kB +5.78% 88.27 kB 93.37 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.development.js +5.33% 395.58 kB 416.67 kB +6.02% 88.33 kB 93.64 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.development.js +5.33% 395.60 kB 416.70 kB +6.02% 88.35 kB 93.67 kB
oss-stable-semver/react-dom/umd/react-dom-server-legacy.browser.development.js +5.30% 412.42 kB 434.28 kB +5.73% 89.08 kB 94.18 kB
oss-stable/react-dom/umd/react-dom-server-legacy.browser.development.js +5.30% 412.44 kB 434.30 kB +5.72% 89.11 kB 94.21 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.development.js +5.28% 413.56 kB 435.40 kB +5.56% 91.50 kB 96.59 kB
facebook-www/ReactDOMServerStreaming-dev.modern.js +5.27% 390.94 kB 411.54 kB +5.65% 86.43 kB 91.32 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.development.js +5.24% 413.15 kB 434.81 kB +5.56% 91.38 kB 96.45 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.development.js +5.20% 405.27 kB 426.37 kB +5.54% 90.35 kB 95.36 kB
oss-experimental/react-dom/umd/react-dom-server.browser.development.js +5.19% 432.68 kB 455.13 kB +5.54% 92.36 kB 97.47 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.development.js +5.18% 407.13 kB 428.22 kB +5.52% 90.81 kB 95.82 kB
oss-experimental/react-dom/umd/react-dom-server-legacy.browser.development.js +5.15% 424.45 kB 446.31 kB +5.49% 91.35 kB 96.36 kB
oss-stable-semver/react-dom/cjs/react-dom-server.node.production.min.js +5.09% 77.06 kB 80.99 kB +4.47% 23.85 kB 24.91 kB
oss-stable/react-dom/cjs/react-dom-server.node.production.min.js +5.09% 77.09 kB 81.01 kB +4.46% 23.87 kB 24.94 kB
oss-stable-semver/react-dom/cjs/react-dom-server.browser.production.min.js +5.05% 74.01 kB 77.75 kB +4.36% 23.14 kB 24.15 kB
oss-stable/react-dom/cjs/react-dom-server.browser.production.min.js +5.05% 74.03 kB 77.77 kB +4.35% 23.16 kB 24.17 kB
facebook-www/ReactDOMServer-prod.modern.js +5.03% 176.92 kB 185.83 kB +4.71% 32.03 kB 33.54 kB
oss-stable-semver/react-dom/umd/react-dom-server.browser.production.min.js +5.03% 74.14 kB 77.87 kB +4.68% 23.50 kB 24.60 kB
oss-stable/react-dom/umd/react-dom-server.browser.production.min.js +5.03% 74.17 kB 77.90 kB +4.68% 23.53 kB 24.63 kB
facebook-www/ReactDOMServer-prod.classic.js +4.98% 178.79 kB 187.70 kB +4.68% 32.37 kB 33.88 kB
oss-stable-semver/react-dom/cjs/react-dom-server.bun.production.min.js +4.94% 75.63 kB 79.36 kB +4.36% 23.00 kB 24.00 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +4.94% 71.83 kB 75.38 kB +4.36% 21.90 kB 22.86 kB
oss-stable/react-dom/cjs/react-dom-server.bun.production.min.js +4.94% 75.65 kB 79.39 kB +4.36% 23.02 kB 24.03 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +4.93% 71.86 kB 75.40 kB +4.35% 21.93 kB 22.88 kB
oss-stable-semver/react-dom/umd/react-dom-server-legacy.browser.production.min.js +4.92% 71.98 kB 75.52 kB +4.46% 22.32 kB 23.31 kB
oss-stable/react-dom/umd/react-dom-server-legacy.browser.production.min.js +4.91% 72.01 kB 75.55 kB +4.45% 22.34 kB 23.34 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.production.min.js +4.87% 79.89 kB 83.78 kB +4.61% 24.50 kB 25.63 kB
oss-stable-semver/react-dom/cjs/react-dom-server.edge.production.min.js +4.87% 78.98 kB 82.82 kB +4.43% 24.72 kB 25.82 kB
oss-stable/react-dom/cjs/react-dom-server.edge.production.min.js +4.87% 79.01 kB 82.85 kB +4.42% 24.75 kB 25.84 kB
oss-experimental/react-dom/umd/react-dom-server.browser.production.min.js +4.85% 80.02 kB 83.89 kB +4.74% 24.87 kB 26.04 kB
oss-experimental/react-dom/cjs/react-dom-server.node.production.min.js +4.83% 83.42 kB 87.45 kB +4.26% 25.42 kB 26.51 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +4.75% 74.47 kB 78.01 kB +4.15% 22.70 kB 23.64 kB
oss-experimental/react-dom/cjs/react-dom-server.bun.production.min.js +4.74% 78.62 kB 82.35 kB +4.19% 23.82 kB 24.82 kB
oss-experimental/react-dom/umd/react-dom-server-legacy.browser.production.min.js +4.73% 74.61 kB 78.14 kB +4.35% 23.05 kB 24.05 kB
facebook-www/ReactDOMServerStreaming-prod.modern.js +4.71% 184.85 kB 193.56 kB +4.18% 33.99 kB 35.41 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.production.min.js +4.69% 85.20 kB 89.20 kB +4.38% 26.19 kB 27.33 kB
oss-stable-semver/react-dom/cjs/react-dom-server-legacy.node.production.min.js +4.50% 77.51 kB 81.00 kB +3.94% 23.76 kB 24.70 kB
oss-stable/react-dom/cjs/react-dom-server-legacy.node.production.min.js +4.50% 77.53 kB 81.02 kB +3.93% 23.79 kB 24.72 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.production.min.js +4.38% 80.46 kB 83.98 kB +3.96% 24.57 kB 25.54 kB
oss-stable-semver/react-server/cjs/react-server.development.js +2.02% 190.62 kB 194.47 kB +2.43% 44.64 kB 45.73 kB
oss-stable/react-server/cjs/react-server.development.js +2.02% 190.62 kB 194.47 kB +2.43% 44.64 kB 45.73 kB
oss-experimental/react-server/cjs/react-server.development.js +1.92% 200.88 kB 204.73 kB +2.37% 46.52 kB 47.62 kB
oss-stable-semver/react-server/cjs/react-server.production.min.js +1.33% 35.04 kB 35.50 kB +0.60% 11.06 kB 11.13 kB
oss-stable/react-server/cjs/react-server.production.min.js +1.33% 35.04 kB 35.50 kB +0.60% 11.06 kB 11.13 kB
oss-experimental/react-server/cjs/react-server.production.min.js +1.23% 37.88 kB 38.35 kB +0.64% 11.79 kB 11.87 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-server.production.min.js +1.00% 3.11 kB 3.15 kB +1.11% 1.17 kB 1.18 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-server.production.min.js +1.00% 3.11 kB 3.15 kB +1.11% 1.17 kB 1.18 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-server.production.min.js +1.00% 3.11 kB 3.15 kB +1.11% 1.17 kB 1.18 kB
oss-experimental/react-noop-renderer/cjs/react-noop-renderer-server.development.js +0.59% 6.26 kB 6.29 kB +0.76% 1.72 kB 1.73 kB
oss-stable-semver/react-noop-renderer/cjs/react-noop-renderer-server.development.js +0.59% 6.26 kB 6.29 kB +0.76% 1.72 kB 1.73 kB
oss-stable/react-noop-renderer/cjs/react-noop-renderer-server.development.js +0.59% 6.26 kB 6.29 kB +0.76% 1.72 kB 1.73 kB

Generated by 🚫 dangerJS against 4140b11

@@ -60,6 +63,8 @@ type Options = {
unstable_externalRuntimeSrc?: string | BootstrapScriptDescriptor,
importMap?: ImportMap,
formState?: ReactFormState<any, any> | null,
onHeaders?: (headers: HeadersDescriptor) => void,
headersLengthHint?: number,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hints seems pretty vague. Can't I specify a max? maxHeadersLength?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a max because I don't want to compute the next headers length before deciding if it is going to be a header. I chose hint b/c I want it to communicate that this number is a target not a hard limit. You'll get a header object "near" this length. Also it's already a little imprecise b/c it's code unit length which can be 1x to 4x in byte length when sent via utf8. My guess is the average english website will have a byte length around close to 1x the headers length but in other character sets the headers are going to be longer

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think HTTP headers really support utf8 in practice. Traditionally it was ISO-8859-1 but in practice only the ASCII subset should be used. So either we assume that the URLs have already been encoded or we should arguably encode the URLs ourselves otherwise.

request.completedRootSegment === null
? request.pendingRootTasks === 0
: request.completedRootSegment.status !== POSTPONED;
emitEarlyPreloads(request.renderState, request.resumableState, shellComplete);
Copy link
Collaborator

@sebmarkbage sebmarkbage Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can potentially happen a lot when you drain a queue often. Unfortunate to add all these extra checks here.

Additionally, if this is happening in a drain event, you might be in a bad context to do something about it. Other other callback tend to be top of stack like scheduled work.

This is also impossible in all APIs but Node.

I wonder if we should just move this into its own function for Node or something.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah makes sense

// I extracted this function out because we want to ensure we consistently emit preloads before
// transitioning to the next request stage and this transition can happen in multiple places in this
// implementation.
function completeWork(request: Request) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little confusing that this name doesn't have anything to do with the same function name in Fiber.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

completeAll is analagous to onAllReady. similar to completeShell for onShellReady

referrerPolicy: props.refererPolicy,
});
headers.highImagePreloads += header;
headers.remainingCapacity -= header.length;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this ends up exceeding capacity, can't we just throw it away and and take the other path instead?

So we don't need the hacky "hint" which may or may not be fully honored.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i suppose you only pay the cost once so we can do the limit thing. But per my other comment the limit is still quite imprecise

@@ -101,6 +103,12 @@ export function prepareHostDispatcher() {
ReactDOMCurrentDispatcher.current = ReactDOMServerDispatcher;
}

export type HeadersDescriptor = {
Link: string,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this field should be optional just to indicate that in the future we might not include it in every call. Given that this type will be reflected in public types.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

// This escaping function is only safe to use for href values being written into
// a "Link" header in between `<` and `>` characters. The primary concern with the href is
// to escape the bounding characters as well as new lines. This is unsafe to use in any other
// context
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is tabs a concern here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my research no, it is specifically \r\n. However this is what the spec says, it's possible there are clients in the wild that are more permissive in what breaks out of the href parsing context

// This is hacky but we always append headers into each queue assuming it is a subsequent
// header. This means that if the string has any length at all it will start with ", "
// so we truncate it here. This is ugly but the alternatives are to use arrays and join
// or to length check the queue each time we write to it. This is the most efficient option
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this will always flatten the ropes and clone it I think so might not be so good. We should just check each time before we add. Since it's interned if (linkHeader !== '') linkHeader += ', '; might be fastest.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah that makes sense

@gnoff gnoff marked this pull request as ready for review November 7, 2023 17:41
@gnoff gnoff force-pushed the fizz-on-headers branch 2 times, most recently from 74128be to 6523996 Compare November 7, 2023 18:07
During a render it can be useful to emit headers to start the process of preloading without waiting for the shell to complete. If provided an `onHeadersReady` callback option Fizz will call this callback with some headers. Currently this is implemented as a set of preload link headers that are generated during the first performWork pass. After this pass preloads are handled as they were preious to this change and emitted as tags.

Since headers are not streamed typically we are choosing an API that provides headers once. Because of this we need to choose a point at which to provide headers once even if we could in theory wait longer to accumulate more. It is possible that a better API is to allow the caller to tell Fizz when it wants whatever headers have accumulated however in practice this would require having some secondary header outside of react that is blocking sending headers and that is considered not a likely ocurrence

In this commit we add the option to pass `onHeadersReady` to static and dynamic render entrypoints. This is not yet wired up to anything so it will never be called. This will be added in a subsequent commit.
@gnoff gnoff merged commit 2983249 into facebook:main Nov 7, 2023
35 of 36 checks passed
@gnoff gnoff deleted the fizz-on-headers branch November 7, 2023 18:16
github-actions bot pushed a commit that referenced this pull request Nov 7, 2023
Adds a new option to `react-dom/server` entrypoints.

`onHeaders: (headers: Headers) => void` (non node envs)
`onHeaders: (headers: { Link?: string }) => void` (node envs)

When any `renderTo...` or `prerender...` function is called and this
option is provided the supplied function will be called sometime on or
before completion of the render with some preload link headers.

When provided during a `renderTo...` the callback will usually be called
after the first pass at work. The idea here is we want to get a set of
headers to start the browser loading well before the shell is ready. We
don't wait for the shell because if we did we may as well send the
preloads as tags in the HTML.

When provided during a `prerender...` the callback will be called after
the entire prerender is complete. The idea here is we are not responding
to a live request and it is preferable to capture as much as possible
for preloading as Headers in case the prerender was unable to finish the
shell.

Currently the following resources are always preloaded as headers when
the option is provided
1. prefetchDNS and preconnects
2. font preloads
3. high priority image preloads

Additionally if we are providing headers when the shell is incomplete
(regardless of whether it is render or prerender) we will also include
any stylesheet Resources (ones with a precedence prop)

There is a second option `maxHeadersLength?: number` which allows you to
specify the maximum length of the header content in unicode code units.
This is what you get when you read the length property of a string in
javascript. It's improtant to note that this is not the same as the
utf-8 byte length when these headers are serialized in a Response. The
utf8 representation may be the same size, or larger but it will never be
smaller.

If you do not supply a `maxHeadersLength` we defaul to `2000`. This was
chosen as half the value of the max headers length supported by commonly
known web servers and CDNs. many browser and web server can support
significantly more headers than this so you can use this option to
increase the headers limit. You can also of course use it to be even
more conservative. Again it is important to keep in mind there is no
direct translation between the max length and the bytelength and so if
you want to stay under a certain byte length you need to be potentially
more aggressive in the maxHeadersLength you choose.

Conceptually `onHeaders` could be called more than once as new headers
are discovered however if we haven't started flushing yet but since most
APIs for the server including the web standard Response only allow you
to set headers once the current implementation will only call it one
time

DiffTrain build for [2983249](2983249)
EdisonVan pushed a commit to EdisonVan/react that referenced this pull request Apr 15, 2024
…k#27641)

Adds a new option to `react-dom/server` entrypoints.

`onHeaders: (headers: Headers) => void` (non node envs)
`onHeaders: (headers: { Link?: string }) => void` (node envs)

When any `renderTo...` or `prerender...` function is called and this
option is provided the supplied function will be called sometime on or
before completion of the render with some preload link headers.

When provided during a `renderTo...` the callback will usually be called
after the first pass at work. The idea here is we want to get a set of
headers to start the browser loading well before the shell is ready. We
don't wait for the shell because if we did we may as well send the
preloads as tags in the HTML.

When provided during a `prerender...` the callback will be called after
the entire prerender is complete. The idea here is we are not responding
to a live request and it is preferable to capture as much as possible
for preloading as Headers in case the prerender was unable to finish the
shell.

Currently the following resources are always preloaded as headers when
the option is provided
1. prefetchDNS and preconnects
2. font preloads
3. high priority image preloads

Additionally if we are providing headers when the shell is incomplete
(regardless of whether it is render or prerender) we will also include
any stylesheet Resources (ones with a precedence prop)

There is a second option `maxHeadersLength?: number` which allows you to
specify the maximum length of the header content in unicode code units.
This is what you get when you read the length property of a string in
javascript. It's improtant to note that this is not the same as the
utf-8 byte length when these headers are serialized in a Response. The
utf8 representation may be the same size, or larger but it will never be
smaller.

If you do not supply a `maxHeadersLength` we defaul to `2000`. This was
chosen as half the value of the max headers length supported by commonly
known web servers and CDNs. many browser and web server can support
significantly more headers than this so you can use this option to
increase the headers limit. You can also of course use it to be even
more conservative. Again it is important to keep in mind there is no
direct translation between the max length and the bytelength and so if
you want to stay under a certain byte length you need to be potentially
more aggressive in the maxHeadersLength you choose.

Conceptually `onHeaders` could be called more than once as new headers
are discovered however if we haven't started flushing yet but since most
APIs for the server including the web standard Response only allow you
to set headers once the current implementation will only call it one
time
bigfootjon pushed a commit that referenced this pull request Apr 18, 2024
Adds a new option to `react-dom/server` entrypoints.

`onHeaders: (headers: Headers) => void` (non node envs)
`onHeaders: (headers: { Link?: string }) => void` (node envs)

When any `renderTo...` or `prerender...` function is called and this
option is provided the supplied function will be called sometime on or
before completion of the render with some preload link headers.

When provided during a `renderTo...` the callback will usually be called
after the first pass at work. The idea here is we want to get a set of
headers to start the browser loading well before the shell is ready. We
don't wait for the shell because if we did we may as well send the
preloads as tags in the HTML.

When provided during a `prerender...` the callback will be called after
the entire prerender is complete. The idea here is we are not responding
to a live request and it is preferable to capture as much as possible
for preloading as Headers in case the prerender was unable to finish the
shell.

Currently the following resources are always preloaded as headers when
the option is provided
1. prefetchDNS and preconnects
2. font preloads
3. high priority image preloads

Additionally if we are providing headers when the shell is incomplete
(regardless of whether it is render or prerender) we will also include
any stylesheet Resources (ones with a precedence prop)

There is a second option `maxHeadersLength?: number` which allows you to
specify the maximum length of the header content in unicode code units.
This is what you get when you read the length property of a string in
javascript. It's improtant to note that this is not the same as the
utf-8 byte length when these headers are serialized in a Response. The
utf8 representation may be the same size, or larger but it will never be
smaller.

If you do not supply a `maxHeadersLength` we defaul to `2000`. This was
chosen as half the value of the max headers length supported by commonly
known web servers and CDNs. many browser and web server can support
significantly more headers than this so you can use this option to
increase the headers limit. You can also of course use it to be even
more conservative. Again it is important to keep in mind there is no
direct translation between the max length and the bytelength and so if
you want to stay under a certain byte length you need to be potentially
more aggressive in the maxHeadersLength you choose.

Conceptually `onHeaders` could be called more than once as new headers
are discovered however if we haven't started flushing yet but since most
APIs for the server including the web standard Response only allow you
to set headers once the current implementation will only call it one
time

DiffTrain build for commit 2983249.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants