-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathmiddleware.ts
More file actions
68 lines (56 loc) · 1.93 KB
/
Copy pathmiddleware.ts
File metadata and controls
68 lines (56 loc) · 1.93 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
export const config = {
matcher: ['/roadmap', '/stellar', '/usecases'],
};
const BOT_USER_AGENTS = [
'twitterbot',
'facebookexternalhit',
'linkedinbot',
'slackbot',
'discordbot',
'telegrambot',
'whatsapp',
'pinterest',
];
export default async function middleware(request: Request) {
const url = new URL(request.url);
const userAgent = request.headers.get('user-agent')?.toLowerCase() || '';
const isBot = BOT_USER_AGENTS.some((bot) => userAgent.includes(bot));
if (!isBot) {
return;
}
const routeMap: Record<string, { title: string; subtitle?: string }> = {
'/roadmap': { title: 'Roadmap', subtitle: 'The future of Wraith' },
'/stellar': { title: 'Stellar Ecosystem', subtitle: 'Seamless Integration' },
'/usecases': { title: 'Use Cases' },
};
const routeData = routeMap[url.pathname];
if (!routeData) {
return;
}
try {
const response = await fetch(url.origin);
let html = await response.text();
const ogImageUrl = `${url.origin}/api/og?title=${encodeURIComponent(routeData.title)}${
routeData.subtitle ? `&subtitle=${encodeURIComponent(routeData.subtitle)}` : ''
}`;
const customMetaTags = `
<meta property="og:title" content="${routeData.title} | Wraith" />
<meta property="og:image" content="${ogImageUrl}" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:title" content="${routeData.title} | Wraith" />
<meta name="twitter:image" content="${ogImageUrl}" />
</head>`;
html = html.replace('</head>', customMetaTags);
return new Response(html, {
headers: {
'content-type': 'text/html;charset=UTF-8',
'cache-control': 'public, max-age=0, s-maxage=0',
},
});
} catch (error) {
console.error('Middleware HTML rewrite failed:', error);
return;
}
}