load fast
Build and Deploy Docker Images / build_and_deploy (push) Has been cancelled

This commit is contained in:
hamid zarghami
2026-06-06 09:08:38 +03:30
parent ace6a0b288
commit e68543f869
12 changed files with 255 additions and 248 deletions
+11 -13
View File
@@ -1,17 +1,20 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { fetchWithTimeout } from "@/lib/helpers/fetchWithTimeout";
import { NextRequest, NextResponse } from "next/server";
import sharp from "sharp";
export const dynamic = "force-dynamic";
export const revalidate = 0;
const FETCH_TIMEOUT_MS = 5000;
const DEFAULT_ICON = "/icons/web-app-manifest-192x192.png";
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ name: string }> },
) {
try {
const { name } = await params;
console.log("name", name);
await params;
const searchParams = request.nextUrl.searchParams;
const logoUrl = searchParams.get("url");
@@ -21,29 +24,27 @@ export async function GET(
return new NextResponse("Logo URL is required", { status: 400 });
}
// دانلود تصویر
const imageResponse = await fetch(logoUrl);
const imageResponse = await fetchWithTimeout(logoUrl, {
timeoutMs: FETCH_TIMEOUT_MS,
});
if (!imageResponse.ok) {
return new NextResponse("Failed to fetch logo", { status: 404 });
return NextResponse.redirect(new URL(DEFAULT_ICON, request.url), 302);
}
const imageBuffer = Buffer.from(await imageResponse.arrayBuffer());
// دریافت ابعاد تصویر
const metadata = await sharp(imageBuffer).metadata();
const { width, height } = metadata;
if (!width || !height) {
return new NextResponse("Invalid image", { status: 400 });
return NextResponse.redirect(new URL(DEFAULT_ICON, request.url), 302);
}
// اگر تصویر از قبل مربع باشد، مستقیماً resize می‌کنیم
if (width === height) {
const processedImage = sharp(imageBuffer).resize(size, size, {
fit: "contain",
background: { r: 0, g: 0, b: 0, alpha: 0 },
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const outputBuffer: any = await processedImage.png().toBuffer();
return new NextResponse(outputBuffer, {
@@ -54,13 +55,11 @@ export async function GET(
});
}
// محاسبه نسبت برای resize اولیه (برای بهینه‌سازی)
const maxDimension = Math.max(width, height);
const scale = size / maxDimension;
const newWidth = Math.round(width * scale);
const newHeight = Math.round(height * scale);
// ابتدا resize می‌کنیم و سپس padding اضافه می‌کنیم
const paddingTop = Math.floor((size - newHeight) / 2);
const paddingBottom = size - newHeight - paddingTop;
const paddingLeft = Math.floor((size - newWidth) / 2);
@@ -79,7 +78,6 @@ export async function GET(
background: { r: 0, g: 0, b: 0, alpha: 0 },
});
// تبدیل به PNG و برگرداندن
const outputBuffer: any = await processedImage.png().toBuffer();
return new NextResponse(outputBuffer, {
@@ -90,6 +88,6 @@ export async function GET(
});
} catch (error) {
console.error("Error processing logo:", error);
return new NextResponse("Internal Server Error", { status: 500 });
return NextResponse.redirect(new URL(DEFAULT_ICON, request.url), 302);
}
}
+3 -1
View File
@@ -1,3 +1,4 @@
import { fetchWithTimeout } from "@/lib/helpers/fetchWithTimeout";
import { NextRequest, NextResponse } from "next/server";
export const dynamic = "force-dynamic";
@@ -15,7 +16,8 @@ export async function GET(request: NextRequest) {
if (!["http:", "https:"].includes(parsed.protocol)) {
return NextResponse.json({ error: "Invalid URL" }, { status: 400 });
}
const res = await fetch(url, {
const res = await fetchWithTimeout(url, {
timeoutMs: 5000,
headers: { Accept: "image/svg+xml, text/xml, text/plain" },
});
if (!res.ok) {