File size: 1,351 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import { NextRequest } from 'next/server'
import { Streamable } from './streamable'
export const config = {
matcher: '/middleware',
}
let streamable: ReturnType<typeof Streamable> | undefined
export default async function handler(req: NextRequest): Promise<Response> {
if (req.nextUrl.searchParams.has('compile')) {
// The request just wants to trigger compilation.
return new Response(null, { status: 204 })
}
// Consume the entire request body.
// This is so we don't confuse the request close with the connection close.
await req.text()
const write = req.nextUrl.searchParams.get('write')
if (write) {
const s = (streamable = Streamable(+write))
// The request was aborted before the response was returned.
if (req.signal.aborted) {
s.abort()
return new Response(null, { status: 204 })
}
req.signal.onabort = () => {
s.abort()
}
return new Response(s.stream)
}
// The 2nd request should render the stats. We don't use a query param
// because edge rendering will create a different bundle for that.
const old = streamable
if (!old) {
return new Response(
'The streamable from the prime request is unexpectedly not available',
{ status: 500 }
)
}
streamable = undefined
const i = await old.finished
return new Response(`${i}`)
}
|