-
Notifications
You must be signed in to change notification settings - Fork 135
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
Fix image optimization support for Next 14.1.1 #377
Merged
conico974
merged 8 commits into
opennextjs:main
from
chungweileong94:fix-image-optimization
Mar 6, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
402a6b3
Move image optimization to plugin
chungweileong94 bb3dfb8
Refactor image optimization code
chungweileong94 df43cf7
Added image optimization plugin for 14.1.1
chungweileong94 f799925
Fix image optimization plugin
chungweileong94 d546a6b
Add changeset
chungweileong94 b1bc2fa
Revert default sharp version to 0.32.6
chungweileong94 8336ef5
e2e test for image optimization
chungweileong94 98db0fc
change one of the test to use an external image
conico974 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }) => { | ||
conico974 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably don't want to apply the changeset now. We usually let the PR merge into main, run the e2e, and once that passes, we do a separate changeset PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be fine actually, changesets get merged before release and the changebot don't release until we merge the version packages PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh right, I forgot.