Skip to content

Commit eba754f

Browse files
committed
fix: fetch image fallback
1 parent 93c6f33 commit eba754f

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/components/App.tsx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,24 @@ const App: FC = () => {
2525
const imageRef = useRef<HTMLImageElement>(null);
2626

2727
const fetchImage = async (url: string) => {
28-
const res = await fetch(`https://cors-anywhere.herokuapp.com/${url}`);
29-
const html = await res.text();
28+
try {
29+
const res = await fetch(`https://cors-anywhere.herokuapp.com/${url}`);
30+
const html = await res.text();
3031

31-
const parser = new DOMParser();
32-
const doc = parser.parseFromString(html, "text/html");
33-
const node = doc.head.querySelector("meta[property='og:image']");
32+
const parser = new DOMParser();
33+
const doc = parser.parseFromString(html, "text/html");
34+
const node = doc.head.querySelector("meta[property='og:image']");
3435

35-
loadImage(node?.getAttribute("content") ?? url);
36+
if (node) {
37+
const content = node.getAttribute("content");
38+
39+
if (content) {
40+
url = content;
41+
}
42+
}
43+
} catch {} // eslint-disable-line no-empty
44+
45+
loadImage(url);
3646
};
3747

3848
const loadImage = async (src: string) => {

0 commit comments

Comments
 (0)