From b941dce9fa5ca2160de8d8718c442b66132eef87 Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 16 Nov 2021 13:09:45 -0800 Subject: [PATCH] Private cache headers in item search If the user is searching for things they own or want, make sure we don't CDN cache it! For many queries, this is taken care of in practice, because the search result includes `currentUserOwnsThis` and `currentUserWantsThis`. But I noticed in testing that, if the search result has no items, so those fields aren't actually part of the _response_, then the private header doesn't get set. So this mainly makes sure we don't accidentally cache an empty result from a user who didn't have anything they owned/wanted yet! --- src/server/types/Item.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/server/types/Item.js b/src/server/types/Item.js index 129aea4..5656d14 100644 --- a/src/server/types/Item.js +++ b/src/server/types/Item.js @@ -656,8 +656,13 @@ const resolvers = { itemSearchItemsLoader, petTypeBySpeciesAndColorLoader, currentUserId, - } + }, + { cacheControl } ) => { + if (currentUserOwnsOrWants != null) { + cacheControl.setCacheHint({ scope: "PRIVATE" }); + } + let bodyId = null; if (fitsPet) { const petType = await petTypeBySpeciesAndColorLoader.load({ @@ -790,8 +795,12 @@ const resolvers = { numTotalItems: async ( { query, bodyId, itemKind, currentUserOwnsOrWants, zoneIds }, { offset, limit }, - { currentUserId, itemSearchNumTotalItemsLoader } + { currentUserId, itemSearchNumTotalItemsLoader }, + { cacheControl } ) => { + if (currentUserOwnsOrWants != null) { + cacheControl.setCacheHint({ scope: "PRIVATE" }); + } const numTotalItems = await itemSearchNumTotalItemsLoader.load({ query: query.trim(), bodyId, @@ -807,8 +816,12 @@ const resolvers = { items: async ( { query, bodyId, itemKind, currentUserOwnsOrWants, zoneIds }, { offset, limit }, - { currentUserId, itemSearchItemsLoader } + { currentUserId, itemSearchItemsLoader }, + { cacheControl } ) => { + if (currentUserOwnsOrWants != null) { + cacheControl.setCacheHint({ scope: "PRIVATE" }); + } const items = await itemSearchItemsLoader.load({ query: query.trim(), bodyId,