/
sitemap.js
82 lines (75 loc) · 1.93 KB
/
sitemap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const fetch = require('isomorphic-unfetch');
const sm = require('sitemap');
const coursesContainsData = courses => Array.isArray(courses) && courses.length > 0;
const getCourses = () => fetch('https://dgclr-server.herokuapp.com/graphql', {
method: 'POST',
body: JSON.stringify({ query: '{courses {slug}}', variables: {} }),
headers: {
'Content-Type': 'application/json',
},
})
.then((res) => {
if (res.ok) {
return res.json();
}
throw Error(res.statusText);
}) // expecting a json response
.then((json) => {
const { data } = json;
if (data && coursesContainsData(data.courses)) {
return data.courses;
}
return [];
})
.catch((e) => {
throw e;
});
const CACHE_TIME_MS = 1000 * 60 * 60 * 24; // 24 h - cache purge period
const sitemap = sm.createSitemap({
hostname: 'https://dgclr.fi',
cacheTime: CACHE_TIME_MS,
});
// For now, lets use just "in memory cache" to check if courses data is fetched
const inMemoryCache = { timestamp: 0 };
const setup = async () => {
const useCache = sitemap.isCacheValid() && inMemoryCache.timestamp > 0;
// If cache is valid, just return sitemap
if (useCache) {
return sitemap;
}
const courses = await getCourses();
if (!useCache) {
inMemoryCache.timestamp = Date.now();
}
// If we get courses data, add it dynamically to sitemap
if (coursesContainsData(courses)) {
for (let i = 0; i < courses.length; i += 1) {
const course = courses[i];
sitemap.add({
url: `/${course.slug}`,
changefreq: 'monthly',
priority: 1,
});
}
}
// Add also pages
sitemap.add({
url: '/',
changefreq: 'monthly',
priority: 0.6,
});
// Add also pages
sitemap.add({
url: '/advanced_search',
changefreq: 'monthly',
priority: 0.6,
});
// Add also pages
sitemap.add({
url: '/info',
changefreq: 'monthly',
priority: 0.9,
});
return sitemap;
};
module.exports = setup;