Skip to content

Commit af2d3ce

Browse files
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]>
1 parent 3deb202 commit af2d3ce

File tree

14 files changed

+204
-23
lines changed

14 files changed

+204
-23
lines changed

.changeset/old-islands-invite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
Fix Image Optimization Support for [email protected]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Image from "next/image";
2+
3+
export default function ImageOptimization() {
4+
return (
5+
<div>
6+
<Image
7+
src="/static/corporate_holiday_card.jpg"
8+
alt="Corporate Holiday Card"
9+
width={300}
10+
height={300}
11+
/>
12+
</div>
13+
);
14+
}

examples/app-pages-router/app/page.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export default function Home() {
3333
<Nav href={"/parallel"} title="Parallel">
3434
Parallel routing
3535
</Nav>
36+
<Nav href={"/image-optimization"} title="Image Optimization">
37+
Image Optimization with next/image
38+
</Nav>
3639
</main>
3740
<h1>Pages Router</h1>
3841
<main className="grid grid-cols-2 gap-4 p-10 [&>a]:border">
117 KB
Loading
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Image from "next/image";
2+
3+
export default function ImageOptimization() {
4+
return (
5+
<div>
6+
<Image
7+
src="https://open-next.js.org/architecture.png"
8+
alt="Open Next architecture"
9+
width={300}
10+
height={300}
11+
/>
12+
</div>
13+
);
14+
}

examples/app-router/app/page.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ export default function Home() {
4444
<Nav href={"/sse"} title="Server Sent Events">
4545
Server Sent Events via Streaming
4646
</Nav>
47+
<Nav href={"/image-optimization"} title="Image Optimization">
48+
Image Optimization with next/image
49+
</Nav>
4750
</main>
4851
</>
4952
);

examples/app-router/next.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ const nextConfig = {
88
experimental: {
99
serverActions: true,
1010
},
11+
images: {
12+
remotePatterns: [
13+
{
14+
protocol: "https",
15+
hostname: "open-next.js.org",
16+
},
17+
],
18+
},
1119
redirects: () => {
1220
return [
1321
{
117 KB
Loading

packages/open-next/src/adapters/image-optimization-adapter.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {
1414
// @ts-ignore
1515
import { defaultConfig } from "next/dist/server/config-shared";
1616
import {
17-
imageOptimizer,
1817
ImageOptimizerCache,
1918
// @ts-ignore
2019
} from "next/dist/server/image-optimizer";
@@ -23,6 +22,7 @@ import type { NextUrlWithParsedQuery } from "next/dist/server/request-meta";
2322

2423
import { loadConfig } from "./config/util.js";
2524
import { awsLogger, debug, error } from "./logger.js";
25+
import { optimizeImage } from "./plugins/image-optimization.js";
2626
import { setNodeEnv } from "./util.js";
2727

2828
// Expected environment variables
@@ -64,7 +64,12 @@ export async function handler(
6464
headers,
6565
queryString === null ? undefined : queryString,
6666
);
67-
const result = await optimizeImage(headers, imageParams);
67+
const result = await optimizeImage(
68+
headers,
69+
imageParams,
70+
nextConfig,
71+
downloadHandler,
72+
);
6873

6974
return buildSuccessResponse(result);
7075
} catch (e: any) {
@@ -110,23 +115,6 @@ function validateImageParams(
110115
return imageParams;
111116
}
112117

113-
async function optimizeImage(
114-
headers: APIGatewayProxyEventHeaders,
115-
imageParams: any,
116-
) {
117-
const result = await imageOptimizer(
118-
// @ts-ignore
119-
{ headers },
120-
{}, // res object is not necessary as it's not actually used.
121-
imageParams,
122-
nextConfig,
123-
false, // not in dev mode
124-
downloadHandler,
125-
);
126-
debug("optimized result", result);
127-
return result;
128-
}
129-
130118
function buildSuccessResponse(result: any) {
131119
return {
132120
statusCode: 200,
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { IncomingMessage, ServerResponse } from "node:http";
2+
3+
import type { APIGatewayProxyEventHeaders } from "aws-lambda";
4+
import type { NextConfig } from "next/dist/server/config-shared";
5+
//#override imports
6+
import {
7+
// @ts-ignore
8+
fetchExternalImage,
9+
// @ts-ignore
10+
fetchInternalImage,
11+
imageOptimizer,
12+
} from "next/dist/server/image-optimizer";
13+
//#endOverride
14+
import type { NextUrlWithParsedQuery } from "next/dist/server/request-meta";
15+
16+
import { debug } from "../logger.js";
17+
18+
//#override optimizeImage
19+
export async function optimizeImage(
20+
headers: APIGatewayProxyEventHeaders,
21+
imageParams: any,
22+
nextConfig: NextConfig,
23+
handleRequest: (
24+
newReq: IncomingMessage,
25+
newRes: ServerResponse,
26+
newParsedUrl?: NextUrlWithParsedQuery,
27+
) => Promise<void>,
28+
) {
29+
const { isAbsolute, href } = imageParams;
30+
31+
const imageUpstream = isAbsolute
32+
? await fetchExternalImage(href)
33+
: await fetchInternalImage(
34+
href,
35+
// @ts-ignore
36+
{ headers },
37+
{}, // res object is not necessary as it's not actually used.
38+
handleRequest,
39+
);
40+
41+
// @ts-ignore
42+
const result = await imageOptimizer(
43+
imageUpstream,
44+
imageParams,
45+
nextConfig,
46+
false, // not in dev mode
47+
);
48+
debug("optimized result", result);
49+
return result;
50+
}
51+
//#endOverride

0 commit comments

Comments
 (0)