Skip to content

Commit 50620c3

Browse files
committed
Remove cache from product detail
1 parent b9b58e0 commit 50620c3

7 files changed

Lines changed: 61 additions & 28 deletions

File tree

src/store/server/ajax.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ import serverSettings from './settings'
44
import api from 'cezerin-client';
55
api.init(serverSettings.apiBaseUrl, serverSettings.security.token);
66

7+
const DEFAULT_CACHE_CONTROL = 'public, max-age=600';
8+
const PRODUCTS_CACHE_CONTROL = 'public, max-age=60';
9+
710
ajaxRouter.get('/products', (req, res, next) => {
8-
api.products.list(req.query).then(({status, json}) => {
9-
res.status(status).send(json);
11+
const filter = req.query;
12+
filter.enabled = true;
13+
api.products.list(filter).then(({status, json}) => {
14+
res.status(status).header('Cache-Control', PRODUCTS_CACHE_CONTROL).send(json);
1015
})
1116
})
1217

@@ -135,25 +140,27 @@ ajaxRouter.put('/cart/billing_address', (req, res, next) => {
135140

136141
ajaxRouter.get('/product_categories', (req, res, next) => {
137142
api.product_categories.list(req.query).then(({status, json}) => {
138-
res.status(status).send(json);
143+
res.status(status).header('Cache-Control', DEFAULT_CACHE_CONTROL).send(json);
139144
})
140145
})
141146

142147
ajaxRouter.get('/product_categories/:id', (req, res, next) => {
143148
api.product_categories.retrieve(req.params.id).then(({status, json}) => {
144-
res.status(status).send(json);
149+
res.status(status).header('Cache-Control', DEFAULT_CACHE_CONTROL).send(json);
145150
})
146151
})
147152

148153
ajaxRouter.get('/pages/:id', (req, res, next) => {
149154
api.pages.retrieve(req.params.id).then(({status, json}) => {
150-
res.status(status).send(json);
155+
res.status(status).header('Cache-Control', DEFAULT_CACHE_CONTROL).send(json);
151156
})
152157
})
153158

154159
ajaxRouter.get('/sitemap', (req, res, next) => {
160+
const filter = req.query;
161+
filter.enabled = true;
155162
api.sitemap.retrieve(req.query).then(({status, json}) => {
156-
res.status(status).send(json);
163+
res.status(status).header('Cache-Control', DEFAULT_CACHE_CONTROL).send(json);
157164
})
158165
})
159166

@@ -177,7 +184,7 @@ ajaxRouter.get('/shipping_methods', (req, res, next) => {
177184

178185
ajaxRouter.get('/countries', (req, res, next) => {
179186
api.countries.list().then(({status, json}) => {
180-
res.status(status).send(json);
187+
res.status(status).header('Cache-Control', DEFAULT_CACHE_CONTROL).send(json);
181188
})
182189
})
183190

src/store/shared/actions.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export function setCategory(category_id) {
8282
const state = getState();
8383
const category = state.app.categories.find(c => c.id === category_id);
8484
dispatch(setCurrentCategory(category));
85-
dispatch(fetchProducts());
8685
dispatch(receiveProduct(null))
8786
}
8887
}
@@ -290,7 +289,7 @@ export function fetchProducts() {
290289
export function fetchMoreProducts() {
291290
return (dispatch, getState) => {
292291
const state = getState();
293-
if(state.app.loadingProducts) {
292+
if(state.app.loadingProducts || state.app.products.length === 0) {
294293
return Promise.resolve();
295294
}
296295
dispatch(requestMoreProducts());
@@ -382,7 +381,6 @@ export const getInitialState = (req, checkout_fields, currentPage, settings) =>
382381
order: null,
383382
productsFilter: {
384383
limit: 30,
385-
enabled: true,
386384
fields: 'path,id,name,category_id,category_name,sku,images,enabled,discontinued,stock_status,stock_quantity,price,on_sale,regular_price'
387385
},
388386
checkout_fields: checkout_fields

src/store/shared/routes.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import {Route, IndexRoute} from 'react-router'
33
import clientSettings from '../client/settings'
4-
import {fetchProduct, fetchPage, setCategory, receiveSitemap} from './actions'
4+
import {fetchProduct, fetchProducts, fetchPage, setCategory, receiveSitemap} from './actions'
55

66
import api from 'cezerin-client';
77
api.initAjax(clientSettings.ajaxBaseUrl);
@@ -15,22 +15,44 @@ import CheckoutContainer from './containers/checkout'
1515
import CheckoutSuccessContainer from './containers/checkoutSuccess'
1616
import NotFoundContainer from './containers/notfound'
1717

18-
function checkSitemap(nextState, cb) {
18+
const getSitemap = (path, state, dispatch) => {
19+
const currentPageFromState = state.app.currentPage;
20+
// loaded on first request (server side)
21+
const currentPageAlreadyInState = currentPageFromState && currentPageFromState.path === path;
22+
if(currentPageAlreadyInState) {
23+
return Promise.resolve({currentPage: currentPageFromState, currentPageAlreadyInState})
24+
} else {
25+
return api.ajax.sitemap.retrieve({ path: path }).then(sitemapResponse => {
26+
const currentPageFromRequest = sitemapResponse.json;
27+
if(currentPageFromRequest) {
28+
dispatch(receiveSitemap(currentPageFromRequest))
29+
}
30+
return ({currentPage: currentPageFromRequest, currentPageAlreadyInState});
31+
})
32+
}
33+
}
34+
35+
function getComponent(nextState, cb) {
1936
const {dispatch, getState} = this.store;
2037
const state = getState();
2138

22-
api.ajax.sitemap.retrieve({ path: nextState.location.pathname, enabled: true }).then(sitemapResponse => {
23-
if (sitemapResponse.json) {
24-
dispatch(receiveSitemap(sitemapResponse.json))
25-
if (sitemapResponse.json.type === 'product-category') {
26-
dispatch(setCategory(sitemapResponse.json.resource))
39+
getSitemap(nextState.location.pathname, state, dispatch).then(({currentPage, currentPageAlreadyInState}) => {
40+
if (currentPage) {
41+
if (currentPage.type === 'product-category') {
42+
dispatch(setCategory(currentPage.resource))
43+
if(!currentPageAlreadyInState){
44+
dispatch(fetchProducts());
45+
}
2746
cb(null, props => <CategoryContainer {...props}/>);
28-
29-
} else if (sitemapResponse.json.type === 'product') {
30-
dispatch(fetchProduct(sitemapResponse.json.resource))
47+
} else if (currentPage.type === 'product') {
48+
if(!currentPageAlreadyInState){
49+
dispatch(fetchProduct(currentPage.resource))
50+
}
3151
cb(null, props => <ProductContainer {...props}/>);
32-
} else if (sitemapResponse.json.type === 'page') {
33-
dispatch(fetchPage(sitemapResponse.json.resource))
52+
} else if (currentPage.type === 'page') {
53+
if(!currentPageAlreadyInState){
54+
dispatch(fetchPage(currentPage.resource))
55+
}
3456
if(nextState.location.pathname == '/') {
3557
cb(null, IndexContainer);
3658
} else if(nextState.location.pathname == '/checkout') {
@@ -51,9 +73,9 @@ function checkSitemap(nextState, cb) {
5173

5274
export default(store) => (
5375
<Route path='/' component={SharedContainer}>
54-
<IndexRoute getComponent={checkSitemap} store={store}/>
55-
<Route path="/:slug" getComponent={checkSitemap} store={store}/>
56-
<Route path="/:categorySlug/:productSlug" getComponent={checkSitemap} store={store}/>
76+
<IndexRoute getComponent={getComponent} store={store}/>
77+
<Route path="/:slug" getComponent={getComponent} store={store}/>
78+
<Route path="/:categorySlug/:productSlug" getComponent={getComponent} store={store}/>
5779
<Route path="*" component={NotFoundContainer} status={404}/>
5880
</Route>
5981
)

themes/current/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"language": "en",
23
"list_thumbnail_width": 340,
34
"preview_thumbnail_width": 100,
45
"big_thumbnail_width": 800
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"cartTitle": "cart",
33
"cartEmpty": "Cart is empty",
4-
"footerCopyright": "Copyright © 2017 Demo Drone Store. All Rights Reserved."
4+
"footerCopyright": "Copyright © 2017 Demo Drone Store. All Rights Reserved. DE. "
55
}

themes/current/locales/en.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"cartTitle": "cart",
3+
"cartEmpty": "Cart is empty",
4+
"footerCopyright": "Copyright © 2017 Demo Drone Store. All Rights Reserved. EN. "
5+
}

themes/current/src/lib/text.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import text from '../../locales/default.json'
2-
export default text
1+
import config from './config'
2+
module.exports = require('../../locales/' + config.language + '.json');

0 commit comments

Comments
 (0)