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

Add test case for image generation #42693

Merged
merged 5 commits into from Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -90,6 +90,7 @@
"@typescript-eslint/eslint-plugin": "4.29.1",
"@typescript-eslint/parser": "4.29.1",
"@vercel/fetch": "6.1.1",
"@vercel/og": "0.0.20",
"@webassemblyjs/ast": "1.11.1",
"@webassemblyjs/floating-point-hex-parser": "1.11.1",
"@webassemblyjs/helper-api-error": "1.11.1",
Expand Down
18 changes: 18 additions & 0 deletions packages/next/build/webpack/plugins/middleware-plugin.ts
Expand Up @@ -686,6 +686,24 @@ function getExtractMetadata(params: {
for (const module of modules) {
const buildInfo = getModuleBuildInfo(module)

/**
* Check if it uses the image generation feature.
*/
if (!dev) {
const resource = module.resource
if (
resource &&
/[\\/]node_modules[\\/]@vercel[\\/]og[\\/]dist[\\/]index.js$/.test(
resource
)
) {
telemetry.record({
eventName: 'NEXT_EDGE_IMAGE_GENERATION_USED',
shuding marked this conversation as resolved.
Show resolved Hide resolved
payload: {},
})
}
}

/**
* When building for production checks if the module is using `eval`
* and in such case produces a compilation error. The module has to
Expand Down
80 changes: 77 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions test/integration/image-generation/app/pages/api/image.jsx
@@ -0,0 +1,9 @@
import { ImageResponse } from '@vercel/og'

export default async () => {
return new ImageResponse(<div tw="w-full h-full text-5xl">hello</div>)
}

export const config = {
runtime: 'experimental-edge',
}
42 changes: 42 additions & 0 deletions test/integration/image-generation/test/index.test.ts
@@ -0,0 +1,42 @@
/* eslint-env jest */
import {
fetchViaHTTP,
findPort,
killApp,
nextBuild,
nextStart,
} from 'next-test-utils'
import { join } from 'path'

const appDir = join(__dirname, '../app')

describe('Image Generation', () => {
describe('Prod', () => {
let app
let appPort

beforeAll(async () => {
await nextBuild(appDir)
shuding marked this conversation as resolved.
Show resolved Hide resolved
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
})

it('should generate the image without errors', async () => {
const res = await fetchViaHTTP(appPort, '/api/image')
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('image/png')

const buffer = await res.buffer()

// It should be a PNG
expect(
[0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a].every(
(b, i) => buffer[i] === b
)
).toBeTrue()
})
})
})