Skip to content

Commit b000c43

Browse files
authoredJul 30, 2024··
feat: support next/image with static exports (#2563)
* test: add next/image to static export fixture and test * feat: support next/image with static exports
1 parent 367c396 commit b000c43

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed
 

‎src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ export const onBuild = async (options: NetlifyPluginOptions) => {
6464
await saveBuildCache(ctx)
6565
}
6666

67-
// static exports only need to be uploaded to the CDN
67+
// static exports only need to be uploaded to the CDN and setup /_next/image handler
6868
if (ctx.buildConfig.output === 'export') {
69-
return copyStaticExport(ctx)
69+
return Promise.all([copyStaticExport(ctx), setImageConfig(ctx)])
7070
}
7171

7272
await verifyAdvancedAPIRoutes(ctx)

‎tests/e2e/export.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,21 @@ test('Renders the Home page correctly with output export and custom dist dir', a
5252

5353
await expectImageWasLoaded(page.locator('img'))
5454
})
55+
56+
test.describe('next/image is using Netlify Image CDN', () => {
57+
test('Local images', async ({ page, outputExport }) => {
58+
const nextImageResponsePromise = page.waitForResponse('**/_next/image**')
59+
60+
await page.goto(`${outputExport.url}/image/local`)
61+
62+
const nextImageResponse = await nextImageResponsePromise
63+
expect(nextImageResponse.request().url()).toContain('_next/image?url=%2Fsquirrel.jpg')
64+
65+
expect(nextImageResponse.status()).toBe(200)
66+
// ensure next/image is using Image CDN
67+
// source image is jpg, but when requesting it through Image CDN avif will be returned
68+
expect(await nextImageResponse.headerValue('content-type')).toEqual('image/avif')
69+
70+
await expectImageWasLoaded(page.locator('img'))
71+
})
72+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Image from 'next/image'
2+
3+
export default function NextImageUsingNetlifyImageCDN() {
4+
return (
5+
<main>
6+
<h1>Next/Image + Netlify Image CDN</h1>
7+
<Image src="/squirrel.jpg" alt="a cute squirrel (next/image)" width={300} height={278} />
8+
</main>
9+
)
10+
}

0 commit comments

Comments
 (0)