-
-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy path$version.docs.framework.$framework.$.tsx
More file actions
116 lines (107 loc) · 3.49 KB
/
$version.docs.framework.$framework.$.tsx
File metadata and controls
116 lines (107 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {
isNotFound,
redirect,
useLocation,
useMatch,
createFileRoute,
} from '@tanstack/react-router'
import { seo } from '~/utils/seo'
import { Doc } from '~/components/Doc'
import { loadDocsPage, resolveDocsRedirect } from '~/utils/docs'
import { getBranch, getLibrary } from '~/libraries'
import { capitalize } from '~/utils/utils'
import { DocContainer } from '~/components/DocContainer'
import type { ConfigSchema } from '~/utils/config'
export const Route = createFileRoute(
'/$libraryId/$version/docs/framework/$framework/$',
)({
staleTime: 1000 * 60 * 5,
loader: async (ctx) => {
const { _splat: docsPath, framework, version, libraryId } = ctx.params
const library = getLibrary(libraryId)
const branch = getBranch(library, version)
const docsRoot = library.docsRoot || 'docs'
try {
return await loadDocsPage({
repo: library.repo,
branch,
docsRoot,
docsPath: `framework/${framework}/${docsPath ?? ''}`,
})
} catch (error) {
// If doc not found, redirect to framework docs root instead of showing 404
// This handles cases like switching frameworks where the same doc path doesn't exist
// Check both isNotFound() and the serialized form from server functions
const isNotFoundError =
isNotFound(error) ||
(error && typeof error === 'object' && 'isNotFound' in error)
if (isNotFoundError) {
const redirectPath = await resolveDocsRedirect({
repo: library.repo,
branch,
docsRoot,
docsPaths: docsPath
? [`framework/${framework}/${docsPath}`, `${framework}/${docsPath}`]
: [],
})
if (redirectPath !== null) {
throw redirect({
href: `/${libraryId}/${version}/docs${redirectPath ? `/${redirectPath}` : ''}`,
statusCode: 308,
})
}
throw redirect({
to: '/$libraryId/$version/docs/framework/$framework',
params: { libraryId, version, framework },
})
}
throw error
}
},
component: Docs,
head: (ctx) => {
const library = getLibrary(ctx.params.libraryId)
const tail = `${library.name} ${capitalize(ctx.params.framework)} Docs`
return {
meta: seo({
title: ctx.loaderData?.title
? `${ctx.loaderData.title} | ${tail}`
: tail,
description: ctx.loaderData?.description,
keywords: ctx.loaderData?.keywords,
noindex: library.visible === false,
}),
}
},
})
function Docs() {
const { contentRsc, filePath, headings, title } = Route.useLoaderData()
const versionMatch = useMatch({ from: '/$libraryId/$version' })
const { config } = versionMatch.loaderData as { config: ConfigSchema }
const { version, libraryId, framework } = Route.useParams()
const library = getLibrary(libraryId)
const branch = getBranch(library, version)
const location = useLocation()
return (
<DocContainer>
<Doc
key={filePath}
title={title}
contentRsc={contentRsc}
repo={library.repo}
branch={branch}
filePath={filePath}
headings={headings}
colorFrom={library.colorFrom}
colorTo={library.colorTo}
textColor={library.textColor}
shouldRenderToc
libraryId={libraryId}
libraryVersion={version === 'latest' ? library.latestVersion : version}
pagePath={location.pathname}
config={config}
framework={framework}
/>
</DocContainer>
)
}