Optimize images and improve social media sharing previews
Convert images to JPG for optimization and ensure correct Open Graph tags are used for social media sharing. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 1f7e7e89-a520-4970-9645-37daadc466dc Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 30e90049-0ceb-4261-a159-b687b34f680b Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/f209e72a-0939-48fa-84fc-57854de71967/1f7e7e89-a520-4970-9645-37daadc466dc/s81zXmM Replit-Helium-Checkpoint-Created: true
This commit is contained in:
+20
-14
@@ -16,6 +16,14 @@ function ogImageUrl(coverImage: string, baseUrl: string): string {
|
||||
return imgPath.startsWith("http") ? imgPath : `${baseUrl}${imgPath}`;
|
||||
}
|
||||
|
||||
function stripExistingMeta(html: string): string {
|
||||
html = html.replace(/<meta\s+property="og:[^"]*"[^>]*>\s*/gi, "");
|
||||
html = html.replace(/<meta\s+name="twitter:[^"]*"[^>]*>\s*/gi, "");
|
||||
html = html.replace(/<meta\s+name="description"[^>]*>\s*/gi, "");
|
||||
html = html.replace(/<meta\s+name="keywords"[^>]*>\s*/gi, "");
|
||||
return html;
|
||||
}
|
||||
|
||||
export function serveStatic(app: Express) {
|
||||
const distPath = path.resolve(__dirname, "public");
|
||||
if (!fs.existsSync(distPath)) {
|
||||
@@ -26,7 +34,7 @@ export function serveStatic(app: Express) {
|
||||
|
||||
app.use(express.static(distPath, {
|
||||
setHeaders(res, filePath) {
|
||||
if (filePath.endsWith("og-image.jpg") || filePath.includes("/uploads/")) {
|
||||
if (filePath.endsWith("og-image.jpg") || filePath.includes("/uploads/") || filePath.includes("/images/")) {
|
||||
res.setHeader("Cache-Control", "public, max-age=86400");
|
||||
}
|
||||
},
|
||||
@@ -36,30 +44,35 @@ export function serveStatic(app: Express) {
|
||||
const url = req.originalUrl;
|
||||
const indexPath = path.resolve(distPath, "index.html");
|
||||
|
||||
const host = req.get("host") || "folx.tv";
|
||||
const protocol = req.get("x-forwarded-proto") || "https";
|
||||
const baseUrl = `${protocol}://${host}`;
|
||||
|
||||
const articleMatch = url.match(/^\/article\/([^?#]+)/);
|
||||
if (articleMatch) {
|
||||
try {
|
||||
const slug = decodeURIComponent(articleMatch[1]);
|
||||
const article = await storage.getArticleBySlug(slug);
|
||||
if (article) {
|
||||
const host = req.get("host") || "folx.tv";
|
||||
const protocol = req.get("x-forwarded-proto") || "https";
|
||||
const baseUrl = `${protocol}://${host}`;
|
||||
const articleUrl = `${baseUrl}/article/${article.slug}`;
|
||||
const imageUrl = ogImageUrl(article.coverImage || "", baseUrl);
|
||||
const finalImage = imageUrl || `${baseUrl}/og-image.jpg`;
|
||||
|
||||
let template = await fs.promises.readFile(indexPath, "utf-8");
|
||||
template = stripExistingMeta(template);
|
||||
|
||||
const finalImage = imageUrl || `${baseUrl}/og-image.jpg`;
|
||||
const ogTags = [
|
||||
`<meta property="og:title" content="${escapeHtml(article.title)}" />`,
|
||||
`<meta property="og:description" content="${escapeHtml(article.excerpt)}" />`,
|
||||
`<meta property="og:type" content="article" />`,
|
||||
`<meta property="og:url" content="${escapeHtml(articleUrl)}" />`,
|
||||
`<meta property="og:image" content="${escapeHtml(finalImage)}" />`,
|
||||
`<meta property="og:image:width" content="1200" />`,
|
||||
`<meta property="og:image:height" content="630" />`,
|
||||
`<meta property="og:image:secure_url" content="${escapeHtml(finalImage)}" />`,
|
||||
`<meta property="og:image:width" content="800" />`,
|
||||
`<meta property="og:image:height" content="450" />`,
|
||||
`<meta property="og:image:type" content="image/jpeg" />`,
|
||||
`<meta property="og:site_name" content="Folx Music Television" />`,
|
||||
`<meta property="og:locale" content="de_DE" />`,
|
||||
`<meta name="twitter:card" content="summary_large_image" />`,
|
||||
`<meta name="twitter:title" content="${escapeHtml(article.title)}" />`,
|
||||
`<meta name="twitter:description" content="${escapeHtml(article.excerpt)}" />`,
|
||||
@@ -68,9 +81,6 @@ export function serveStatic(app: Express) {
|
||||
`<title>${escapeHtml(article.title)} - Folx Music Television</title>`,
|
||||
].join("\n ");
|
||||
|
||||
template = template.replace(/<meta property="og:[^>]*>\s*/g, "");
|
||||
template = template.replace(/<meta name="description"[^>]*>\s*/g, "");
|
||||
template = template.replace(/<meta name="twitter:[^>]*>\s*/g, "");
|
||||
template = template.replace(/<title>[^<]*<\/title>/, ogTags);
|
||||
|
||||
res.status(200).set({ "Content-Type": "text/html" }).end(template);
|
||||
@@ -80,10 +90,6 @@ export function serveStatic(app: Express) {
|
||||
}
|
||||
}
|
||||
|
||||
const host = req.get("host") || "folx.tv";
|
||||
const protocol = req.get("x-forwarded-proto") || "https";
|
||||
const baseUrl = `${protocol}://${host}`;
|
||||
|
||||
let template = await fs.promises.readFile(indexPath, "utf-8");
|
||||
template = template.replace(/https:\/\/www\.folx\.tv\//g, `${baseUrl}/`);
|
||||
template = template.replace(/https:\/\/www\.folx\.tv\/og-image\.jpg/g, `${baseUrl}/og-image.jpg`);
|
||||
|
||||
Reference in New Issue
Block a user