Skip to content

Commit ad37fc1

Browse files
committed
Serve /cdn-cgi/image/... images in dev
1 parent 00f6071 commit ad37fc1

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

.changeset/wise-gifts-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
Serve `/cdn-cgi/image/...` images in dev

packages/cloudflare/src/cli/templates/worker.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ export default {
3232

3333
populateProcessEnv(url, env.NEXTJS_ENV);
3434

35+
// Serve images in development.
36+
if (url.pathname.startsWith("/cdn-cgi/image/")) {
37+
const m = url.pathname.match(/\/cdn-cgi\/image\/.+?\/(?<url>.+)$/);
38+
if (m === null) {
39+
return new Response("Not Found!", { status: 404 });
40+
}
41+
const imageUrl = m.groups!.url!;
42+
if (imageUrl.startsWith("http://") || imageUrl.startsWith("https://")) {
43+
return fetch(imageUrl, { cf: { cacheEverything: true } });
44+
}
45+
return env.ASSETS.fetch(new URL(`/${imageUrl}`, url));
46+
}
47+
48+
// Fallback for the Next default image loader.
3549
if (url.pathname === "/_next/image") {
3650
const imageUrl = url.searchParams.get("url") ?? "";
3751
return imageUrl.startsWith("/")

0 commit comments

Comments
 (0)