Skip to content

Commit 7db7055

Browse files
Add a redundancy to improve possibilities of Twitter/X taking the OG image (#57)
1 parent 4422c73 commit 7db7055

File tree

3 files changed

+56
-42
lines changed

3 files changed

+56
-42
lines changed

src/lib/server/metaTagsInjector.js

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ import fs from "fs";
33
import matter from "gray-matter";
44
import path from "path";
55

6+
/**
7+
* Verifies if an image exists and has appropriate dimensions for social media
8+
* @param {string} imagePath - Path to the image relative to static directory
9+
* @returns {object} - Object containing verified image path and dimensions
10+
*/
11+
function verifyImage(imagePath) {
12+
const defaultImage = "/assets/media/website_screenshot.png";
13+
const localImagePath = path.join(process.cwd(), "static", imagePath);
14+
15+
if (!fs.existsSync(localImagePath)) {
16+
console.warn(`Warning: Image not found at ${imagePath}. Using default.`);
17+
return {
18+
path: defaultImage,
19+
width: 1200,
20+
height: 630
21+
};
22+
}
23+
24+
return {
25+
path: imagePath,
26+
width: 1200, // Default OG image width
27+
height: 630 // Default OG image height
28+
};
29+
}
30+
631
/**
732
* Extracts metadata from a markdown file
833
* @param {string} slug - The slug/identifier for the blog post
@@ -167,27 +192,15 @@ export function injectMetaTags(html, url) {
167192
? `/assets/og/${slug}.png`
168193
: "/assets/media/website_screenshot.png";
169194

170-
// Verify image exists, fall back to default if not
171-
let finalImagePath = customOgImagePath;
172-
const localImagePath = path.join(
173-
process.cwd(),
174-
"static",
175-
customOgImagePath
176-
);
177-
178-
if (!fs.existsSync(localImagePath)) {
179-
console.warn(`Warning: OG image not found for ${slug}. Using default.`);
180-
finalImagePath = "/assets/media/website_screenshot.png";
181-
}
195+
// Verify image exists, get dimensions, fall back to default if needed
196+
const verifiedImage = verifyImage(customOgImagePath);
197+
const finalImagePath = verifiedImage.path;
182198

183199
// Make sure the image URL is absolute and doesn't have any special characters
184-
const absoluteImageUrl = `${siteUrl}${finalImagePath.replace(
185-
/\s/g,
186-
"%20"
187-
)}`;
200+
const absoluteImageUrl = new URL(finalImagePath, siteUrl).toString();
188201

189202
// Generate absolute URL for the post - ensure no trailing slash
190-
const absoluteUrl = `${siteUrl}/blog/${slug}`;
203+
const absoluteUrl = new URL(`/blog/${slug}`, siteUrl).toString();
191204

192205
// Create the meta tag string - with escaping for special characters
193206
// Enforce Twitter character limits
@@ -226,6 +239,7 @@ ${
226239
<meta name="twitter:title" content="${safeTitle}" />
227240
<meta name="twitter:description" content="${safeDescription}" />
228241
<meta name="twitter:image" content="${absoluteImageUrl}" />
242+
<meta property="twitter:image" content="${absoluteImageUrl}" />
229243
<meta name="twitter:image:alt" content="${safeTitle}" />
230244
<meta name="twitter:domain" content="${siteUrl.replace(/^https?:\/\//, "")}" />
231245
@@ -238,8 +252,8 @@ ${
238252
<meta property="og:image" content="${absoluteImageUrl}" />
239253
<meta property="og:image:secure_url" content="${absoluteImageUrl}" />
240254
<meta property="og:image:type" content="image/png" />
241-
<meta property="og:image:width" content="1200" />
242-
<meta property="og:image:height" content="630" />
255+
<meta property="og:image:width" content="${verifiedImage.width}" />
256+
<meta property="og:image:height" content="${verifiedImage.height}" />
243257
<meta property="og:locale" content="en_US" />
244258
${
245259
formattedDate

static/data/github-contributors.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,28 @@
483483
"site_admin": false,
484484
"contributions": 98
485485
},
486+
{
487+
"login": "jsbautista",
488+
"id": 42411448,
489+
"node_id": "MDQ6VXNlcjQyNDExNDQ4",
490+
"avatar_url": "https://avatars.githubusercontent.com/u/42411448?v=4",
491+
"gravatar_id": "",
492+
"url": "https://api.github.com/users/jsbautista",
493+
"html_url": "https://github.com/jsbautista",
494+
"followers_url": "https://api.github.com/users/jsbautista/followers",
495+
"following_url": "https://api.github.com/users/jsbautista/following{/other_user}",
496+
"gists_url": "https://api.github.com/users/jsbautista/gists{/gist_id}",
497+
"starred_url": "https://api.github.com/users/jsbautista/starred{/owner}{/repo}",
498+
"subscriptions_url": "https://api.github.com/users/jsbautista/subscriptions",
499+
"organizations_url": "https://api.github.com/users/jsbautista/orgs",
500+
"repos_url": "https://api.github.com/users/jsbautista/repos",
501+
"events_url": "https://api.github.com/users/jsbautista/events{/privacy}",
502+
"received_events_url": "https://api.github.com/users/jsbautista/received_events",
503+
"type": "User",
504+
"user_view_type": "public",
505+
"site_admin": false,
506+
"contributions": 94
507+
},
486508
{
487509
"login": "dhoegh",
488510
"id": 7973946,
@@ -505,28 +527,6 @@
505527
"site_admin": false,
506528
"contributions": 93
507529
},
508-
{
509-
"login": "jsbautista",
510-
"id": 42411448,
511-
"node_id": "MDQ6VXNlcjQyNDExNDQ4",
512-
"avatar_url": "https://avatars.githubusercontent.com/u/42411448?v=4",
513-
"gravatar_id": "",
514-
"url": "https://api.github.com/users/jsbautista",
515-
"html_url": "https://github.com/jsbautista",
516-
"followers_url": "https://api.github.com/users/jsbautista/followers",
517-
"following_url": "https://api.github.com/users/jsbautista/following{/other_user}",
518-
"gists_url": "https://api.github.com/users/jsbautista/gists{/gist_id}",
519-
"starred_url": "https://api.github.com/users/jsbautista/starred{/owner}{/repo}",
520-
"subscriptions_url": "https://api.github.com/users/jsbautista/subscriptions",
521-
"organizations_url": "https://api.github.com/users/jsbautista/orgs",
522-
"repos_url": "https://api.github.com/users/jsbautista/repos",
523-
"events_url": "https://api.github.com/users/jsbautista/events{/privacy}",
524-
"received_events_url": "https://api.github.com/users/jsbautista/received_events",
525-
"type": "User",
526-
"user_view_type": "public",
527-
"site_admin": false,
528-
"contributions": 92
529-
},
530530
{
531531
"login": "isabela-pf",
532532
"id": 50221806,

static/data/hubspot.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,5 +231,5 @@
231231
"totalDonations": 150,
232232
"totalMonthlyDonations": 0,
233233
"totalOneTimeDonations": 150,
234-
"lastUpdated": "2025-04-14T19:26:36.933Z"
234+
"lastUpdated": "2025-04-15T18:26:07.181Z"
235235
}

0 commit comments

Comments
 (0)