-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-plugin-meta-images.ts
More file actions
78 lines (65 loc) · 2.28 KB
/
vite-plugin-meta-images.ts
File metadata and controls
78 lines (65 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { Plugin } from 'vite';
import fs from 'fs';
import path from 'path';
/**
* Vite plugin that updates og:image and twitter:image meta tags
* to point to the app's opengraph image with the correct Replit domain.
*/
export function metaImagesPlugin(): Plugin {
return {
name: 'vite-plugin-meta-images',
transformIndexHtml(html) {
const baseUrl = getDeploymentUrl();
if (!baseUrl) {
log('[meta-images] no Replit deployment domain found, skipping meta tag updates');
return html;
}
// Check if opengraph image exists in public directory
const publicDir = path.resolve(process.cwd(), 'client', 'public');
const opengraphPngPath = path.join(publicDir, 'opengraph.png');
const opengraphJpgPath = path.join(publicDir, 'opengraph.jpg');
const opengraphJpegPath = path.join(publicDir, 'opengraph.jpeg');
let imageExt: string | null = null;
if (fs.existsSync(opengraphPngPath)) {
imageExt = 'png';
} else if (fs.existsSync(opengraphJpgPath)) {
imageExt = 'jpg';
} else if (fs.existsSync(opengraphJpegPath)) {
imageExt = 'jpeg';
}
if (!imageExt) {
log('[meta-images] OpenGraph image not found, skipping meta tag updates');
return html;
}
const imageUrl = `${baseUrl}/opengraph.${imageExt}`;
log('[meta-images] updating meta image tags to:', imageUrl);
html = html.replace(
/<meta\s+property="og:image"\s+content="[^"]*"\s*\/>/g,
`<meta property="og:image" content="${imageUrl}" />`
);
html = html.replace(
/<meta\s+name="twitter:image"\s+content="[^"]*"\s*\/>/g,
`<meta name="twitter:image" content="${imageUrl}" />`
);
return html;
},
};
}
function getDeploymentUrl(): string | null {
if (process.env.REPLIT_INTERNAL_APP_DOMAIN) {
const url = `https://${process.env.REPLIT_INTERNAL_APP_DOMAIN}`;
log('[meta-images] using internal app domain:', url);
return url;
}
if (process.env.REPLIT_DEV_DOMAIN) {
const url = `https://${process.env.REPLIT_DEV_DOMAIN}`;
log('[meta-images] using dev domain:', url);
return url;
}
return null;
}
function log(...args: any[]): void {
if (process.env.NODE_ENV === 'production') {
console.log(...args);
}
}