-
Notifications
You must be signed in to change notification settings - Fork 754
Add configurable R2 public URL for image uploads #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add configurable R2 public URL for image uploads #64
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| ...(r2Hostname | ||
| ? [ | ||
| { | ||
| protocol: "https" as const, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Protocol is hardcoded to https but R2_PUBLIC_URL may specify http://. This creates a mismatch where upload-image.ts generates URLs with the actual protocol from R2_PUBLIC_URL, but Next.js Image will only allow HTTPS.
For example, if R2_PUBLIC_URL=http://localhost:9000, uploads will generate http://localhost:9000/image.jpg but Next.js will reject it because the remote pattern only allows https://localhost:9000.
Fix by extracting the protocol:
const getR2Config = (): { protocol: 'http' | 'https'; hostname: string } | null => {
const r2PublicUrl = process.env.R2_PUBLIC_URL;
if (!r2PublicUrl) return null;
try {
const urlWithProtocol = r2PublicUrl.startsWith("http")
? r2PublicUrl
: `https://${r2PublicUrl}`;
const url = new URL(urlWithProtocol);
return {
protocol: url.protocol.replace(':', '') as 'http' | 'https',
hostname: url.hostname
};
} catch {
console.warn(`Invalid R2_PUBLIC_URL: ${r2PublicUrl}`);
return null;
}
};
const r2Config = getR2Config();
// Then use:
protocol: r2Config.protocol,
hostname: r2Config.hostname,| protocol: "https" as const, | |
| protocol: r2Config?.protocol || "https" as const, |
Spotted by Graphite Agent
Is this helpful? React 👍 or 👎 to let us know.
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
|||||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||
User description
This PR was created by a Graphite background agent: https://app.graphite.com/background-agents/michaelshimeles/nextjs-starter-kit/task/bgt_01kc4x8pcbeg6spy6zpfarthnb
PR Type
Enhancement
Description
Replace hardcoded R2 URL with configurable environment variable
Add URL validation and normalization in upload function
Extract hostname from R2_PUBLIC_URL for Next.js image config
Make remote image patterns conditional based on configuration
Diagram Walkthrough
File Walkthrough
upload-image.ts
Make R2 URL configurable with validationlib/upload-image.ts
R2_PUBLIC_URLenvironmentvariable
public URL
next.config.ts
Extract and conditionally configure R2 hostnamenext.config.ts
getR2Hostname()function to extract hostname fromR2_PUBLIC_URLhttps://if neededhostname availability
.env.example
Document R2_PUBLIC_URL configuration variable.env.example
R2_PUBLIC_URLenvironment variable with documentation commenthttps://your-bucket.r2.dev)