From 60d8c95318197f17996fca6cf2d1a470373f275f Mon Sep 17 00:00:00 2001 From: Lewis Carhart Date: Sat, 21 Jun 2025 16:54:06 +0100 Subject: [PATCH 1/3] feat: add logo existence check to API endpoint for cached websites --- apps/trust/src/app/api/get-sites/route.tsx | 29 +++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/apps/trust/src/app/api/get-sites/route.tsx b/apps/trust/src/app/api/get-sites/route.tsx index 3ddcea9e0b..9c5126c1e7 100644 --- a/apps/trust/src/app/api/get-sites/route.tsx +++ b/apps/trust/src/app/api/get-sites/route.tsx @@ -2,6 +2,16 @@ import { db } from '@comp/db'; import { NextRequest, NextResponse } from 'next/server'; import { cache } from 'react'; +async function checkLogoExists(url: string): Promise { + try { + const response = await fetch(url); + return response.status !== 404; + } catch (error) { + console.error(`Error checking logo for ${url}:`, error); + return false; + } +} + const getCachedSites = cache(async () => { const sites = await db.trust.findMany({ where: { @@ -23,15 +33,28 @@ const getCachedSites = cache(async () => { }, }); - return websites.map((website) => website.website); + const logoUrls = websites + .map((website) => website.website) + .filter((website): website is string => Boolean(website)) + .map((website) => { + const domain = website.replace(/^https?:\/\//, '').replace(/^www\./, ''); + return `https://img.logo.dev/${domain}?token=pk_QtKMUKc7QOKZmbzLG3Q8NQ&retina=true&fallback=404`; + }); + + const validLogos = await Promise.all( + logoUrls.map(async (url) => { + const exists = await checkLogoExists(url); + return exists ? url : null; + }), + ); + + return validLogos.filter((url): url is string => url !== null); }); export async function GET(request: NextRequest) { try { const websites = await getCachedSites(); - console.log(websites); - const response = NextResponse.json(websites); response.headers.set('Cache-Control', 'public, s-maxage=3600, stale-while-revalidate=86400'); From 24ea750254b15a56b4fb40e6a4cc22579c642758 Mon Sep 17 00:00:00 2001 From: Lewis Carhart Date: Sat, 21 Jun 2025 17:13:00 +0100 Subject: [PATCH 2/3] refactor: remove logo existence check and return logo URLs directly in getCachedSites function --- apps/trust/src/app/api/get-sites/route.tsx | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/apps/trust/src/app/api/get-sites/route.tsx b/apps/trust/src/app/api/get-sites/route.tsx index 9c5126c1e7..ce9d8d5b50 100644 --- a/apps/trust/src/app/api/get-sites/route.tsx +++ b/apps/trust/src/app/api/get-sites/route.tsx @@ -2,16 +2,6 @@ import { db } from '@comp/db'; import { NextRequest, NextResponse } from 'next/server'; import { cache } from 'react'; -async function checkLogoExists(url: string): Promise { - try { - const response = await fetch(url); - return response.status !== 404; - } catch (error) { - console.error(`Error checking logo for ${url}:`, error); - return false; - } -} - const getCachedSites = cache(async () => { const sites = await db.trust.findMany({ where: { @@ -38,17 +28,10 @@ const getCachedSites = cache(async () => { .filter((website): website is string => Boolean(website)) .map((website) => { const domain = website.replace(/^https?:\/\//, '').replace(/^www\./, ''); - return `https://img.logo.dev/${domain}?token=pk_QtKMUKc7QOKZmbzLG3Q8NQ&retina=true&fallback=404`; + return `https://img.logo.dev/${domain}?token=pk_QtKMUKc7QOKZmbzLG3Q8NQ&retina=true4`; }); - const validLogos = await Promise.all( - logoUrls.map(async (url) => { - const exists = await checkLogoExists(url); - return exists ? url : null; - }), - ); - - return validLogos.filter((url): url is string => url !== null); + return logoUrls; }); export async function GET(request: NextRequest) { From 87bd99fe0426233ff2b8356b05a44e80c4becd79 Mon Sep 17 00:00:00 2001 From: Lewis Carhart Date: Sat, 21 Jun 2025 17:18:46 +0100 Subject: [PATCH 3/3] fix: correct URL formatting for logo retrieval in getCachedSites function --- apps/trust/src/app/api/get-sites/route.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/trust/src/app/api/get-sites/route.tsx b/apps/trust/src/app/api/get-sites/route.tsx index ce9d8d5b50..3cb6b62656 100644 --- a/apps/trust/src/app/api/get-sites/route.tsx +++ b/apps/trust/src/app/api/get-sites/route.tsx @@ -28,7 +28,7 @@ const getCachedSites = cache(async () => { .filter((website): website is string => Boolean(website)) .map((website) => { const domain = website.replace(/^https?:\/\//, '').replace(/^www\./, ''); - return `https://img.logo.dev/${domain}?token=pk_QtKMUKc7QOKZmbzLG3Q8NQ&retina=true4`; + return `https://img.logo.dev/${domain}?token=pk_QtKMUKc7QOKZmbzLG3Q8NQ&retina=true`; }); return logoUrls;