Skip to content

Commit 1d92054

Browse files
Removed the OGScraper
1 parent 1c7a09b commit 1d92054

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"imports":{"open-graph-scraper":"https://raw.githubusercontent.com/jshemas/openGraphScraper/master/dist/index.d.ts","netlify:edge":"https://edge.netlify.com/v1/index.ts"},"scopes":{}}
1+
{"imports":{"netlify:edge":"https://edge.netlify.com/v1/index.ts"},"scopes":{}}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
22
"imports": {
3-
"open-graph-scraper": "https://raw.githubusercontent.com/jshemas/openGraphScraper/master/dist/index.d.ts"
43
}
54
}

.netlify/edge-functions/og-image.js

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
1-
import ogs from "https://esm.sh/[email protected]/";
1+
// Simple Open Graph scraper implementation for Deno/Edge Functions
2+
async function extractOgImage(url) {
3+
try {
4+
const response = await fetch(url, {
5+
redirect: 'follow',
6+
headers: {
7+
'Accept': 'text/html',
8+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
9+
}
10+
});
11+
12+
if (!response.ok) {
13+
return false;
14+
}
15+
16+
const html = await response.text();
17+
18+
// Extract og:image using regex (simple approach for edge functions)
19+
const ogImageMatch = html.match(/<meta\s+property=["\']og:image["\']\s+content=["\']([^"\']+)["\'][^>]*>/i);
20+
if (ogImageMatch && ogImageMatch[1]) {
21+
return ogImageMatch[1];
22+
}
23+
24+
// Fallback to other image meta tags
25+
const twitterImageMatch = html.match(/<meta\s+name=["\']twitter:image["\']\s+content=["\']([^"\']+)["\'][^>]*>/i);
26+
if (twitterImageMatch && twitterImageMatch[1]) {
27+
return twitterImageMatch[1];
28+
}
29+
30+
return false;
31+
} catch (error) {
32+
console.error('Error extracting OG image:', error);
33+
return false;
34+
}
35+
}
236

337
export default async function ogImage( req ) {
438

@@ -10,28 +44,10 @@ export default async function ogImage( req ) {
1044
return new Response( '', { status: '511' } );
1145
}
1246

13-
url = url.searchParams.get( 'url' );
14-
const fetchOptions = {
15-
redirect: 'follow',
16-
headers: {
17-
'Accept': 'text/html',
18-
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
19-
}
20-
};
47+
const targetUrl = url.searchParams.get( 'url' );
2148

22-
let image = false;
23-
ogs( { url, fetchOptions } )
24-
.then( data => {
25-
const { error, result, response } = data;
26-
if ( !error && result.success ) {
27-
image = result.ogImage[0].url;
28-
}
29-
else
30-
{
31-
console.log( 'Failed Response:', response );
32-
}
33-
});
34-
console.log( image );
49+
const image = await extractOgImage(targetUrl);
50+
console.log( 'Extracted image:', image );
3551

3652
return new Response( JSON.stringify( { image } ) );
3753
}

0 commit comments

Comments
 (0)