From f04b51bcdd50c24856164638861125194693a00f Mon Sep 17 00:00:00 2001 From: Matt Hinchliffe Date: Mon, 20 Mar 2017 12:04:06 +0000 Subject: [PATCH] Fix: don't request products without a session ID and fix response caching --- main.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index 644e794..0011d57 100644 --- a/main.js +++ b/main.js @@ -41,20 +41,29 @@ const getUuid = () => { const getProducts = () => { const cachedProducts = cache('products'); const cachedUUID = cache('uuid'); - if (cachedProducts && cachedUUID){ + + if (cachedProducts && cachedUUID) { return Promise.resolve({ products: cachedProducts, uuid: cachedUUID }); } + // if we don't have a session token cookie, don't bother... + if (!getSessionId()) { + return Promise.resolve({}); + } + if (!requests.products) { requests.products = request('/products', { credentials: 'include' }) .then(({ products, uuid } = {}) => { - delete requests.products; + requests.products = undefined; + if (products) { - cache('uuid', uuid); + cache('products', products); } + if (uuid) { cache('uuid', uuid); } + return { products, uuid }; }); }