Skip to content

Commit

Permalink
Check if ogImage exists on remote server
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDaub committed Nov 1, 2024
1 parent a0413cf commit 4f66dd3
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ async function extractCanonicalLink(html) {
return DOMPurify.sanitize(node.href);
}

const checkOgImage = async (url) => {
const signal = AbortSignal.timeout(5000);
try {
const res = await fetch(url, {
agent: useAgent(url),
signal,
method: "HEAD",
headers: {
"User-Agent": env.USER_AGENT,
},
});
return res.ok;
} catch {
return false;
}
};

export const metadata = async (url) => {
let result, html;
if (cache.has(url)) {
Expand Down Expand Up @@ -116,7 +133,10 @@ export const metadata = async (url) => {
output.domain = DOMPurify.sanitize(domain);
}
if (image && image.startsWith("https://")) {
output.image = DOMPurify.sanitize(image);
const exists = await checkOgImage(image);
if (exists) {
output.image = DOMPurify.sanitize(image);
}
}
if (ogDescription) {
output.ogDescription = DOMPurify.sanitize(
Expand Down

0 comments on commit 4f66dd3

Please sign in to comment.