Select unfurl mode with X-Unfurl-Type header

This commit is contained in:
Mononaut 2023-08-14 17:52:15 +09:00
parent e9165e5dd8
commit 13f6f9f9e5
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E

View File

@ -250,25 +250,25 @@ class Server {
|| rawPath.startsWith('/api/v1/translators/images')
|| rawPath.startsWith('/resources/profile')
) {
if (isSearchCrawler(req.headers['user-agent'])) {
if (isPreviewCrawler(req)) {
res.status(404).send();
return;
} else {
if (this.secureHost) {
https.get(config.SERVER.HOST + rawPath, { headers: { 'user-agent': 'mempoolunfurl' }}, (got) => got.pipe(res));
} else {
http.get(config.SERVER.HOST + rawPath, { headers: { 'user-agent': 'mempoolunfurl' }}, (got) => got.pipe(res));
}
return;
} else {
res.status(404).send();
return;
}
}
let result = '';
try {
if (isSearchCrawler(req.headers['user-agent'])) {
result = await this.renderSEOPage(rawPath);
} else {
if (isPreviewCrawler(req)) {
result = await this.renderUnfurlMeta(rawPath);
} else {
result = await this.renderSEOPage(rawPath);
}
if (result && result.length) {
if (result === '404') {
@ -349,6 +349,6 @@ function capitalize(str) {
}
}
function isSearchCrawler(useragent: string): boolean {
return /googlebot|applebot|bingbot/i.test(useragent);
function isPreviewCrawler(req: Request): boolean {
return req?.header('X-Unfurl-Type') === 'preview';
}