fix: serve Facebook images through path-based proxy URL
Deploy React Site / build-and-deploy (push) Successful in 11s

nginx does not percent-decode $arg_ variables, so pass the encoded image URL in the path (nginx decodes $uri). Mirror the Meta CDN whitelist in the Vite dev middleware.
This commit is contained in:
2026-10-19 21:34:16 +02:00
parent 61c9f8d614
commit 97dd2f87f1
2 changed files with 16 additions and 4 deletions
+15 -3
View File
@@ -20,9 +20,15 @@ export default defineConfig(({ mode }) => {
configureServer(server) {
server.middlewares.use("/api/proxy-image", async (req, res) => {
try {
// req.url contient uniquement la partie après /api/proxy-image (ex: /?url=...)
const urlObj = new URL(req.url || "", `http://${req.headers.host}`);
const targetUrl = urlObj.searchParams.get("url");
// req.url contient uniquement la partie après /api/proxy-image,
// l'URL cible y est encodée (ex: /https%3A%2F%2Fscontent...).
const raw = (req.url || "").replace(/^\//, "");
const targetUrl = decodeURIComponent(raw);
const allowed =
/^https?:\/\/[a-z0-9.-]+\.(fbcdn\.net|fbsbx\.com|facebook\.com)([/?]|$)/i.test(
targetUrl,
);
if (!targetUrl) {
res.statusCode = 400;
@@ -30,6 +36,12 @@ export default defineConfig(({ mode }) => {
return;
}
if (!allowed) {
res.statusCode = 403;
res.end("Forbidden");
return;
}
// On récupère l'image avec un User-Agent "normal" pour éviter les blocages 403/404
const response = await fetch(targetUrl, {
headers: {