Skip to content

Commit

Permalink
feat: support next/image with static exports (#2563)
Browse files Browse the repository at this point in the history
* test: add next/image to static export fixture and test

* feat: support next/image with static exports
  • Loading branch information
pieh committed Jul 30, 2024
1 parent 367c396 commit b000c43
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export const onBuild = async (options: NetlifyPluginOptions) => {
await saveBuildCache(ctx)
}

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

await verifyAdvancedAPIRoutes(ctx)
Expand Down
18 changes: 18 additions & 0 deletions tests/e2e/export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,21 @@ test('Renders the Home page correctly with output export and custom dist dir', a

await expectImageWasLoaded(page.locator('img'))
})

test.describe('next/image is using Netlify Image CDN', () => {
test('Local images', async ({ page, outputExport }) => {
const nextImageResponsePromise = page.waitForResponse('**/_next/image**')

await page.goto(`${outputExport.url}/image/local`)

const nextImageResponse = await nextImageResponsePromise
expect(nextImageResponse.request().url()).toContain('_next/image?url=%2Fsquirrel.jpg')

expect(nextImageResponse.status()).toBe(200)
// ensure next/image is using Image CDN
// source image is jpg, but when requesting it through Image CDN avif will be returned
expect(await nextImageResponse.headerValue('content-type')).toEqual('image/avif')

await expectImageWasLoaded(page.locator('img'))
})
})
10 changes: 10 additions & 0 deletions tests/fixtures/output-export/app/image/local/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Image from 'next/image'

export default function NextImageUsingNetlifyImageCDN() {
return (
<main>
<h1>Next/Image + Netlify Image CDN</h1>
<Image src="/squirrel.jpg" alt="a cute squirrel (next/image)" width={300} height={278} />
</main>
)
}

0 comments on commit b000c43

Please sign in to comment.