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