Skip to content

Commit 0e7f354

Browse files
authored
Merge pull request #953 from Merit-Systems/debug-null-metadata
fix: resolve relative OG image URLs in origin metadata upsert
2 parents 12cf0b1 + 27a0dc1 commit 0e7f354

3 files changed

Lines changed: 56 additions & 26 deletions

File tree

apps/scan/src/app/api/resources/sync/route.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,21 @@ export const GET = async (request: NextRequest) => {
180180
description: metadata?.description ?? og?.ogDescription,
181181
favicon: favicon ?? undefined,
182182
ogImages:
183-
og?.ogImage?.map(image => ({
184-
url: image.url,
185-
height: image.height,
186-
width: image.width,
187-
title: og.ogTitle,
188-
description: og.ogDescription,
189-
})) ?? [],
183+
og?.ogImage?.flatMap(image => {
184+
try {
185+
return [
186+
{
187+
url: new URL(image.url, origin).toString(),
188+
height: image.height,
189+
width: image.width,
190+
title: og.ogTitle,
191+
description: og.ogDescription,
192+
},
193+
];
194+
} catch {
195+
return [];
196+
}
197+
}) ?? [],
190198
};
191199

192200
// Upsert origin to database

apps/scan/src/lib/resources.ts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,21 @@ export async function registerSiwxResource(
258258
description: description ?? undefined,
259259
favicon: favicon ?? undefined,
260260
ogImages:
261-
og?.ogImage?.map(image => ({
262-
url: image.url,
263-
height: image.height,
264-
width: image.width,
265-
title: og.ogTitle,
266-
description: og.ogDescription,
267-
})) ?? [],
261+
og?.ogImage?.flatMap(image => {
262+
try {
263+
return [
264+
{
265+
url: new URL(image.url, origin).toString(),
266+
height: image.height,
267+
width: image.width,
268+
title: og.ogTitle,
269+
description: og.ogDescription,
270+
},
271+
];
272+
} catch {
273+
return [];
274+
}
275+
}) ?? [],
268276
});
269277
} catch (err) {
270278
console.error(
@@ -524,15 +532,31 @@ export const registerResource = async (
524532
description: description ?? undefined,
525533
favicon: favicon ?? undefined,
526534
ogImages:
527-
og?.ogImage?.map(image => ({
528-
url: image.url,
529-
height: image.height,
530-
width: image.width,
531-
title: og.ogTitle,
532-
description: og.ogDescription,
533-
})) ?? [],
534-
}).catch(() => {
535-
// P2002 or other race — another call already upserted this origin.
535+
og?.ogImage?.flatMap(image => {
536+
try {
537+
return [
538+
{
539+
url: new URL(image.url, origin).toString(),
540+
height: image.height,
541+
width: image.width,
542+
title: og.ogTitle,
543+
description: og.ogDescription,
544+
},
545+
];
546+
} catch {
547+
return [];
548+
}
549+
}) ?? [],
550+
}).catch(err => {
551+
// P2002: another concurrent call already upserted this origin — safe to ignore.
552+
// Log anything else so metadata failures aren't silent.
553+
const isP2002 =
554+
err instanceof Error &&
555+
'code' in err &&
556+
(err as { code: string }).code === 'P2002';
557+
if (!isP2002) {
558+
console.error('[registerResource] Origin metadata upsert failed:', err);
559+
}
536560
});
537561

538562
await upsertResourceResponse(

apps/scan/src/services/db/resources/resource.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,7 @@ export const deprecateStaleResources = async (
441441
return 0;
442442
}
443443

444-
const activeKeys = new Set(
445-
activeResources.map(r => `${r.method}::${r.url}`)
446-
);
444+
const activeKeys = new Set(activeResources.map(r => `${r.method}::${r.url}`));
447445
const staleIds = allResources
448446
.filter(r => !activeKeys.has(`${r.method}::${r.resource}`))
449447
.map(r => r.id);

0 commit comments

Comments
 (0)