From 7b985c89776a3f480b3061ce77a05758480da0dc Mon Sep 17 00:00:00 2001 From: Vedant terse Date: Mon, 20 Oct 2025 22:34:39 +0530 Subject: [PATCH 1/2] fix: remove hardcoded R2 public URL and use env variable instead --- .env.example | 1 + lib/upload-image.ts | 2 +- next.config.ts | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 08562072..03541fec 100644 --- a/.env.example +++ b/.env.example @@ -9,6 +9,7 @@ R2_UPLOAD_IMAGE_ACCESS_KEY_ID= R2_UPLOAD_IMAGE_SECRET_ACCESS_KEY= CLOUDFLARE_ACCOUNT_ID= R2_UPLOAD_IMAGE_BUCKET_NAME=images +R2_PUBLIC_URL= # Auth BETTER_AUTH_SECRET= diff --git a/lib/upload-image.ts b/lib/upload-image.ts index d02e0f16..b22196c4 100644 --- a/lib/upload-image.ts +++ b/lib/upload-image.ts @@ -23,6 +23,6 @@ export const uploadImageAssets = async (buffer: Buffer, key: string) => { }) ); - const publicUrl = `https://pub-6f0cf05705c7412b93a792350f3b3aa5.r2.dev/${key}`; + const publicUrl = `${process.env.R2_PUBLIC_URL}/${key}`; return publicUrl; }; diff --git a/next.config.ts b/next.config.ts index 35aa4664..db1ee665 100644 --- a/next.config.ts +++ b/next.config.ts @@ -10,7 +10,7 @@ const nextConfig: NextConfig = { remotePatterns: [ { protocol: "https", - hostname: "pub-6f0cf05705c7412b93a792350f3b3aa5.r2.dev", + hostname: process.env.R2_PUBLIC_URL?.replace(/^https?:\/\//, "") || "", }, { protocol: "https", From f6e78c094f0ff8a1c9f80e8c8122428f3a8a7b33 Mon Sep 17 00:00:00 2001 From: Michael Shimeles <69605071+michaelshimeles@users.noreply.github.com> Date: Wed, 10 Dec 2025 14:54:34 -0500 Subject: [PATCH 2/2] Update lib/upload-image.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- lib/upload-image.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/upload-image.ts b/lib/upload-image.ts index b22196c4..8c7bb48f 100644 --- a/lib/upload-image.ts +++ b/lib/upload-image.ts @@ -23,6 +23,12 @@ export const uploadImageAssets = async (buffer: Buffer, key: string) => { }) ); - const publicUrl = `${process.env.R2_PUBLIC_URL}/${key}`; + const baseUrl = process.env.R2_PUBLIC_URL; + if (!baseUrl) { + throw new Error("R2_PUBLIC_URL environment variable is not configured"); + } + const normalizedBase = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl; + const publicUrl = `${normalizedBase}/${key}`; + return publicUrl; return publicUrl; };