Skip to content

Commit fe9c786

Browse files
committed
readPublicFileをキャッシュ
1 parent 7c87b28 commit fe9c786

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

app/lib/docs.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,26 @@ export interface SectionRevision {
112112
path: string;
113113
}
114114

115+
const publicFileCache = new Map<string, Promise<string>>();
115116
async function readPublicFile(path: string): Promise<string> {
116117
try {
117118
if (isCloudflare()) {
118-
const cfAssets = getCloudflareContext().env.ASSETS;
119-
const res = await cfAssets!.fetch(`https://assets.local/${path}`);
120-
if (!res.ok) {
121-
console.error(
122-
`Failed to fetch ${path}: ${res.status} ${await res.text()}`
123-
);
124-
notFound();
119+
if (publicFileCache.has(path)) {
120+
return publicFileCache.get(path)!;
125121
}
126-
return await res.text();
122+
const p = (async () => {
123+
const cfAssets = getCloudflareContext().env.ASSETS;
124+
const res = await cfAssets!.fetch(`https://assets.local/${path}`);
125+
if (!res.ok) {
126+
console.error(
127+
`Failed to fetch ${path}: ${res.status} ${await res.text()}`
128+
);
129+
notFound();
130+
}
131+
return await res.text();
132+
})();
133+
publicFileCache.set(path, p);
134+
return p;
127135
} else {
128136
return await readFile(join(process.cwd(), "public", path), "utf-8");
129137
}
@@ -203,7 +211,9 @@ export async function getMarkdownSections(
203211
}
204212
return a.localeCompare(b);
205213
}
206-
const files = (await readdir(join(process.cwd(), "public", "docs", lang, page)))
214+
const files = (
215+
await readdir(join(process.cwd(), "public", "docs", lang, page))
216+
)
207217
.filter((f) => f.endsWith(".md"))
208218
.sort(naturalSortMdFiles);
209219

0 commit comments

Comments
 (0)