Repository: vercel/next.js
File: packages/next/src/client/components/segment-cache/cache.ts (TypeScript)

Code before:

  const staleTimeMs = !isNaN(staleTimeSeconds)
    ? getStaleTimeMs(staleTimeSeconds)
    : STATIC_STALETIME_MS

  return now + staleTimeMs
}

async function getStaleAt(
  now: number,
  serverData: NavigationFlightResponse,
  response: RSCResponse<unknown>
): Promise<number> {
  if (serverData.s !== undefined) {
    // Iterate the async iterable and take the last yielded value. The server
    // yields updated staleTime values during the render; the last one is the
    // final staleTime.
    let staleTimeSeconds: number | undefined
    for await (const value of serverData.s) {
      staleTimeSeconds = value
    }

    if (staleTimeSeconds !== undefined) {
      const staleTimeMs = isNaN(staleTimeSeconds)
        ? STATIC_STALETIME_MS
        : getStaleTimeMs(staleTimeSeconds)

      return now + staleTimeMs
    }
  }

  return getStaleAtFromHeader(now, response)
}

/**
 * Strips the leading isPartial byte from a runtime prefetch response stream.
 * Returns the remaining stream and whether the response is partial.
 */
async function stripIsPartialByte(
  stream: ReadableStream<Uint8Array>
): Promise<{ stream: ReadableStream<Uint8Array>; isPartial: boolean }> {
  const reader = stream.getReader()
  const { done, value } = await reader.read()
  if (done || !value || value.byteLength === 0) {
    return {
      stream: new ReadableStream({ start: (c) => c.close() }),
      isPartial: false,
    }
  }
  const isPartial = value[0] === 0x7e // ASCII '~'
  const remainder = value.byteLength > 1 ? value.subarray(1) : null

Review comment (performance):
Instead of `await`, how about we read it from the extra internal field added by Flight, like we do for vary params here: https://github.com/vercel/next.js/blob/ea3cad2848539d9a6273b42b09011d52b8a99b6a/packages/next/src/shared/lib/segment-cache/vary-params-decoding.ts#L37-L53

Code after:

  const staleTimeMs = !isNaN(staleTimeSeconds)
    ? getStaleTimeMs(staleTimeSeconds)
    : STATIC_STALETIME_MS

  return now + staleTimeMs
}

async function getStaleAt(
  now: number,
  serverData: NavigationFlightResponse,
  response: RSCResponse<unknown>
): Promise<number> {
  if (serverData.s !== undefined) {
    // Iterate the async iterable and take the last yielded value. The server
    // yields updated staleTime values during the render; the last one is the
    // final staleTime.
    let staleTimeSeconds: number | undefined

    // TODO: Buffer the response and then read the iterable values
    // synchronously, similar to readVaryParams. This would avoid the need to
    // make getStaleAt async, and we could also use it in
    // writeDynamicTreeResponseIntoCache. This will also be needed when React
    // starts leaving async iterables hanging when the outer RSC stream is
    // aborted e.g. due to sync I/O (with unstable_allowPartialStream).
    for await (const value of serverData.s) {
      staleTimeSeconds = value
    }

    if (staleTimeSeconds !== undefined) {
      const staleTimeMs = isNaN(staleTimeSeconds)
        ? STATIC_STALETIME_MS
        : getStaleTimeMs(staleTimeSeconds)

      return now + staleTimeMs
    }
  }

  return getStaleAtFromHeader(now, response)
}

/**
 * Strips the leading isPartial byte from a runtime prefetch response stream.
 * Returns the remaining stream and whether the response is partial.
 */
async function stripIsPartialByte(
  stream: ReadableStream<Uint8Array>
): Promise<{ stream: ReadableStream<Uint8Array>; isPartial: boolean }> {
  const reader = stream.getReader()
  const { done, value } = await reader.read()
  if (done || !value || value.byteLength === 0) {