Skip to content

Commit

Permalink
add iconResponse.ok
Browse files Browse the repository at this point in the history
  • Loading branch information
seadfeng committed Sep 3, 2024
1 parent 16582db commit af478fd
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/app/favicon/[domain]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,27 @@ export async function GET(request: NextRequest, { params: { domain } }: { params
});
}
const iconResponse = await fetch(selectedIcon.href, { headers });
const iconBuffer = await iconResponse.arrayBuffer();

// 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`
}
});
}
const iconBuffer = await iconResponse.arrayBuffer();

// Return the image response with execution time
return new Response(iconBuffer, {
Expand Down

0 comments on commit af478fd

Please # to comment.