summaryrefslogtreecommitdiff
path: root/packages/backend/src/server
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2025-05-28 13:32:21 -0400
committerHazelnoot <acomputerdog@gmail.com>2025-06-04 10:47:19 -0400
commita91c0de9b5b337fdb65fbd922969132d610bd8c4 (patch)
treebe3a6b3145ed24e5b3e2935dd86c327a4ad494f9 /packages/backend/src/server
parentredirect to exclude hash from preview URL (diff)
downloadsharkey-a91c0de9b5b337fdb65fbd922969132d610bd8c4.tar.gz
sharkey-a91c0de9b5b337fdb65fbd922969132d610bd8c4.tar.bz2
sharkey-a91c0de9b5b337fdb65fbd922969132d610bd8c4.zip
cache alternate URLs in UrlPreviewService
Diffstat (limited to 'packages/backend/src/server')
-rw-r--r--packages/backend/src/server/web/UrlPreviewService.ts18
1 files changed, 17 insertions, 1 deletions
diff --git a/packages/backend/src/server/web/UrlPreviewService.ts b/packages/backend/src/server/web/UrlPreviewService.ts
index 3e0133d50e..78b2204fbb 100644
--- a/packages/backend/src/server/web/UrlPreviewService.ts
+++ b/packages/backend/src/server/web/UrlPreviewService.ts
@@ -185,7 +185,7 @@ export class UrlPreviewService {
return;
}
- const cacheKey = `${url}@${lang}@${cacheFormatVersion}`;
+ const cacheKey = getCacheKey(url, lang);
if (await this.sendCachedPreview(cacheKey, reply, fetch)) {
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.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
if (!summary.activityPub || summary.haveNoteLocally) {
reply.header('Cache-Control', 'public, max-age=86400');
@@ -552,3 +564,7 @@ export class UrlPreviewService {
return true;
}
}
+
+function getCacheKey(url: string, lang = 'none') {
+ return `${url}@${lang}@${cacheFormatVersion}`;
+}