feat: enhance SSR cache management with dependency tracking and entry-level invalidation

This commit is contained in:
2026-02-25 17:35:10 +00:00
parent 3886eb9f34
commit 3b84e49383
8 changed files with 96 additions and 86 deletions

View File

@@ -13,6 +13,10 @@ const { apiSsrBaseURL, ssrPublishCheckCollections } = require("../config")
function ssrRequest(cacheKey, endpoint, query, options) {
let url = endpoint + (query ? "?" + query : "")
// track which collections/entries contribute to this SSR render
// endpoint may contain path segments (e.g. "content/abc123") or query strings
const collectionName = endpoint.split("?")[0].split("/")[0]
if (ssrPublishCheckCollections?.includes(endpoint)) {
// @ts-ignore
let validUntil = context.ssrCacheValidUntil
@@ -63,6 +67,29 @@ function ssrRequest(cacheKey, endpoint, query, options) {
// json is go data structure and incompatible with js, so we need to convert it
const r = { data: JSON.parse(JSON.stringify(json)), count: count }
// track dependencies: "col:id" for single-entry, "col:*" for list queries
// @ts-ignore dynamic property set by get_read.js
if (context.ssrDeps) {
let entryId = null
if (!Array.isArray(r.data) && r.data && r.data.id) {
// direct ID lookup (COLLECTION/ID) API returned single object
entryId = r.data.id
} else if (options?.limit === 1 && Array.isArray(r.data) && r.data.length === 1 && r.data[0] && r.data[0].id) {
// filter-based detail query with limit:1
entryId = r.data[0].id
}
if (entryId) {
// @ts-ignore
context.ssrDeps[collectionName + ":" + entryId] = true
} else {
// list query any change to this collection affects this page
// @ts-ignore
context.ssrDeps[collectionName + ":*"] = true
}
}
// @ts-ignore
context.ssrCache[cacheKey] = r