Skip to content

Commit

Permalink
update 404 return
Browse files Browse the repository at this point in the history
  • Loading branch information
seadfeng committed Sep 5, 2024
1 parent f2d531f commit a0f27cf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "favicon-downloader",
"version": "0.2.7.rc",
"version": "0.2.8.rc",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/favicon/[domain]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 22 additions & 19 deletions src/app/favicon/[domain]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = `
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="#cccccc"/>
<text x="50%" y="50%" font-size="48" text-anchor="middle" dominant-baseline="middle" fill="#000000">${firstLetter}</text>
</svg>
`;
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);
Expand Down Expand Up @@ -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 = `
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="#cccccc"/>
<text x="50%" y="50%" font-size="48" text-anchor="middle" dominant-baseline="middle" fill="#000000">${firstLetter}</text>
</svg>
`;
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
Expand Down

0 comments on commit a0f27cf

Please sign in to comment.