Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ImageResponse to next/og #56662

Merged
merged 10 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/next-swc/crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ pub async fn get_next_edge_import_map(
"next/router" => "next/dist/esm/client/router".to_string(),
"next/script" => "next/dist/esm/client/script".to_string(),
"next/server" => "next/dist/esm/server/web/exports/index".to_string(),
"next/og" => "next/dist/esm/server/og/image-response".to_string(),

"next/dist/client/components/headers" => "next/dist/esm/client/components/headers".to_string(),
"next/dist/client/components/navigation" => "next/dist/esm/client/components/navigation".to_string(),
Expand All @@ -410,6 +411,7 @@ pub async fn get_next_edge_import_map(
"next/dist/shared/lib/dynamic" => "next/dist/esm/shared/lib/dynamic".to_string(),
"next/dist/shared/lib/head" => "next/dist/esm/shared/lib/head".to_string(),
"next/dist/shared/lib/image-external" => "next/dist/esm/shared/lib/image-external".to_string(),
"dist/server/og/image-response" => "next/dist/esm/server/og/image-response".to_string(),
},
);

Expand Down
1 change: 1 addition & 0 deletions packages/next/og.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist/server/og/image-response'
1 change: 1 addition & 0 deletions packages/next/og.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/server/og/image-response')
2 changes: 2 additions & 0 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
"jest.d.ts",
"amp.js",
"amp.d.ts",
"og.js",
"og.d.ts",
"index.d.ts",
"types/index.d.ts",
"types/global.d.ts",
Expand Down
6 changes: 4 additions & 2 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ export default async function getBaseWebpackConfig(
],
alias: {
// Alias 3rd party @vercel/og package to vendored og image package to reduce bundle size
'@vercel/og': 'next/dist/server/web/spec-extension/image-response',
'@vercel/og': 'next/dist/server/og/image-response',

// Alias next/dist imports to next/dist/esm assets,
// let this alias hit before `next` alias.
Expand All @@ -827,6 +827,8 @@ export default async function getBaseWebpackConfig(
// Alias the usage of next public APIs
[path.join(NEXT_PROJECT_ROOT, 'server')]:
'next/dist/esm/server/web/exports/index',
[path.join(NEXT_PROJECT_ROOT, 'og')]:
'next/dist/esm/server/og/image-response',
[path.join(NEXT_PROJECT_ROOT_DIST, 'client', 'link')]:
'next/dist/esm/client/link',
[path.join(
Expand Down Expand Up @@ -1903,7 +1905,7 @@ export default async function getBaseWebpackConfig(
{
// Mark `image-response.js` as side-effects free to make sure we can
// tree-shake it if not used.
test: /[\\/]next[\\/]dist[\\/](esm[\\/])?server[\\/]web[\\/]exports[\\/]image-response\.js/,
test: /[\\/]next[\\/]dist[\\/](esm[\\/])?server[\\/]og[\\/]image-response\.js/,
sideEffects: false,
},
].filter(Boolean),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ function getExtractMetadata(params: {
const resource = module.resource
const hasOGImageGeneration =
resource &&
/[\\/]node_modules[\\/]@vercel[\\/]og[\\/]dist[\\/]index\.(edge|node)\.js$|[\\/]next[\\/]dist[\\/](esm[\\/])?server[\\/]web[\\/]spec-extension[\\/]image-response\.js$/.test(
/[\\/]node_modules[\\/]@vercel[\\/]og[\\/]dist[\\/]index\.(edge|node)\.js$|[\\/]next[\\/]dist[\\/](esm[\\/])?server[\\/]og[\\/]image-response\.js$/.test(
resource
)

Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/compiled/@vercel/og/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@vercel/og",
"version": "0.5.15",
"LICENSE": "MLP-2.0",
"type": "module",
"main": "./index.node.js",
Expand All @@ -9,6 +10,7 @@
"import": "./index.node.js",
"node": "./index.node.js",
"default": "./index.node.js"
}
},
"./package.json": "./package.json"
}
}
52 changes: 52 additions & 0 deletions packages/next/src/server/og/image-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export class ImageResponse extends Response {
public static displayName = 'NextImageResponse'
constructor(
...args: ConstructorParameters<
typeof import('next/dist/compiled/@vercel/og').ImageResponse
>
) {
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()
}

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] || {}

super(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,
})
}
}
55 changes: 4 additions & 51 deletions packages/next/src/server/web/spec-extension/image-response.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,5 @@
export class ImageResponse extends Response {
public static displayName = 'NextImageResponse'
constructor(
...args: ConstructorParameters<
typeof import('next/dist/compiled/@vercel/og').ImageResponse
>
) {
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()
}

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] || {}

super(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,
})
}
export function ImageResponse() {
throw new Error(
'ImageResponse export is moved to next/og since Next.js 14, please import from "next/og" instead'
)
huozhi marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions packages/next/taskfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export async function copy_vercel_og(task, opts) {
join(__dirname, 'src/compiled/@vercel/og/package.json'),
{
name: '@vercel/og',
version: require('@vercel/og/package.json').version,
huozhi marked this conversation as resolved.
Show resolved Hide resolved
LICENSE: 'MLP-2.0',
type: 'module',
main: './index.node.js',
Expand All @@ -238,6 +239,7 @@ export async function copy_vercel_og(task, opts) {
node: './index.node.js',
default: './index.node.js',
},
'./package.json': './package.json',
},
},
{ spaces: 2 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

// without id
export async function generateImageMetadata({ params }) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export async function generateImageMetadata({ params }) {
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export const alt = 'Open Graph'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export default function og() {
return new ImageResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export const alt = 'Twitter'
export const size = { width: 1200, height: 675 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export const contentType = 'image/png'

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/metadata-dynamic-routes/app/favicon.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This file should be ignored
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export default function favicon() {
return new ImageResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export const contentType = 'image/png'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export const contentType = 'image/png'

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/metadata-dynamic-routes/app/icon.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export const contentType = 'image/png'
export const size = { width: 512, height: 512 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export const alt = 'Open Graph'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export const alt = 'Open Graph'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'
import { ImageResponse as ImageResponse2 } from '@vercel/og'

// Node.js: Using @vercel/og external package, and should be aliased to "next/server" ImageResponse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'
// @ts-ignore
import { ImageResponse as ImageResponse2 } from '@vercel/og'

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/metadata-dynamic-routes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ createNextDescribe(
it('should error when id is missing in generateImageMetadata', async () => {
const iconFilePath = 'app/metadata-base/unset/icon.tsx'
const contentMissingIdProperty = `
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'
export async function generateImageMetadata() {
return [
{
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/metadata-edge/app/og.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImageResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export default function og() {
return new ImageResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ImageResponse, NextResponse } from 'next/server'
import { NextResponse } from 'next/server'
import { ImageResponse } from 'next/og'

export async function middleware(req) {
console.log('middleware', req.url)
Expand Down
Loading