Skip to content

Commit e9dc621

Browse files
authored
Serve /cdn-cgi/image/... images in dev (#491)
1 parent 451d98b commit e9dc621

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+
// Note: "/cdn-cgi/image/..." requests do not reach production workers.
37+
if (url.pathname.startsWith("/cdn-cgi/image/")) {
38+
const m = url.pathname.match(/\/cdn-cgi\/image\/.+?\/(?<url>.+)$/);
39+
if (m === null) {
40+
return new Response("Not Found!", { status: 404 });
41+
}
42+
const imageUrl = m.groups!.url!;
43+
return imageUrl.match(/^https?:\/\//)
44+
? fetch(imageUrl, { cf: { cacheEverything: true } })
45+
: 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)