Skip to content

Commit

Permalink
enable ImageResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Aug 11, 2023
1 parent c97e724 commit e6cc704
Showing 1 changed file with 38 additions and 44 deletions.
82 changes: 38 additions & 44 deletions packages/next/src/server/web/spec-extension/image-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,48 @@ export class ImageResponse {
typeof import('next/dist/compiled/@vercel/og').ImageResponse
>
) {
// @ts-expect-error - process.turbopack is a custom property
if (process.turbopack) {
// TODO(sokra) enable this again when turbopack supports wasm
throw new Error("Turbopack doesn't support ImageResponse currently")
} else {
const readable = new ReadableStream({
async start(controller) {
const OGImageResponse: typeof import('next/dist/compiled/@vercel/og').ImageResponse =
// So far we have to manually determine which build to use,
// as the auto resolving is not working
(
await import(
process.env.NEXT_RUNTIME === 'edge'
? 'next/dist/compiled/@vercel/og/index.edge.js'
: 'next/dist/compiled/@vercel/og/index.node.js'
)
).ImageResponse
const imageResponse = new OGImageResponse(...args) as Response
const readable = new ReadableStream({
async start(controller) {
const OGImageResponse: typeof import('next/dist/compiled/@vercel/og').ImageResponse =
// So far we have to manually determine which build to use,
// as the auto resolving is not working
(
await import(
process.env.NEXT_RUNTIME === 'edge'
? 'next/dist/compiled/@vercel/og/index.edge.js'
: 'next/dist/compiled/@vercel/og/index.node.js'
)
).ImageResponse
const imageResponse = new OGImageResponse(...args) as Response

if (!imageResponse.body) {
return controller.close()
}
if (!imageResponse.body) {
return controller.close()
}

const reader = imageResponse.body!.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
return controller.close()
}
controller.enqueue(value)
const reader = imageResponse.body!.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
return controller.close()
}
},
})
controller.enqueue(value)
}
},
})

const options = args[1] || {}
const options = args[1] || {}

return new Response(readable, {
headers: {
'content-type': 'image/png',
'cache-control':
process.env.NODE_ENV === 'development'
? 'no-cache, no-store'
: 'public, immutable, no-transform, max-age=31536000',
...options.headers,
},
status: options.status,
statusText: options.statusText,
})
}
return new Response(readable, {
headers: {
'content-type': 'image/png',
'cache-control':
process.env.NODE_ENV === 'development'
? 'no-cache, no-store'
: 'public, immutable, no-transform, max-age=31536000',
...options.headers,
},
status: options.status,
statusText: options.statusText,
})
}
}

0 comments on commit e6cc704

Please sign in to comment.