@@ -431,9 +431,13 @@ export function createRenderState(
431431 fontPreloads : '' ,
432432 highImagePreloads : '' ,
433433 remainingCapacity :
434- typeof maxHeadersLength === 'number'
434+ // We seed the remainingCapacity with 2 extra bytes because when we decrement the capacity
435+ // we always assume we are inserting an interstitial ", " however the first header does not actually
436+ // consume these two extra bytes.
437+ 2 +
438+ ( typeof maxHeadersLength === 'number'
435439 ? maxHeadersLength
436- : DEFAULT_HEADERS_CAPACITY_IN_UTF16_CODE_UNITS ,
440+ : DEFAULT_HEADERS_CAPACITY_IN_UTF16_CODE_UNITS ) ,
437441 }
438442 : null ;
439443 const renderState : RenderState = {
@@ -2953,7 +2957,7 @@ function pushImg(
29532957 // make this behavior different between render and prerender since in the latter case
29542958 // we are less sensitive to the current requests runtime per and more sensitive to maximizing
29552959 // headers.
2956- ( headers . remainingCapacity -= header . length ) >= 2 )
2960+ ( headers . remainingCapacity -= header . length + 2 ) >= 0 )
29572961 ) {
29582962 // If we postpone in the shell we will still emit this preload so we track
29592963 // it to make sure we don't reset it.
@@ -5393,7 +5397,7 @@ function prefetchDNS(href: string) {
53935397 // make this behavior different between render and prerender since in the latter case
53945398 // we are less sensitive to the current requests runtime per and more sensitive to maximizing
53955399 // headers.
5396- ( headers . remainingCapacity -= header . length ) >= 2 )
5400+ ( headers . remainingCapacity -= header . length + 2 ) >= 0 )
53975401 ) {
53985402 // Store this as resettable in case we are prerendering and postpone in the Shell
53995403 renderState . resets . dns [ key ] = EXISTS ;
@@ -5452,7 +5456,7 @@ function preconnect(href: string, crossOrigin: ?CrossOriginEnum) {
54525456 // make this behavior different between render and prerender since in the latter case
54535457 // we are less sensitive to the current requests runtime per and more sensitive to maximizing
54545458 // headers.
5455- ( headers . remainingCapacity -= header . length ) >= 2 )
5459+ ( headers . remainingCapacity -= header . length + 2 ) >= 0 )
54565460 ) {
54575461 // Store this in resettableState in case we are prerending and postpone in the Shell
54585462 renderState . resets . connect [ bucket ] [ key ] = EXISTS ;
@@ -5518,7 +5522,7 @@ function preload(href: string, as: string, options?: ?PreloadImplOptions) {
55185522 // make this behavior different between render and prerender since in the latter case
55195523 // we are less sensitive to the current requests runtime per and more sensitive to maximizing
55205524 // headers.
5521- ( headers . remainingCapacity -= header . length ) >= 2 )
5525+ ( headers . remainingCapacity -= header . length + 2 ) >= 0 )
55225526 ) {
55235527 // If we postpone in the shell we will still emit a preload as a header so we
55245528 // track this to make sure we don't reset it.
@@ -5633,7 +5637,7 @@ function preload(href: string, as: string, options?: ?PreloadImplOptions) {
56335637 // make this behavior different between render and prerender since in the latter case
56345638 // we are less sensitive to the current requests runtime per and more sensitive to maximizing
56355639 // headers.
5636- ( headers . remainingCapacity -= header . length ) >= 2 )
5640+ ( headers . remainingCapacity -= header . length + 2 ) >= 0 )
56375641 ) {
56385642 // If we postpone in the shell we will still emit this preload so we
56395643 // track it here to prevent it from being reset.
@@ -6260,7 +6264,7 @@ export function emitEarlyPreloads(
62606264 // This means that a particularly long header might close out the header queue where later
62616265 // headers could still fit. We could in the future alter the behavior here based on prerender vs render
62626266 // since during prerender we aren't as concerned with pure runtime performance.
6263- if ( ( headers . remainingCapacity -= header . length ) >= 2 ) {
6267+ if ( ( headers . remainingCapacity -= header . length + 2 ) >= 0 ) {
62646268 renderState . resets . style [ key ] = PRELOAD_NO_CREDS ;
62656269 if ( linkHeader ) {
62666270 linkHeader += ', ';
0 commit comments