next.js/test/e2e/app-dir/cache-components/app/routes/static-stream-sync/route.ts
route.ts27 lines720 B
import type { NextRequest } from 'next/server'

import { getSentinelValue } from '../../getSentinelValue'

export function GET(request: NextRequest) {
  const response = JSON.stringify({
    value: getSentinelValue(),
    message: 'stream response',
  })
  const part1 = response.slice(0, Math.floor(response.length / 2))
  const part2 = response.slice(Math.floor(response.length / 2))

  const encoder = new TextEncoder()
  const chunks = [encoder.encode(part1), encoder.encode(part2)]

  let sent = 0
  const stream = new ReadableStream({
    pull(controller) {
      controller.enqueue(chunks[sent++])
      if (sent === chunks.length) {
        controller.close()
      }
    },
  })
  return new Response(stream)
}
Quest for Codev2.0.0
/
SIGN IN