Skip to content

Commit 22afaa8

Browse files
authoredFeb 7, 2024
fix: correctly serve images by storing binary as base64 (#241)
* refactor: use playwright fixtures for e2e tests * chore: adapt test from next.js * fix: images need to be encoded as base64 * fix: remove additional dependency * fix: oof, one character too much
1 parent 45a5d1c commit 22afaa8

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed
 

‎src/build/content/prerendered.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const buildAppCacheValue = async (path: string): Promise<PageCacheValue> => ({
3232

3333
const buildRouteCacheValue = async (path: string): Promise<RouteCacheValue> => ({
3434
kind: 'ROUTE',
35-
body: await readFile(`${path}.body`, 'utf-8'),
35+
body: await readFile(`${path}.body`, 'base64'),
3636
...JSON.parse(await readFile(`${path}.meta`, 'utf-8')),
3737
})
3838

‎src/run/handlers/cache.cts

+6-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class NetlifyCacheHandler implements CacheHandler {
8383
return {
8484
lastModified: blob.lastModified,
8585
value: {
86-
body: Buffer.from(blob.value.body),
86+
body: Buffer.from(blob.value.body, 'base64'),
8787
kind: blob.value.kind,
8888
status: blob.value.status,
8989
headers: blob.value.headers,
@@ -114,6 +114,11 @@ export class NetlifyCacheHandler implements CacheHandler {
114114

115115
console.debug(`[NetlifyCacheHandler.set]: ${key}`)
116116

117+
if (data?.kind === 'ROUTE') {
118+
// @ts-expect-error gotta find a better solution for this
119+
data.body = data.body.toString('base64')
120+
}
121+
117122
await this.blobStore.setJSON(blobKey, {
118123
lastModified,
119124
value: data,

‎tests/e2e/edge-middleware.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from '@playwright/test'
22
import { test } from '../utils/create-e2e-fixture.js'
3+
import { getImageSize } from 'next/dist/server/image-optimizer.js'
34

45
test('Runs edge middleware', async ({ page, middleware }) => {
56
await page.goto(`${middleware.url}/test/redirect`)
@@ -27,3 +28,14 @@ test('Supports CJS dependencies in Edge Middleware', async ({ page, middleware }
2728

2829
expect(await res?.headerValue('x-cjs-module-works')).toEqual("true")
2930
})
31+
32+
// adaptation of https://github.com/vercel/next.js/blob/8aa9a52c36f338320d55bd2ec292ffb0b8c7cb35/test/e2e/app-dir/metadata-edge/index.test.ts#L24C5-L31C7
33+
test('it should render OpenGraph image meta tag correctly', async ({ page, middleware }) => {
34+
await page.goto(`${middleware.url}/`)
35+
const ogURL = await page.locator('meta[property="og:image"]').getAttribute('content')
36+
expect(ogURL).toBeTruthy()
37+
const ogResponse = await fetch(new URL(new URL(ogURL!).pathname, middleware.url))
38+
const imageBuffer = await ogResponse.arrayBuffer()
39+
const size = await getImageSize(Buffer.from(imageBuffer), 'png')
40+
expect([size.width, size.height]).toEqual([1200, 630])
41+
})

‎tests/fixtures/middleware/app/og.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ImageResponse } from 'next/og'
2+
3+
export default function og() {
4+
return new ImageResponse(
5+
(
6+
<div
7+
style={{
8+
width: '100%',
9+
height: '100%',
10+
display: 'flex',
11+
alignItems: 'center',
12+
justifyContent: 'center',
13+
fontSize: 128,
14+
background: 'lavender',
15+
}}
16+
>
17+
Open Graph
18+
</div>
19+
)
20+
)
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const alt = 'Open Graph'
2+
3+
export { default } from './og'

0 commit comments

Comments
 (0)
Please sign in to comment.