-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.ts
More file actions
45 lines (41 loc) · 1.33 KB
/
index.ts
File metadata and controls
45 lines (41 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Cloudflare Worker entry point with image optimization.
*
* For apps without image optimization, use vinext/server/app-router-entry
* directly in wrangler.jsonc: "main": "vinext/server/app-router-entry"
*/
import { handleImageOptimization } from "vinext/server/image-optimization";
import handler from "vinext/server/app-router-entry";
interface Env {
ASSETS: Fetcher;
IMAGES: {
input(stream: ReadableStream): {
transform(options: Record<string, unknown>): {
output(options: {
format: string;
quality: number;
}): Promise<{ response(): Response }>;
};
};
};
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
// Image optimization via Cloudflare Images binding
if (url.pathname === "/_vinext/image") {
return handleImageOptimization(request, {
fetchAsset: (path) =>
env.ASSETS.fetch(new Request(new URL(path, request.url))),
transformImage: async (body, { width, format, quality }) => {
const result = await env.IMAGES.input(body)
.transform(width > 0 ? { width } : {})
.output({ format, quality });
return result.response();
},
});
}
// Delegate everything else to vinext
return handler.fetch(request);
},
};