diff --git a/ghost/core/core/server/api/endpoints/posts-public.js b/ghost/core/core/server/api/endpoints/posts-public.js index 17168d83f2..63fe59ad8c 100644 --- a/ghost/core/core/server/api/endpoints/posts-public.js +++ b/ghost/core/core/server/api/endpoints/posts-public.js @@ -23,6 +23,34 @@ const rejectPrivateFieldsTransformer = input => mapQuery(input, function (value, }; }); +function generateOptionsData(frame, options) { + return options.reduce((memo, option) => { + let value = frame.options?.[option]; + if (['include', 'fields', 'formats'].includes(option)) { + value = value?.split(',').sort(); + } + + if (option === 'page') { + value = value || 1; + } + + return { + ...memo, + [option]: value + }; + }, {}); +} + +function generateAuthData(frame) { + if (frame.options?.context?.member) { + return { + free: frame.options?.context?.member.status === 'free', + tiers: frame.options?.context?.member.products?.map((product) => { + return product.slug; + }).sort() + }; + } +} module.exports = { docName: 'posts', @@ -31,6 +59,23 @@ module.exports = { cacheInvalidate: false }, cache: postsPublicService.api?.cache, + generateCacheKeyData(frame) { + return { + options: generateOptionsData(frame, [ + 'include', + 'filter', + 'fields', + 'formats', + 'limit', + 'order', + 'page', + 'absolute_urls', + 'collection' + ]), + auth: generateAuthData(frame), + method: 'browse' + }; + }, options: [ 'include', 'filter', @@ -67,6 +112,24 @@ module.exports = { headers: { cacheInvalidate: false }, + cache: postsPublicService.api?.cache, + generateCacheKeyData(frame) { + return { + options: generateOptionsData(frame, [ + 'include', + 'fields', + 'formats', + 'absolute_urls' + ]), + auth: generateAuthData(frame), + method: 'read', + identifier: { + id: frame.data.id, + slug: frame.data.slug, + uuid: frame.data.uuid + } + }; + }, options: [ 'include', 'fields',