Use R2 for public image assets

This commit is contained in:
2026-04-15 11:10:44 -04:00
parent 21c82882f4
commit ff108963a4
6 changed files with 87 additions and 9 deletions

View File

@@ -1,8 +1,40 @@
import type { NextConfig } from "next";
function getR2PublicUrl(): URL | null {
const publicUrl = process.env.NEXT_PUBLIC_R2_PUBLIC_URL?.trim();
if (!publicUrl) {
return null;
}
try {
const url = new URL(publicUrl);
if (url.protocol !== "https:" && url.protocol !== "http:") {
return null;
}
return url;
} catch {
return null;
}
}
const r2PublicUrl = getR2PublicUrl();
const nextConfig: NextConfig = {
images: {
remotePatterns: [],
remotePatterns: r2PublicUrl
? [
{
protocol: r2PublicUrl.protocol.replace(":", "") as "http" | "https",
hostname: r2PublicUrl.hostname,
port: r2PublicUrl.port,
pathname: "/**",
},
]
: [],
unoptimized: Boolean(r2PublicUrl),
},
};