-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix image optimization support for Next 14.1.1 (#377)
* Move image optimization to plugin * Refactor image optimization code * Added image optimization plugin for 14.1.1 * Fix image optimization plugin * Add changeset * Revert default sharp version to 0.32.6 * e2e test for image optimization * change one of the test to use an external image --------- Co-authored-by: Dorseuil Nicolas <[email protected]>
- Loading branch information
1 parent
3deb202
commit af2d3ce
Showing
14 changed files
with
204 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"open-next": patch | ||
--- | ||
|
||
Fix Image Optimization Support for [email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Image from "next/image"; | ||
|
||
export default function ImageOptimization() { | ||
return ( | ||
<div> | ||
<Image | ||
src="/static/corporate_holiday_card.jpg" | ||
alt="Corporate Holiday Card" | ||
width={300} | ||
height={300} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Image from "next/image"; | ||
|
||
export default function ImageOptimization() { | ||
return ( | ||
<div> | ||
<Image | ||
src="https://open-next.js.org/architecture.png" | ||
alt="Open Next architecture" | ||
width={300} | ||
height={300} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/open-next/src/adapters/plugins/image-optimization.replacement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { IncomingMessage, ServerResponse } from "node:http"; | ||
|
||
import type { APIGatewayProxyEventHeaders } from "aws-lambda"; | ||
import type { NextConfig } from "next/dist/server/config-shared"; | ||
//#override imports | ||
import { | ||
// @ts-ignore | ||
fetchExternalImage, | ||
// @ts-ignore | ||
fetchInternalImage, | ||
imageOptimizer, | ||
} from "next/dist/server/image-optimizer"; | ||
//#endOverride | ||
import type { NextUrlWithParsedQuery } from "next/dist/server/request-meta"; | ||
|
||
import { debug } from "../logger.js"; | ||
|
||
//#override optimizeImage | ||
export async function optimizeImage( | ||
headers: APIGatewayProxyEventHeaders, | ||
imageParams: any, | ||
nextConfig: NextConfig, | ||
handleRequest: ( | ||
newReq: IncomingMessage, | ||
newRes: ServerResponse, | ||
newParsedUrl?: NextUrlWithParsedQuery, | ||
) => Promise<void>, | ||
) { | ||
const { isAbsolute, href } = imageParams; | ||
|
||
const imageUpstream = isAbsolute | ||
? await fetchExternalImage(href) | ||
: await fetchInternalImage( | ||
href, | ||
// @ts-ignore | ||
{ headers }, | ||
{}, // res object is not necessary as it's not actually used. | ||
handleRequest, | ||
); | ||
|
||
// @ts-ignore | ||
const result = await imageOptimizer( | ||
imageUpstream, | ||
imageParams, | ||
nextConfig, | ||
false, // not in dev mode | ||
); | ||
debug("optimized result", result); | ||
return result; | ||
} | ||
//#endOverride |
35 changes: 35 additions & 0 deletions
35
packages/open-next/src/adapters/plugins/image-optimization.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { IncomingMessage, ServerResponse } from "node:http"; | ||
|
||
import { APIGatewayProxyEventHeaders } from "aws-lambda"; | ||
import { NextConfig } from "next/dist/server/config-shared"; | ||
//#override imports | ||
import { imageOptimizer } from "next/dist/server/image-optimizer"; | ||
//#endOverride | ||
import { NextUrlWithParsedQuery } from "next/dist/server/request-meta"; | ||
|
||
import { debug } from "../logger.js"; | ||
|
||
//#override optimizeImage | ||
export async function optimizeImage( | ||
headers: APIGatewayProxyEventHeaders, | ||
imageParams: any, | ||
nextConfig: NextConfig, | ||
handleRequest: ( | ||
newReq: IncomingMessage, | ||
newRes: ServerResponse, | ||
newParsedUrl: NextUrlWithParsedQuery, | ||
) => Promise<void>, | ||
) { | ||
const result = await imageOptimizer( | ||
// @ts-ignore | ||
{ headers }, | ||
{}, // res object is not necessary as it's not actually used. | ||
imageParams, | ||
nextConfig, | ||
false, // not in dev mode | ||
handleRequest, | ||
); | ||
debug("optimized result", result); | ||
return result; | ||
} | ||
//#endOverride |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/tests-e2e/tests/appPagesRouter/image-optimization.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { expect, test } from "@playwright/test"; | ||
|
||
test("Image Optimization", async ({ page }) => { | ||
await page.goto("/"); | ||
|
||
const imageResponsePromise = page.waitForResponse( | ||
/corporate_holiday_card.jpg/, | ||
); | ||
await page.locator('[href="/image-optimization"]').click(); | ||
const imageResponse = await imageResponsePromise; | ||
|
||
await page.waitForURL("/image-optimization"); | ||
|
||
const imageContentType = imageResponse.headers()["content-type"]; | ||
expect(imageContentType).toBe("image/webp"); | ||
|
||
let el = page.locator("img"); | ||
await expect(el).toHaveJSProperty("complete", true); | ||
await expect(el).not.toHaveJSProperty("naturalWidth", 0); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/tests-e2e/tests/appRouter/image-optimization.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { expect, test } from "@playwright/test"; | ||
|
||
test("Image Optimization", async ({ page }) => { | ||
await page.goto("/"); | ||
|
||
const imageResponsePromise = page.waitForResponse( | ||
/https%3A%2F%2Fopen-next.js.org%2Farchitecture.png/, | ||
); | ||
await page.locator('[href="/image-optimization"]').click(); | ||
const imageResponse = await imageResponsePromise; | ||
|
||
await page.waitForURL("/image-optimization"); | ||
|
||
const imageContentType = imageResponse.headers()["content-type"]; | ||
expect(imageContentType).toBe("image/webp"); | ||
|
||
let el = page.locator("img"); | ||
await expect(el).toHaveJSProperty("complete", true); | ||
await expect(el).not.toHaveJSProperty("naturalWidth", 0); | ||
}); |