Skip to content

Commit 48ac488

Browse files
author
Snehil
committed
Add flag for seo page generation
1 parent f0f397b commit 48ac488

6 files changed

Lines changed: 173 additions & 145 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"scripts": {
66
"dev": "next dev",
7+
"prebuild": "node scripts/dump-paths.mjs",
78
"build": "next build && next export",
89
"postbuild": "cp ./seo/sitemap.xml ./public/",
910
"start": "next start",

pages/[userId]/[repositoryId]/index.js

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,61 +31,65 @@ const atob = (base64) => {
3131
};
3232

3333
export async function getStaticProps({ params }) {
34-
const fs = (await import("fs")).default;
35-
const repositories = JSON.parse(
36-
fs.readFileSync("./seo/dump-repositories.json")
37-
);
34+
try {
35+
const fs = (await import("fs")).default;
36+
const repositories = JSON.parse(
37+
fs.readFileSync("./seo/dump-repositories.json")
38+
);
3839

39-
const r = find(
40-
repositories,
41-
(r) =>
42-
r.name === params.repositoryId &&
43-
(r.owner.id === params.userId || r.owner.username === params.userId)
44-
);
40+
const r = find(
41+
repositories,
42+
(r) =>
43+
r.name === params.repositoryId &&
44+
(r.owner.id === params.userId || r.owner.username === params.userId)
45+
);
4546

46-
if (r) {
47-
let branchSha = getBranchSha(r.defaultBranch, r.branches, r.tags);
47+
if (r) {
48+
let branchSha = getBranchSha(r.defaultBranch, r.branches, r.tags);
4849

49-
// const entitiesRes = await getContent(r.id, branchSha, null, null);
50+
const entitiesRes = await getContent(r.id, branchSha, null, null);
5051

51-
// const commitHistory = await getCommitHistory(r.id, branchSha, null, 1);
52+
const commitHistory = await getCommitHistory(r.id, branchSha, null, 1);
5253

53-
// const readmeRegex = new RegExp(/^README/gi);
54-
// let readmeFile = null;
55-
// for (let i = 0; i < entitiesRes?.content?.length; i++) {
56-
// if (readmeRegex.test(entitiesRes.content[i].name)) {
57-
// const readme = await getContent(
58-
// r.id,
59-
// branchSha,
60-
// entitiesRes.content[i].name
61-
// );
62-
// if (readme?.content[0]) {
63-
// readmeFile = atob(readme.content[0].content);
64-
// }
65-
// }
66-
// }
67-
return {
68-
props: {
69-
repository: r,
70-
// entitiesRes,
71-
// commitHistory: { commits: [{}], ...commitHistory },
72-
// readmeFile,
73-
},
74-
revalidate: 1,
75-
};
76-
}
54+
const readmeRegex = new RegExp(/^README/gi);
55+
let readmeFile = null;
56+
for (let i = 0; i < entitiesRes?.content?.length; i++) {
57+
if (readmeRegex.test(entitiesRes.content[i].name)) {
58+
const readme = await getContent(
59+
r.id,
60+
branchSha,
61+
entitiesRes.content[i].name
62+
);
63+
if (readme?.content[0]) {
64+
readmeFile = atob(readme.content[0].content);
65+
}
66+
}
67+
}
68+
return {
69+
props: {
70+
repository: r,
71+
entitiesRes,
72+
commitHistory: { commits: [{}], ...commitHistory },
73+
readmeFile,
74+
},
75+
revalidate: 1,
76+
};
77+
}
78+
} catch (e) {}
7779
return {
7880
props: {},
7981
};
8082
}
8183

8284
export async function getStaticPaths() {
83-
const fs = (await import("fs")).default;
8485
let paths = [];
85-
try {
86-
paths = JSON.parse(fs.readFileSync("./seo/paths-repositories.json"));
87-
} catch (e) {
88-
console.error(e);
86+
if (process.env.GENERATE_SEO_PAGES) {
87+
try {
88+
const fs = (await import("fs")).default;
89+
paths = JSON.parse(fs.readFileSync("./seo/paths-repositories.json"));
90+
} catch (e) {
91+
console.error(e);
92+
}
8993
}
9094
return {
9195
paths,

pages/[userId]/[repositoryId]/issues/[issueIid]/index.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,41 +37,47 @@ import Link from "next/link";
3737
import filter from "lodash/filter";
3838

3939
export async function getStaticProps({ params }) {
40-
const fs = (await import("fs")).default;
41-
const issues = JSON.parse(fs.readFileSync("./seo/dump-issues.json")),
42-
repositories = JSON.parse(fs.readFileSync("./seo/dump-repositories.json")),
43-
comments = JSON.parse(fs.readFileSync("./seo/dump-comments.json"));
44-
45-
const r = find(
46-
repositories,
47-
(r) =>
48-
r.name === params.repositoryId &&
49-
(r.owner.id === params.userId || r.owner.username === params.userId)
50-
);
40+
try {
41+
const fs = (await import("fs")).default;
42+
const issues = JSON.parse(fs.readFileSync("./seo/dump-issues.json")),
43+
repositories = JSON.parse(
44+
fs.readFileSync("./seo/dump-repositories.json")
45+
),
46+
comments = JSON.parse(fs.readFileSync("./seo/dump-comments.json"));
5147

52-
if (r) {
53-
const i = find(
54-
issues,
55-
(t) => t.iid === params.issueIid && t.repositoryId === r.id
48+
const r = find(
49+
repositories,
50+
(r) =>
51+
r.name === params.repositoryId &&
52+
(r.owner.id === params.userId || r.owner.username === params.userId)
5653
);
57-
if (i) {
58-
const cs = filter(comments, (c) => i.comments.includes(c.id));
59-
return {
60-
props: { repository: r, issue: i, comments: cs },
61-
revalidate: 1,
62-
};
54+
55+
if (r) {
56+
const i = find(
57+
issues,
58+
(t) => t.iid === params.issueIid && t.repositoryId === r.id
59+
);
60+
if (i) {
61+
const cs = filter(comments, (c) => i.comments.includes(c.id));
62+
return {
63+
props: { repository: r, issue: i, comments: cs },
64+
revalidate: 1,
65+
};
66+
}
6367
}
64-
}
68+
} catch (e) {}
6569
return { props: {} };
6670
}
6771

6872
export async function getStaticPaths() {
69-
const fs = (await import("fs")).default;
7073
let paths = [];
71-
try {
72-
paths = JSON.parse(fs.readFileSync("./seo/paths-issues.json"));
73-
} catch (e) {
74-
console.error(e);
74+
if (process.env.GENERATE_SEO_PAGES) {
75+
try {
76+
const fs = (await import("fs")).default;
77+
paths = JSON.parse(fs.readFileSync("./seo/paths-issues.json"));
78+
} catch (e) {
79+
console.error(e);
80+
}
7581
}
7682
return {
7783
paths,

pages/[userId]/[repositoryId]/pulls/[pullRequestIid]/index.js

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,70 +33,74 @@ import filter from "lodash/filter";
3333
import PullRequestIssueView from "../../../../../components/repository/issuesView";
3434

3535
export async function getStaticProps({ params }) {
36-
const fs = (await import("fs")).default;
37-
const repositories = JSON.parse(
38-
fs.readFileSync("./seo/dump-repositories.json")
39-
),
40-
pulls = JSON.parse(fs.readFileSync("./seo/dump-pulls.json")),
41-
comments = JSON.parse(fs.readFileSync("./seo/dump-comments.json"));
42-
43-
const r = find(
44-
repositories,
45-
(r) =>
46-
r.name === params.repositoryId &&
47-
(r.owner.id === params.userId || r.owner.username === params.userId)
48-
);
36+
try {
37+
const fs = (await import("fs")).default;
38+
const repositories = JSON.parse(
39+
fs.readFileSync("./seo/dump-repositories.json")
40+
),
41+
pulls = JSON.parse(fs.readFileSync("./seo/dump-pulls.json")),
42+
comments = JSON.parse(fs.readFileSync("./seo/dump-comments.json"));
4943

50-
if (r) {
51-
const p = find(
52-
pulls,
53-
(t) => t.iid === params.pullRequestIid && t.base?.repositoryId === r.id
44+
const r = find(
45+
repositories,
46+
(r) =>
47+
r.name === params.repositoryId &&
48+
(r.owner.id === params.userId || r.owner.username === params.userId)
5449
);
55-
if (p) {
56-
if (p.base.repositoryId === p.head.repositoryId) {
57-
p.head.repository = p.base.repository = r;
58-
if (p.state === "OPEN") {
59-
p.head.sha = getBranchSha(p.head.branch, r.branches, r.tags);
60-
p.base.sha = getBranchSha(p.base.branch, r.branches, r.tags);
61-
} else {
62-
p.base.sha = p.base.commitSha;
63-
p.head.sha = p.head.commitSha;
64-
}
65-
} else {
66-
const forkRepo = find(repositories, { id: p.head.repositoryId });
67-
if (forkRepo) {
68-
p.head.repository = forkRepo;
69-
p.base.repository = r;
50+
51+
if (r) {
52+
const p = find(
53+
pulls,
54+
(t) => t.iid === params.pullRequestIid && t.base?.repositoryId === r.id
55+
);
56+
if (p) {
57+
if (p.base.repositoryId === p.head.repositoryId) {
58+
p.head.repository = p.base.repository = r;
7059
if (p.state === "OPEN") {
71-
p.head.sha = getBranchSha(
72-
p.head.branch,
73-
forkRepo.branches,
74-
forkRepo.tags
75-
);
60+
p.head.sha = getBranchSha(p.head.branch, r.branches, r.tags);
7661
p.base.sha = getBranchSha(p.base.branch, r.branches, r.tags);
7762
} else {
7863
p.base.sha = p.base.commitSha;
7964
p.head.sha = p.head.commitSha;
8065
}
66+
} else {
67+
const forkRepo = find(repositories, { id: p.head.repositoryId });
68+
if (forkRepo) {
69+
p.head.repository = forkRepo;
70+
p.base.repository = r;
71+
if (p.state === "OPEN") {
72+
p.head.sha = getBranchSha(
73+
p.head.branch,
74+
forkRepo.branches,
75+
forkRepo.tags
76+
);
77+
p.base.sha = getBranchSha(p.base.branch, r.branches, r.tags);
78+
} else {
79+
p.base.sha = p.base.commitSha;
80+
p.head.sha = p.head.commitSha;
81+
}
82+
}
8183
}
84+
const cs = filter(comments, (c) => p.comments.includes(c.id));
85+
return {
86+
props: { repository: r, pullRequest: p, comments: cs },
87+
revalidate: 1,
88+
};
8289
}
83-
const cs = filter(comments, (c) => p.comments.includes(c.id));
84-
return {
85-
props: { repository: r, pullRequest: p, comments: cs },
86-
revalidate: 1,
87-
};
8890
}
89-
}
91+
} catch (e) {}
9092
return { props: {} };
9193
}
9294

9395
export async function getStaticPaths() {
94-
const fs = (await import("fs")).default;
9596
let paths = [];
96-
try {
97-
paths = JSON.parse(fs.readFileSync("./seo/paths-pulls.json"));
98-
} catch (e) {
99-
console.error(e);
97+
if (process.env.GENERATE_SEO_PAGES) {
98+
try {
99+
const fs = (await import("fs")).default;
100+
paths = JSON.parse(fs.readFileSync("./seo/paths-pulls.json"));
101+
} catch (e) {
102+
console.error(e);
103+
}
100104
}
101105
return {
102106
paths,

pages/[userId]/index.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,28 @@ export async function getStaticProps({ params }) {
2828
u,
2929
d,
3030
allRepos = [];
31-
const fs = (await import("fs")).default;
32-
let whois = JSON.parse(fs.readFileSync("./seo/dump-whois.json")),
33-
repositories = JSON.parse(fs.readFileSync("./seo/dump-repositories.json")),
34-
owners = JSON.parse(fs.readFileSync("./seo/dump-owners.json"));
35-
if (validAddress.test(params.userId)) {
36-
data = find(whois, { address: params.userId });
37-
} else {
38-
data = find(whois, { name: params.userId });
39-
}
40-
if (data?.ownerType === "USER") {
41-
u = find(owners, { address: data.address });
42-
const pr = filter(repositories, (r) => r.owner.id === u.creator);
43-
allRepos = sortBy(pr, (r) => -Number(r.updatedAt));
44-
} else if (data?.ownerType === "DAO") {
45-
d = find(owners, { address: data.address });
46-
const pr = filter(repositories, (r) => r.owner.id === d.address);
47-
allRepos = sortBy(pr, (r) => -Number(r.updatedAt));
48-
}
31+
try {
32+
const fs = (await import("fs")).default;
33+
let whois = JSON.parse(fs.readFileSync("./seo/dump-whois.json")),
34+
repositories = JSON.parse(
35+
fs.readFileSync("./seo/dump-repositories.json")
36+
),
37+
owners = JSON.parse(fs.readFileSync("./seo/dump-owners.json"));
38+
if (validAddress.test(params.userId)) {
39+
data = find(whois, { address: params.userId });
40+
} else {
41+
data = find(whois, { name: params.userId });
42+
}
43+
if (data?.ownerType === "USER") {
44+
u = find(owners, { address: data.address });
45+
const pr = filter(repositories, (r) => r.owner.id === u.creator);
46+
allRepos = sortBy(pr, (r) => -Number(r.updatedAt));
47+
} else if (data?.ownerType === "DAO") {
48+
d = find(owners, { address: data.address });
49+
const pr = filter(repositories, (r) => r.owner.id === d.address);
50+
allRepos = sortBy(pr, (r) => -Number(r.updatedAt));
51+
}
52+
} catch (e) {}
4953

5054
return {
5155
props: { user: u || {}, dao: d || {}, allRepos },
@@ -74,12 +78,14 @@ export async function getStaticProps({ params }) {
7478
// }
7579

7680
export async function getStaticPaths() {
77-
const fs = (await import("fs")).default;
7881
let paths = [];
79-
try {
80-
paths = JSON.parse(fs.readFileSync("./seo/paths-owners.json"));
81-
} catch (e) {
82-
console.error(e);
82+
if (process.env.GENERATE_SEO_PAGES) {
83+
try {
84+
const fs = (await import("fs")).default;
85+
paths = JSON.parse(fs.readFileSync("./seo/paths-owners.json"));
86+
} catch (e) {
87+
console.error(e);
88+
}
8389
}
8490
return {
8591
paths,

0 commit comments

Comments
 (0)