diff --git a/CHANGELOG.md b/CHANGELOG.md index 68d5922..5cb42ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +# 0.2.8.rc + +/favicon/[domain] return 404 svg icon + # 0.2.5.rc Fix https://github.com/seadfeng/favicon-downloader/issues/9 diff --git a/package.json b/package.json index 1d0b976..1007d9a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "favicon-downloader", - "version": "0.2.7.rc", + "version": "0.2.8.rc", "private": true, "scripts": { "dev": "next dev", diff --git a/src/app/api/favicon/[domain]/route.ts b/src/app/api/favicon/[domain]/route.ts index 6911f30..14795e0 100644 --- a/src/app/api/favicon/[domain]/route.ts +++ b/src/app/api/favicon/[domain]/route.ts @@ -9,8 +9,8 @@ export async function GET(request: NextRequest, { params }: { params: { domain: const { domain } = params; // Validate domain name format - if (!/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/.test(domain)) { - return new Response('Invalid domain name format', { status: 400 }); + if (!/([a-z0-9-]+\.)+[a-z0-9]{1,}$/.test(domain)) { + return new Response(`Invalid domain name format${domain}`, { status: 400 }); } // Define a helper function to handle the response diff --git a/src/app/favicon/[domain]/route.ts b/src/app/favicon/[domain]/route.ts index 3f37981..b874124 100644 --- a/src/app/favicon/[domain]/route.ts +++ b/src/app/favicon/[domain]/route.ts @@ -3,6 +3,7 @@ import { ResponseInfo } from '@/types'; import type { NextRequest } from 'next/server'; export const runtime = 'edge'; + export async function GET(request: NextRequest, { params: { domain } }: { params: { domain: string } }) { let icons: { sizes?: string; href: string }[] = []; const larger: boolean = request.nextUrl.searchParams.get('larger') === 'true'; // Get the 'larger' parameter @@ -14,11 +15,29 @@ export async function GET(request: NextRequest, { params: { domain } }: { params // Convert the domain to ASCII encoding using URL API const asciiDomain = new URL(`http://${domain}`).hostname; + const svg404 = () => { + const firstLetter = domain.charAt(0).toUpperCase(); + const svgContent = ` + + + ${firstLetter} + + `; + return new Response(svgContent, { + status: 404, + headers: { + 'Cache-Control': 'public, max-age=86400', + 'Content-Type': 'image/svg+xml' + } + }); + } + // Validate domain name format - if (!/^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/.test(asciiDomain)) { - return new Response('Invalid domain name format', { status: 400 }); + if (!/([a-z0-9-]+\.)+[a-z0-9]{1,}$/.test(asciiDomain)) { + return svg404(); } + if (larger) { const duckduckgoUrl = `https://icons.duckduckgo.com/ip3/${asciiDomain}.ico`; console.log("Ico source:", duckduckgoUrl); @@ -99,23 +118,7 @@ export async function GET(request: NextRequest, { params: { domain } }: { params // Calculate execution time const endTime = Date.now(); const executionTime = endTime - startTime; - if (!iconResponse.ok) { - const firstLetter = domain.charAt(0).toUpperCase(); - const svgContent = ` - - - ${firstLetter} - - `; - return new Response(svgContent, { - status: 404, - headers: { - 'Cache-Control': 'public, max-age=86400', - 'Content-Type': 'image/svg+xml', - 'X-Execution-Time': `${executionTime}ms` - } - }); - } + if (!iconResponse.ok) return svg404(); const iconBuffer = await iconResponse.arrayBuffer(); // Return the image response with execution time