Skip to content

Commit 887a6d0

Browse files
committed
Merge branch 'get-static-contributors' into get-static-contributors-consolidation
2 parents 3500725 + ea4fcba commit 887a6d0

File tree

7 files changed

+27
-44
lines changed

7 files changed

+27
-44
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,3 @@ robots.txt
5555

5656
# Crowdin report output
5757
src/data/crowdin/bucketsAwaitingReviewReport.csv
58-
59-
src/data/git-contributor-cache.json

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
"version": "8.5.2",
44
"private": true,
55
"scripts": {
6-
"dev": "yarn && next dev",
7-
"postinstall": "yarn theme && yarn rm-contributor-cache",
8-
"build": "yarn && next build",
9-
"rm-contributor-cache": "touch ./src/data/git-contributor-cache.json && rm ./src/data/git-contributor-cache.json",
6+
"dev": "next dev",
7+
"postinstall": "yarn theme",
8+
"build": "next build",
109
"postbuild": "next-sitemap",
1110
"start": "next start",
1211
"lint": "next lint",

src/lib/api/fetchGitHistory.ts

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import fs from "fs"
2-
import { join } from "path"
3-
4-
import type { FileContributor } from "@/lib/types"
1+
import type { CommitHistory, FileContributor } from "@/lib/types"
52

63
import {
74
CONTENT_DIR,
8-
GIT_CONTRIBUTOR_CACHE_JSON,
95
GITHUB_COMMITS_URL,
106
OLD_CONTENT_DIR,
117
} from "@/lib/constants"
@@ -50,27 +46,11 @@ async function fetchWithRateLimit(
5046

5147
// Fetch commit history and save it to a JSON file
5248
export const fetchAndCacheGitContributors = async (
53-
mdDir: string
54-
): Promise<FileContributor[]> => {
55-
const filepath = join("/", mdDir, "index.md")
56-
57-
// Load cache
58-
let commitHistory = {}
59-
if (fs.existsSync(GIT_CONTRIBUTOR_CACHE_JSON)) {
60-
try {
61-
commitHistory = JSON.parse(
62-
fs.readFileSync(GIT_CONTRIBUTOR_CACHE_JSON, "utf8")
63-
)
64-
} catch (error) {
65-
console.error(
66-
`Error reading commit history cache for filepath ${filepath}`,
67-
error
68-
)
69-
}
70-
}
71-
49+
filepath: string,
50+
cache: CommitHistory
51+
) => {
7252
// First, check cache for existing commit history for English version (despite locale)
73-
if (commitHistory[filepath]) return commitHistory[filepath]
53+
if (cache[filepath]) return cache[filepath]
7454

7555
// Fetch and save commit history for file
7656
const history = (await fetchWithRateLimit(filepath)) || []
@@ -94,12 +74,8 @@ export const fetchAndCacheGitContributors = async (
9474
index === self.findIndex((t) => t.login === contributor.login)
9575
)
9676

97-
// Amend to commitHistory log and save
98-
commitHistory[filepath] = uniqueContributors
99-
fs.writeFileSync(
100-
GIT_CONTRIBUTOR_CACHE_JSON,
101-
JSON.stringify(commitHistory, null, 2)
102-
)
77+
// Amend to cache
78+
cache[filepath] = uniqueContributors
10379

10480
return uniqueContributors
10581
}

src/lib/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,3 @@ export const DEFAULT_GLOSSARY_NS = "glossary"
149149
export const HAMBURGER_BUTTON_ID = "mobile-menu-button"
150150
export const MOBILE_LANGUAGE_BUTTON_NAME = "mobile-language-button"
151151
export const DESKTOP_LANGUAGE_BUTTON_NAME = "desktop-language-button"
152-
153-
export const GIT_CONTRIBUTOR_CACHE_JSON = "src/data/git-contributor-cache.json"

src/lib/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ export type FileContributor = {
377377
date?: string
378378
}
379379

380+
type FilePath = string
381+
export type CommitHistory = Record<FilePath, FileContributor[]>
382+
380383
/**
381384
* Table of contents
382385
*/

src/lib/utils/contributors.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import type { FileContributor, Lang, Layout } from "@/lib/types"
1+
import { join } from "path"
2+
3+
import type { CommitHistory, FileContributor, Lang, Layout } from "@/lib/types"
24

35
import { DEFAULT_LOCALE } from "@/lib/constants"
46

@@ -16,9 +18,13 @@ export const getFileContributorInfo = async (
1618
slug: string,
1719
locale: string,
1820
fileLang: string,
19-
layout: Layout
21+
layout: Layout,
22+
cache: CommitHistory
2023
) => {
21-
const gitContributors = await fetchAndCacheGitContributors(mdDir)
24+
const gitContributors = await fetchAndCacheGitContributors(
25+
join("/", mdDir, "index.md"),
26+
cache
27+
)
2228

2329
const latestCommitDate = getLastModifiedDate(slug, locale!)
2430
const gitHubLastEdit = gitContributors[0]?.date

src/pages/[...slug].tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import readingTime from "reading-time"
1616
import remarkGfm from "remark-gfm"
1717

1818
import type {
19-
Lang,
19+
CommitHistory,
2020
Layout,
2121
LayoutMappingType,
2222
NextPageWithLayout,
@@ -113,6 +113,8 @@ const gfIssuesDataFetch = runOnlyOnce(async () => {
113113
return await fetchGFIs()
114114
})
115115

116+
const commitHistoryCache: CommitHistory = {}
117+
116118
export const getStaticProps = (async (context) => {
117119
const params = context.params!
118120
const { locale } = context
@@ -178,7 +180,8 @@ export const getStaticProps = (async (context) => {
178180
slug,
179181
locale!,
180182
frontmatter.lang,
181-
layout
183+
layout,
184+
commitHistoryCache
182185
)
183186

184187
const gfissues = await gfIssuesDataFetch()

0 commit comments

Comments
 (0)