-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSitemap.js
More file actions
80 lines (71 loc) · 2.2 KB
/
Sitemap.js
File metadata and controls
80 lines (71 loc) · 2.2 KB
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
//@flow
import {SitemapStream, streamToPromise} from 'sitemap';
import {postPath} from './Post';
import {createEnvironment} from './Environment';
import {fetchQuery} from 'react-relay/hooks';
import graphql from 'babel-plugin-relay/macro';
import useBasePath from './lib/useBasePath';
import type {Sitemap_QueryResponse} from './__generated__/Sitemap_Query.graphql';
const sitemapQuery = graphql`
query Sitemap_Query($repoOwner: String!, $repoName: String!, $cursor: String)
@persistedQueryConfiguration(
accessToken: {environmentVariable: "OG_GITHUB_TOKEN"}
fixedVariables: {environmentVariable: "REPOSITORY_FIXED_VARIABLES"}
freeVariables: ["cursor"]
cacheSeconds: 300
) {
gitHub {
repository(name: $repoName, owner: $repoOwner) {
issues(
first: 100
after: $cursor
orderBy: {direction: DESC, field: CREATED_AT}
labels: ["publish", "Publish"]
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
number
title
updatedAt
}
}
}
}
}
`;
export async function buildSitemap({siteHostname}: {siteHostname: string}) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const basePath = useBasePath();
const smStream = new SitemapStream({hostname: `${siteHostname}`});
smStream.write({url: `${basePath}/`});
const environment = createEnvironment();
let hasNextPage = true;
let cursor = null;
// Only fetch the first 1000
let reqCount = 0;
while (hasNextPage && reqCount <= 10) {
const data: ?Sitemap_QueryResponse = await fetchQuery(
environment,
sitemapQuery,
{cursor},
).toPromise();
reqCount++;
for (const node of data?.gitHub?.repository?.issues?.nodes || []) {
if (node) {
smStream.write({
url: `${basePath}${postPath({post: node})}`,
lastmod: node.updatedAt,
});
}
}
const pageInfo = data?.gitHub?.repository?.issues?.pageInfo;
hasNextPage = pageInfo?.hasNextPage;
cursor = pageInfo?.endCursor;
hasNextPage = cursor ? pageInfo?.hasNextPage : false;
}
smStream.end();
return await streamToPromise(smStream);
}