cache alternate URLs in UrlPreviewService
This commit is contained in:
@@ -185,7 +185,7 @@ export class UrlPreviewService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const cacheKey = `${url}@${lang}@${cacheFormatVersion}`;
|
const cacheKey = getCacheKey(url, lang);
|
||||||
if (await this.sendCachedPreview(cacheKey, reply, fetch)) {
|
if (await this.sendCachedPreview(cacheKey, reply, fetch)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -236,6 +236,18 @@ export class UrlPreviewService {
|
|||||||
// Await this to avoid hammering redis when a bunch of URLs are fetched at once
|
// Await this to avoid hammering redis when a bunch of URLs are fetched at once
|
||||||
await this.previewCache.set(cacheKey, summary);
|
await this.previewCache.set(cacheKey, summary);
|
||||||
|
|
||||||
|
// Also cache the response URL in case of redirects
|
||||||
|
if (summary.url !== url) {
|
||||||
|
const responseCacheKey = getCacheKey(summary.url, lang);
|
||||||
|
await this.previewCache.set(responseCacheKey, summary);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also cache the ActivityPub URL, if different from the others
|
||||||
|
if (summary.activityPub && summary.activityPub !== summary.url) {
|
||||||
|
const apCacheKey = getCacheKey(summary.activityPub, lang);
|
||||||
|
await this.previewCache.set(apCacheKey, summary);
|
||||||
|
}
|
||||||
|
|
||||||
// Cache 1 day (matching redis), but only once we finalize the result
|
// Cache 1 day (matching redis), but only once we finalize the result
|
||||||
if (!summary.activityPub || summary.haveNoteLocally) {
|
if (!summary.activityPub || summary.haveNoteLocally) {
|
||||||
reply.header('Cache-Control', 'public, max-age=86400');
|
reply.header('Cache-Control', 'public, max-age=86400');
|
||||||
@@ -552,3 +564,7 @@ export class UrlPreviewService {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCacheKey(url: string, lang = 'none') {
|
||||||
|
return `${url}@${lang}@${cacheFormatVersion}`;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user