mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-09-18 19:41:48 +02:00
Only fetch open graph metadata for html urls
This commit is contained in:
5
.changeset/real-dancers-punch.md
Normal file
5
.changeset/real-dancers-punch.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"nostrudel": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Only fetch open graph metadata for html urls
|
@@ -15,7 +15,7 @@ export default function OpenGraphCard({ url, ...props }: { url: URL } & Omit<Car
|
|||||||
return (
|
return (
|
||||||
<LinkBox borderRadius="lg" borderWidth={1} overflow="hidden" {...props}>
|
<LinkBox borderRadius="lg" borderWidth={1} overflow="hidden" {...props}>
|
||||||
{data.ogImage?.map((ogImage) => (
|
{data.ogImage?.map((ogImage) => (
|
||||||
<Image src={ogImage.url} mx="auto" />
|
<Image key={ogImage.url} src={ogImage.url} mx="auto" />
|
||||||
))}
|
))}
|
||||||
|
|
||||||
<Box m="2" mt="4">
|
<Box m="2" mt="4">
|
||||||
|
@@ -2,11 +2,22 @@ import { useAsync } from "react-use";
|
|||||||
import extractMetaTags from "../lib/open-graph-scraper/extract";
|
import extractMetaTags from "../lib/open-graph-scraper/extract";
|
||||||
import { fetchWithCorsFallback } from "../helpers/cors";
|
import { fetchWithCorsFallback } from "../helpers/cors";
|
||||||
|
|
||||||
|
const pageExtensions = [".html", ".php", "htm"];
|
||||||
|
|
||||||
export default function useOpenGraphData(url: URL) {
|
export default function useOpenGraphData(url: URL) {
|
||||||
return useAsync(async () => {
|
return useAsync(async () => {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const ext = url.pathname.match(/\.[\w+d]+$/)?.[0];
|
||||||
|
if (ext && !pageExtensions.includes(ext)) return null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const html = await fetchWithCorsFallback(url).then((res) => res.text());
|
const res = await fetchWithCorsFallback(url, { signal: controller.signal });
|
||||||
|
const contentType = res.headers.get("content-type");
|
||||||
|
|
||||||
|
if (contentType?.includes("text/html")) {
|
||||||
|
const html = await res.text();
|
||||||
return extractMetaTags(html);
|
return extractMetaTags(html);
|
||||||
|
} else controller.abort();
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
return null;
|
return null;
|
||||||
}, [url.toString()]);
|
}, [url.toString()]);
|
||||||
|
Reference in New Issue
Block a user