Skip to content

Commit 8b8bfc2

Browse files
Avoid caching temporary docs fallbacks (#1170)
Avoid caching docs fallbacks
1 parent 29ac8d7 commit 8b8bfc2

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

src/utils/docs.functions.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,19 @@ async function readRepoFileOrFallback(
161161
await loadDocumentsServerModule()
162162

163163
try {
164-
return await fetchRepoFile(repo, branch, filePath)
164+
return {
165+
file: await fetchRepoFile(repo, branch, filePath),
166+
isFallback: false,
167+
}
165168
} catch (error) {
166169
if (!isRecoverableGitHubContentError(error)) {
167170
throw error
168171
}
169172

170-
return buildUnavailableFile(filePath)
173+
return {
174+
file: buildUnavailableFile(filePath),
175+
isFallback: true,
176+
}
171177
}
172178
}
173179

@@ -423,19 +429,24 @@ export const fetchDocs = createServerFn({ method: 'GET' })
423429
.validator(repoFileInput)
424430
.handler(async ({ data }: { data: RepoFileRequest }) => {
425431
const { repo, branch, filePath } = data
426-
const file = await readRepoFileOrFallback(repo, branch, filePath)
432+
const result = await readRepoFileOrFallback(repo, branch, filePath)
427433

428-
if (!file) {
434+
if (!result.file) {
429435
throw notFound()
430436
}
431437

432438
const { extractFrontMatter } = await loadDocumentsServerModule()
433-
const frontMatter = extractFrontMatter(file)
439+
const frontMatter = extractFrontMatter(result.file)
434440
const description =
435441
frontMatter.userDescription ?? removeMarkdown(frontMatter.excerpt ?? '')
436442
const keywords = extractFrontMatterKeywords(frontMatter.data.keywords)
437443

438-
setDocsCacheHeaders('public, max-age=3600, stale-while-revalidate=86400')
444+
setDocsCacheHeaders(
445+
result.isFallback
446+
? 'no-store'
447+
: 'public, max-age=3600, stale-while-revalidate=86400',
448+
)
449+
if (result.isFallback) setResponseHeader('Cache-Control', 'no-store')
439450

440451
return {
441452
content: frontMatter.content,
@@ -470,15 +481,20 @@ export const fetchFile = createServerFn({ method: 'GET' })
470481
.validator(repoFileInput)
471482
.handler(async ({ data }: { data: RepoFileRequest }) => {
472483
const { repo, branch, filePath } = data
473-
const file = await readRepoFileOrFallback(repo, branch, filePath)
484+
const result = await readRepoFileOrFallback(repo, branch, filePath)
474485

475-
if (!file) {
486+
if (!result.file) {
476487
throw notFound()
477488
}
478489

479-
setDocsCacheHeaders('public, max-age=300, stale-while-revalidate=300')
490+
setDocsCacheHeaders(
491+
result.isFallback
492+
? 'no-store'
493+
: 'public, max-age=300, stale-while-revalidate=300',
494+
)
495+
if (result.isFallback) setResponseHeader('Cache-Control', 'no-store')
480496

481-
return file
497+
return result.file
482498
})
483499

484500
export const fetchRepoDirectoryContents = createServerFn({

0 commit comments

Comments
 (0)