Skip to content

Commit

Permalink
Merge pull request #30 from Financial-Times/matth/fix-products
Browse files Browse the repository at this point in the history
Don't request products without a session ID and fix response caching
  • Loading branch information
i-like-robots authored Mar 20, 2017
2 parents 7609887 + f04b51b commit d4114e6
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
});
}
Expand Down

0 comments on commit d4114e6

Please sign in to comment.