@griddo/cx 1.64.6 → 1.64.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/gatsby-node.js CHANGED
@@ -7,6 +7,7 @@ const NavigationService = require('./src/services/navigation').default;
7
7
  const SitesService = require('./src/services/sites').default;
8
8
  const SettingsService = require('./src/services/settings').default;
9
9
  const RobotsService = require('./src/services/robots').default;
10
+ const { initCache } = require('./src/utils/cache');
10
11
 
11
12
  const {
12
13
  isComponentLibraryEnv,
@@ -66,6 +67,7 @@ exports.onCreateWebpackConfig = ({ actions, plugins, loaders, getConfig }) => {
66
67
  // onPreInit (hook)
67
68
  // -----------------------------------------------------------------------------
68
69
  exports.onPreInit = async () => {
70
+ initCache();
69
71
  await folders.prepareStaticFolder();
70
72
  await SettingsService.getAll();
71
73
  updatedSites = await sites.checkSites();
@@ -79,6 +81,7 @@ exports.onPreInit = async () => {
79
81
  exports.createPages = async ({ actions }) => {
80
82
  // fetch
81
83
  for (const site of updatedSites) {
84
+ const cache = !site.shouldBeUpdated;
82
85
  const NavService = new NavigationService();
83
86
  const { id: siteID, slug: siteSlug, theme, favicon } = site;
84
87
  const {
@@ -143,7 +146,7 @@ exports.createPages = async ({ actions }) => {
143
146
  createdPages.push(pageId);
144
147
 
145
148
  // Get page data
146
- const page = await SitesService.getPage(pageId);
149
+ const page = await SitesService.getPage(pageId, cache);
147
150
 
148
151
  helpers.log(
149
152
  'info',
@@ -153,7 +156,8 @@ exports.createPages = async ({ actions }) => {
153
156
 
154
157
  // Template data with the distributor data added.
155
158
  const distributorTemplate = await DistributorService.getDistributorData(
156
- page
159
+ page,
160
+ cache
157
161
  );
158
162
 
159
163
  additionalInfo.navigations = NavService.getPageNavigations(page);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@griddo/cx",
3
3
  "description": "Griddo SSG based on Gatsby",
4
- "version": "1.64.6",
4
+ "version": "1.64.9",
5
5
  "authors": [
6
6
  "Álvaro Sánchez' <alvaro.sanches@secuoyas.com>",
7
7
  "Carlos Torres <carlos.torres@secuoyas.com>",
@@ -64,7 +64,7 @@
64
64
  "react-helmet": "^6.0.0"
65
65
  },
66
66
  "devDependencies": {
67
- "@griddo/eslint-config-back": "^1.64.6",
67
+ "@griddo/eslint-config-back": "^1.64.9",
68
68
  "eslint": "^7.5.0",
69
69
  "eslint-plugin-node": "^11.1.0",
70
70
  "eslint-plugin-react": "7.14.3",
@@ -96,5 +96,5 @@
96
96
  "publishConfig": {
97
97
  "access": "public"
98
98
  },
99
- "gitHead": "6e6a7945bb41cab79796c90786ba62069f09a977"
99
+ "gitHead": "89612d5233cf272c1535b62aacb4ec5cd5f2fdbd"
100
100
  }
package/src/api/index.js CHANGED
@@ -1,50 +1,68 @@
1
1
  const axios = require("axios")
2
-
3
2
  const auth = require('../services/auth').default;
3
+ const { saveCache, getCache } = require('../utils/cache');
4
4
 
5
5
  require("dotenv").config()
6
6
 
7
- const getApi = async (endpoint, body) => {
7
+ const getApi = async (endpoint, body, cached = false) => {
8
+ const cacheOptions = { endpoint, body };
9
+ if (cached) {
10
+ const cachedResponse = getCache(cacheOptions);
11
+ if (cachedResponse) return cachedResponse;
12
+ }
8
13
  try {
9
- const response = await axios({
14
+ const { data } = await axios({
10
15
  url: endpoint,
11
16
  method: "get",
12
17
  headers: { ...auth.headers },
13
18
  data: body,
14
- })
15
- return response.data
19
+ });
20
+ saveCache(cacheOptions, data);
21
+ return data;
16
22
  } catch (e) {
17
- showApiError(e, { endpoint, body })
23
+ showApiError(e, { endpoint, body });
18
24
  }
19
- }
25
+ };
20
26
 
21
- const putApi = async (endpoint, body) => {
27
+ const putApi = async (endpoint, body, cached = false) => {
28
+ const cacheOptions = { endpoint, body };
29
+ if (cached) {
30
+ const cachedResponse = getCache(cacheOptions);
31
+ if (cachedResponse) return cachedResponse;
32
+ }
22
33
  try {
23
- const response = await axios({
34
+ const { data } = await axios({
24
35
  url: endpoint,
25
36
  method: "put",
26
37
  headers: { ...auth.headers },
27
38
  data: body,
28
- })
29
- return response
39
+ });
40
+ saveCache(cacheOptions, data);
41
+ return data;
30
42
  } catch (e) {
31
- showApiError(e, { endpoint, body })
43
+ showApiError(e, { endpoint, body });
32
44
  }
33
- }
45
+ };
34
46
 
35
- const postApi = async (endpoint, body, headers) => {
47
+ const postApi = async (endpoint, body, headers, cached) => {
48
+ const cacheOptions = { endpoint, body, headers };
49
+ if (cached) {
50
+ const cachedResponse = getCache(cacheOptions);
51
+ if (cachedResponse) return cachedResponse;
52
+ }
36
53
  try {
37
- const response = await axios({
54
+ const { data } = await axios({
38
55
  url: endpoint,
39
56
  method: "post",
40
57
  headers: { ...headers, ...auth.headers },
41
58
  data: body,
42
- })
43
- return response.data
59
+ });
60
+ saveCache(cacheOptions, data);
61
+ return data;
44
62
  } catch (e) {
45
63
  showApiError(e, { endpoint, body, headers });
46
64
  }
47
- }
65
+ };
48
66
 
49
67
  const showApiError = (e, callInfo = {}) => {
50
68
  const {
@@ -29,23 +29,24 @@ class DistributorService {
29
29
  };
30
30
  }
31
31
 
32
- static async fetchData({ page, component }) {
32
+ static async fetchData({ page, component, cached }) {
33
33
  const { data } = component;
34
34
  if (!data) {
35
35
  console.log(`======= ERROR: Página ${page.id} tiene un hasDistributorData pero no un elemento data con la configuración del distribuidor en el mismo nivel.`);
36
36
  return [];
37
37
  }
38
38
  const body = this.getBody(data);
39
- const response = await SitesService.getDistributorData(page, body);
39
+ const response = await SitesService.getDistributorData(page, body, cached);
40
40
  return response;
41
41
  }
42
42
 
43
- static async getDistributorData(page) {
43
+ static async getDistributorData(page, cached = false) {
44
44
  try {
45
45
  const { template } = page;
46
46
 
47
47
  const getDistributorsContent = async (template, level = 1) => {
48
48
  if (!template || typeof (template) !== 'object') return;
49
+ if (!JSON.stringify(template).includes('"hasDistributorData":true')) return;
49
50
  for (let key in template) {
50
51
  const component = template[key];
51
52
  if (!component || typeof (component) !== 'object') continue;
@@ -53,6 +54,7 @@ class DistributorService {
53
54
  component.queriedItems = await this.fetchData({
54
55
  page,
55
56
  component,
57
+ cached
56
58
  });
57
59
  }
58
60
  await getDistributorsContent(component, level + 1);
@@ -23,30 +23,30 @@ class SitesService {
23
23
  return response;
24
24
  }
25
25
 
26
- static async getPage(id) {
26
+ static async getPage(id, cache) {
27
27
  const {
28
28
  GET_PAGE: [prefix, suffix],
29
29
  } = ENDPOINTS;
30
30
  const dynamicURL = `${prefix}${id}${suffix}`;
31
- const response = await api.get(dynamicURL);
31
+ const response = await api.get(dynamicURL, null, cache);
32
32
  return response;
33
33
  }
34
34
 
35
- static async getInfo(id) {
35
+ static async getInfo(id, cached) {
36
36
  const {
37
37
  INFO: [prefix, suffix],
38
38
  } = ENDPOINTS;
39
39
  const dynamicURL = `${prefix}${id}${suffix}`;
40
- const response = await api.get(dynamicURL);
40
+ const response = await api.get(dynamicURL, null, cached);
41
41
  return response;
42
42
  }
43
43
 
44
- static async getLanguages(id) {
44
+ static async getLanguages(id, cached) {
45
45
  const {
46
46
  LANGUAGES: [prefix, suffix],
47
47
  } = ENDPOINTS;
48
48
  const dynamicURL = `${prefix}${id}${suffix}`;
49
- const response = await api.get(dynamicURL);
49
+ const response = await api.get(dynamicURL, null, cached);
50
50
  return response;
51
51
  }
52
52
 
@@ -68,14 +68,14 @@ class SitesService {
68
68
  return response;
69
69
  }
70
70
 
71
- static async getDistributorData(page, body) {
71
+ static async getDistributorData(page, body, cached) {
72
72
  const {
73
73
  GET_DISTRIBUTOR_DATA: [prefix, suffix],
74
74
  } = ENDPOINTS;
75
75
  const { language: lang, site: id } = page;
76
76
 
77
77
  const dynamicURL = `${prefix}${id}${suffix}`;
78
- const response = await api.post(dynamicURL, body, { lang });
78
+ const response = await api.post(dynamicURL, body, { lang }, cached);
79
79
  return response;
80
80
  }
81
81
 
@@ -88,21 +88,21 @@ class SitesService {
88
88
  return response;
89
89
  }
90
90
 
91
- static async getSocials(id) {
91
+ static async getSocials(id, cached) {
92
92
  const {
93
93
  SOCIALS: [prefix, suffix],
94
94
  } = ENDPOINTS;
95
95
  const dynamicURL = `${prefix}${id}${suffix}`;
96
- const response = await api.get(dynamicURL);
96
+ const response = await api.get(dynamicURL, null, cached);
97
97
  return response;
98
98
  }
99
99
 
100
- static async getPages(id) {
100
+ static async getPages(id, cached = false) {
101
101
  const {
102
102
  GET_PAGES: [prefix, suffix],
103
103
  } = ENDPOINTS;
104
104
  const dynamicURL = `${prefix}${id}${suffix}`;
105
- const response = await api.get(dynamicURL);
105
+ const response = await api.get(dynamicURL, null, cached);
106
106
  return response;
107
107
  }
108
108
  }
@@ -0,0 +1,36 @@
1
+ const fs = require('fs');
2
+ const crypto = require('crypto');
3
+
4
+ const apiCacheDirectory = './apiCache';
5
+
6
+ const initCache = () => {
7
+ if (!fs.existsSync(apiCacheDirectory)) {
8
+ fs.mkdirSync(apiCacheDirectory);
9
+ }
10
+ };
11
+
12
+ const generateFilename = (petition) => {
13
+ const hashSum = crypto.createHash('sha256');
14
+ hashSum.update(JSON.stringify(petition));
15
+ return `${apiCacheDirectory}/${hashSum.digest('hex')}`;
16
+ };
17
+
18
+ const saveCache = (petition, content = '') => {
19
+ const rightContent = typeof (content) === 'object' ? JSON.stringify(content) : content;
20
+ fs.writeFileSync(generateFilename(petition), rightContent, 'utf8');
21
+ };
22
+
23
+ const getCache = (petition) => {
24
+ try {
25
+ const content = fs.readFileSync(generateFilename(petition));
26
+ return JSON.parse(content);
27
+ } catch {
28
+ return null;
29
+ }
30
+ };
31
+
32
+ module.exports = {
33
+ initCache,
34
+ saveCache,
35
+ getCache,
36
+ };
@@ -66,17 +66,17 @@ const unpublishSites = async sites => {
66
66
  await folders.deleteSites(sites);
67
67
  };
68
68
 
69
- const getSiteData = async siteID => {
69
+ const getSiteData = async (siteID, cached) => {
70
70
  const buildData = await SitesService.startPageRender(siteID);
71
71
 
72
72
  helpers.log('success', 'PAGE RENDER STARTS', buildData);
73
73
 
74
- const siteInfo = await SitesService.getInfo(siteID);
75
- const siteLangs = await SitesService.getLanguages(siteID);
76
- const socials = await SitesService.getSocials(siteID);
74
+ const siteInfo = await SitesService.getInfo(siteID,cached);
75
+ const siteLangs = await SitesService.getLanguages(siteID, cached);
76
+ const socials = await SitesService.getSocials(siteID, cached);
77
77
  const siteLangsInfo = siteLangs.items;
78
78
  const defaultLang = siteLangsInfo.find(lang => lang.isDefault);
79
- const sitePages = await SitesService.getPages(siteID);
79
+ const sitePages = await SitesService.getPages(siteID, cached);
80
80
 
81
81
  const validPagesIds = buildData.publishIds;
82
82
  const { siteHash, unpublishHashes } = buildData;