Skip to content

Commit ea4fcba

Browse files
committed
refactor: use memory for caching
1 parent f919d23 commit ea4fcba

File tree

6 files changed

+21
-39
lines changed

6 files changed

+21
-39
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: 8 additions & 30 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"
@@ -49,26 +45,12 @@ async function fetchWithRateLimit(
4945
}
5046

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

7355
// Fetch and save commit history for file
7456
const history = (await fetchWithRateLimit(filepath)) || []
@@ -92,12 +74,8 @@ export const fetchAndCacheGitContributors = async (mdDir: string) => {
9274
index === self.findIndex((t) => t.login === contributor.login)
9375
)
9476

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

10280
return uniqueContributors
10381
}

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/pages/[...slug].tsx

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

1818
import type {
19+
CommitHistory,
1920
Lang,
2021
Layout,
2122
LayoutMappingType,
@@ -115,6 +116,8 @@ const gfIssuesDataFetch = runOnlyOnce(async () => {
115116
return await fetchGFIs()
116117
})
117118

119+
const commitHistoryCache: CommitHistory = {}
120+
118121
export const getStaticProps = (async (context) => {
119122
const params = context.params!
120123
const { locale } = context
@@ -181,7 +184,10 @@ export const getStaticProps = (async (context) => {
181184

182185
const gfissues = await gfIssuesDataFetch()
183186

184-
const gitContributors = await fetchAndCacheGitContributors(mdDir)
187+
const gitContributors = await fetchAndCacheGitContributors(
188+
join("/", mdDir, "index.md"),
189+
commitHistoryCache
190+
)
185191

186192
return {
187193
props: {

0 commit comments

Comments
 (0)