-
Notifications
You must be signed in to change notification settings - Fork 744
fix: remove hardcoded R2 public URL and use env variable instead #58
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ const nextConfig: NextConfig = { | |
| remotePatterns: [ | ||
| { | ||
| protocol: "https", | ||
| hostname: "pub-6f0cf05705c7412b93a792350f3b3aa5.r2.dev", | ||
| hostname: process.env.R2_PUBLIC_URL?.replace(/^https?:\/\//, "") || "", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve hostname extraction and handle missing R2_PUBLIC_URL. The current implementation has several issues:
Apply this diff to properly parse the hostname using the URL API: +const getR2Hostname = () => {
+ const url = process.env.R2_PUBLIC_URL;
+ if (!url) {
+ console.warn("R2_PUBLIC_URL is not configured. R2 image optimization will not work.");
+ return undefined;
+ }
+ try {
+ // Ensure URL has protocol for parsing
+ const fullUrl = url.startsWith('http') ? url : `https://${url}`;
+ return new URL(fullUrl).hostname;
+ } catch (error) {
+ console.error("Invalid R2_PUBLIC_URL format:", url);
+ return undefined;
+ }
+};
+
const nextConfig: NextConfig = {
/* config options here */
reactStrictMode: false,
typescript: {
ignoreBuildErrors: true,
},
images: {
- remotePatterns: [
+ remotePatterns: [
+ ...(getR2Hostname() ? [{
- {
protocol: "https",
- hostname: process.env.R2_PUBLIC_URL?.replace(/^https?:\/\//, "") || "",
- },
+ hostname: getR2Hostname()!,
+ }] : []),
{
protocol: "https",
hostname: "jdj14ctwppwprnqu.public.blob.vercel-storage.com",This approach:
🤖 Prompt for AI Agents |
||
| }, | ||
| { | ||
| protocol: "https", | ||
|
|
||
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.
Add validation and normalization for R2_PUBLIC_URL.
The code doesn't validate that
R2_PUBLIC_URLis defined, which will result in URLs like"undefined/image.png"if the environment variable is missing. Additionally, if the URL has a trailing slash, the result will be malformed (e.g.,"https://example.com//key").Apply this diff to add validation and handle trailing slashes:
📝 Committable suggestion
🤖 Prompt for AI Agents