-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.config.js
100 lines (87 loc) · 2.79 KB
/
sitemap.config.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* eslint-disable no-await-in-loop */
/* eslint-disable no-console */
const fetch = require('isomorphic-unfetch')
const formatURLString = require('./src/helpers/formatURLString')
const apiHost = process.env.API_HOST || 'https://api.refine.bio'
const apiVersion = process.env.API_VERSION || 'v1'
const apiPath = `${apiHost}/${apiVersion}`
const hostname = process.env.HOST_NAME || 'https://www.refine.bio'
const limit = 1000 // for API limit value
const config = {
apiVersion,
apiPath,
endpoints: {
experiments: `${apiPath}/search/?limit=${limit}&ordering=id`
},
filePrefix: 'sitemap',
hostname,
outDir: `${__dirname}/public`,
sitemapInfoFile: 'sitemap-info.json',
staticPaths: ['/', '/about', '/license', '/privacy', '/terms'],
baseConfig: {
limit: 50000 // a sitemap size limit
}
}
// returns a sitemap url for the given resource
const getSitemapUrlForResource = (resource) => {
return (result) => {
const resourceUrl = {}
// adds a custom url setting for each given resource
switch (resource) {
case 'experiments':
resourceUrl.url = `/${resource}/${
result.accession_code
}/${formatURLString(result.title)}`
resourceUrl.priority = 0.8
break
default:
break
}
return resourceUrl
}
}
// returns an array of the sitemap urls for all resources
const getSitemapUrlsForResources = async (...resources) => {
const resourceUrls = []
const resourceInfo = {}
for (const resource of resources) {
let current = `${config.endpoints[resource]}`
while (current) {
try {
console.log(`Fetching ${resource} from ${current}`)
const readableStream = await fetch(current)
const response = await readableStream.json()
current = response.next
if (!resourceInfo.count) {
resourceInfo.count = response.count
}
if (!resourceInfo.runAt) {
resourceInfo.runAt = new Date()
}
resourceUrls.push(
...response.results.map(getSitemapUrlForResource(resource))
)
} catch (e) {
console.log(`Encountered error ${e}`)
current = null
}
}
}
return { resourceUrls, resourceInfo }
}
// returns an array of all sitemap urls (static paths + resources)
const generateSitemapUrls = async () => {
console.log('Building Site Sitemap...')
const staticUrls = config.staticPaths.map((path) => ({
url: `${config.hostname}${path}`,
lastmod: `${new Date().toISOString()}`,
priority: 0.5
}))
const { resourceUrls, resourceInfo } = await getSitemapUrlsForResources(
...Object.keys(config.endpoints)
)
const sitemapUrls = [...staticUrls, ...resourceUrls]
return { sitemapUrls, resourceInfo }
}
// supports CommonJS exports (used in sitemap.js)
module.exports = { config, generateSitemapUrls }