Skip to content

Commit 6c0012e

Browse files
authored
fix: keep docs server imports out of client bundle path
1 parent a497420 commit 6c0012e

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

src/utils/docs.functions.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,12 @@ import { createServerFn } from '@tanstack/react-start'
33
import { setResponseHeader } from '@tanstack/react-start/server'
44
import removeMarkdown from 'remove-markdown'
55
import * as v from 'valibot'
6-
import {
7-
extractFrontMatter,
8-
fetchApiContents,
9-
fetchRepoFile,
10-
isRecoverableGitHubContentError,
11-
shouldUseLocalDocsFiles,
12-
} from '~/utils/documents.server'
136
import { extractFrameworksFromMarkdown } from './markdown/filterFrameworkContent'
14-
import { getCachedDocsArtifact } from './github-content-cache.server'
157
import { buildRedirectManifest, type RedirectManifestEntry } from './redirects'
168
import { isValidRepoPath, MAX_REPO_PATH_LENGTH } from './repo-path'
179
import { removeLeadingSlash } from './utils'
1810
import type { DocsRedirectManifest } from './docs-redirects'
11+
import type { GitHubFileNode } from './documents.server'
1912

2013
export type DocsTreeNode = {
2114
path: string
@@ -123,6 +116,14 @@ const temporarilyUnavailableMarkdown = `# Content temporarily unavailable
123116
124117
We are having trouble fetching this document from GitHub right now. Please try again in a minute.`
125118

119+
async function loadDocumentsServerModule() {
120+
return import('./documents.server')
121+
}
122+
123+
async function loadGitHubContentCacheServerModule() {
124+
return import('./github-content-cache.server')
125+
}
126+
126127
function buildUnavailableFile(filePath: string) {
127128
if (filePath.toLowerCase().endsWith('.md')) {
128129
return temporarilyUnavailableMarkdown
@@ -136,6 +137,9 @@ async function readRepoFileOrFallback(
136137
branch: string,
137138
filePath: string,
138139
) {
140+
const { fetchRepoFile, isRecoverableGitHubContentError } =
141+
await loadDocumentsServerModule()
142+
139143
try {
140144
return await fetchRepoFile(repo, branch, filePath)
141145
} catch (error) {
@@ -183,6 +187,8 @@ export async function collectRedirectEntriesForFile(
183187
onCanonicalPath: (canonicalPath: string) => void
184188
},
185189
): Promise<Array<RedirectManifestEntry>> {
190+
const { extractFrontMatter, isRecoverableGitHubContentError } =
191+
await loadDocumentsServerModule()
186192
const canonicalPath = getCanonicalDocsPath(node.path, opts.docsRoot)
187193

188194
if (canonicalPath === null) {
@@ -238,6 +244,7 @@ async function buildDocsManifest({
238244
branch: string
239245
docsRoot: string
240246
}): Promise<DocsManifest> {
247+
const { fetchApiContents, fetchRepoFile } = await loadDocumentsServerModule()
241248
const nodes = await fetchApiContents(repo, branch, docsRoot)
242249

243250
if (!nodes) {
@@ -279,6 +286,7 @@ async function buildDocsPathManifest({
279286
branch: string
280287
docsRoot: string
281288
}): Promise<DocsManifest> {
289+
const { fetchApiContents } = await loadDocumentsServerModule()
282290
const nodes = await fetchApiContents(repo, branch, docsRoot)
283291

284292
if (!nodes) {
@@ -302,6 +310,11 @@ export const fetchDocsManifest = createServerFn({ method: 'GET' })
302310
.validator(docsManifestInput)
303311
.handler(async ({ data }) => {
304312
const { repo, branch, docsRoot } = data
313+
const [{ shouldUseLocalDocsFiles }, { getCachedDocsArtifact }] =
314+
await Promise.all([
315+
loadDocumentsServerModule(),
316+
loadGitHubContentCacheServerModule(),
317+
])
305318

306319
if (shouldUseLocalDocsFiles()) {
307320
return buildDocsManifest({ repo, branch, docsRoot })
@@ -322,6 +335,11 @@ export const fetchDocsPathManifest = createServerFn({ method: 'GET' })
322335
.validator(docsManifestInput)
323336
.handler(async ({ data }) => {
324337
const { repo, branch, docsRoot } = data
338+
const [{ shouldUseLocalDocsFiles }, { getCachedDocsArtifact }] =
339+
await Promise.all([
340+
loadDocumentsServerModule(),
341+
loadGitHubContentCacheServerModule(),
342+
])
325343

326344
if (shouldUseLocalDocsFiles()) {
327345
return buildDocsPathManifest({ repo, branch, docsRoot })
@@ -341,6 +359,7 @@ export const fetchDocsPathManifest = createServerFn({ method: 'GET' })
341359
export const fetchDocsRedirect = createServerFn({ method: 'GET' })
342360
.validator(docsRedirectInput)
343361
.handler(async ({ data }) => {
362+
const { isRecoverableGitHubContentError } = await loadDocumentsServerModule()
344363
let manifest: DocsManifest
345364

346365
try {
@@ -389,6 +408,7 @@ export const fetchDocs = createServerFn({ method: 'GET' })
389408
throw notFound()
390409
}
391410

411+
const { extractFrontMatter } = await loadDocumentsServerModule()
392412
const frontMatter = extractFrontMatter(file)
393413
const description =
394414
frontMatter.userDescription ?? removeMarkdown(frontMatter.excerpt ?? '')
@@ -446,7 +466,9 @@ export const fetchRepoDirectoryContents = createServerFn({
446466
.validator(repoDirectoryInput)
447467
.handler(async ({ data }: { data: RepoDirectoryRequest }) => {
448468
const { repo, branch, startingPath } = data
449-
let githubContents: Awaited<ReturnType<typeof fetchApiContents>>
469+
const { fetchApiContents, isRecoverableGitHubContentError } =
470+
await loadDocumentsServerModule()
471+
let githubContents: Array<GitHubFileNode> | null
450472

451473
try {
452474
githubContents = await fetchApiContents(repo, branch, startingPath)

0 commit comments

Comments
 (0)