-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
sitemap.js
79 lines (68 loc) · 2.11 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
const axios = require('axios');
const fs = require('fs');
const { createSitemapIndex } = require('sitemap');
const slugify = require('./src/common/slugify');
const hostname = process.env.HOST_NAME || 'https://www.refine.bio';
const targetFolder = `${__dirname}/public`;
const ApiVersion = 'v1';
const ApiHost = `${process.env.REACT_APP_API_HOST || 'https://api.refine.bio'}/${ApiVersion}`;
const limit = 1000;
const resourceEndpoints = {
experiments: `${ApiHost}/search/?limit=${limit}&ordering=id`,
// samples: `${Api}/samples?limit=${limit}&ordering=id`,
};
/**
*Generates a sitemap by querying the API to get all experiments accession codes
*/
const getSitemapUrlForResource = (resource) =>
(result) => {
switch (resource) {
case 'experiments':
const { accession_code: code } = result;
const title = slugify(result.title);
return {
url: `/${resource}/${code}/${title}`,
priority: 0.8,
}
}
}
const getUrlsForResources = async (...resources) => {
const urls = [];
for (const resource of resources) {
const getUrl = getSitemapUrlForResource(resource);
let next = resourceEndpoints[resource];
while (next) {
try {
console.log(`Fetching ${resource} from ${next}`);
const resp = await axios.get(next);
next = resp.data.next;
urls.push(...resp.data.results.map(getUrl));
} catch (e) {
console.log(`Encountered error ${e}`);
next = null;
}
}
}
return urls;
}
const generateSitemap = async (resources) => {
console.log('Building Site Sitemap...');
const resourceUrls = await getUrlsForResources('experiments');
const staticUrls = ['/', '/about', '/license', '/privacy', '/terms'].map(
url => ({ url, priority: 0.5 })
);
const urls = [...staticUrls, ...resourceUrls];
console.log(`Generating sitemap index and sitemaps for ${urls.length} urls...`);
createSitemapIndex({
urls,
targetFolder,
hostname,
cacheTime: 60000,
sitemapName: 'sitemap',
sitemapSize: 50000,
xslUrl: '',
gzip: false,
callback: null,
});
}
generateSitemap();