@@ -9,18 +9,27 @@ import { createServerFn } from '@tanstack/start'
99import { z } from 'zod'
1010import { setHeader } from 'vinxi/http'
1111
12+ const FetchDocsDataSchema = z . object ( {
13+ repo : z . string ( ) ,
14+ branch : z . string ( ) ,
15+ filePath : z . string ( ) ,
16+ } )
17+ type FetchDocsData = z . infer < typeof FetchDocsDataSchema >
18+
1219export const loadDocs = async ( {
1320 repo,
1421 branch,
1522 // currentPath,
1623 // redirectPath,
1724 docsPath,
25+ useServerFn = true ,
1826} : {
1927 repo : string
2028 branch : string
2129 docsPath : string
2230 currentPath : string
2331 redirectPath : string
32+ useServerFn ?: boolean
2433} ) => {
2534 if ( ! branch ) {
2635 throw new Error ( 'Invalid branch' )
@@ -32,55 +41,59 @@ export const loadDocs = async ({
3241
3342 const filePath = `${ docsPath } .md`
3443
35- return await fetchDocs ( {
44+ const params : { data : FetchDocsData } = {
3645 data : {
3746 repo,
3847 branch,
3948 filePath,
40- // currentPath,
41- // redirectPath,
4249 } ,
43- } )
50+ }
51+
52+ return useServerFn ? fetchDocs ( params ) : fetchDocsFunction ( params )
4453}
4554
46- export const fetchDocs = createServerFn ( { method : 'GET' } )
47- . validator (
48- z . object ( { repo : z . string ( ) , branch : z . string ( ) , filePath : z . string ( ) } )
49- )
50- . handler ( async ( { data : { repo, branch, filePath } } ) => {
51- const file = await fetchRepoFile ( repo , branch , filePath )
55+ const fetchDocsFunction = async ( {
56+ data : { repo, branch, filePath } ,
57+ } : {
58+ data : FetchDocsData
59+ } ) => {
60+ const file = await fetchRepoFile ( repo , branch , filePath )
61+
62+ if ( ! file ) {
63+ throw notFound ( )
64+ // if (currentPath === redirectPath) {
65+ // // console.log('not found')
66+ // throw notFound()
67+ // } else {
68+ // // console.log('redirect')
69+ // throw redirect({
70+ // to: redirectPath,
71+ // })
72+ // }
73+ }
5274
53- if ( ! file ) {
54- throw notFound ( )
55- // if (currentPath === redirectPath) {
56- // // console.log('not found')
57- // throw notFound()
58- // } else {
59- // // console.log('redirect')
60- // throw redirect({
61- // to: redirectPath,
62- // })
63- // }
64- }
75+ const frontMatter = extractFrontMatter ( file )
76+ const description = removeMarkdown ( frontMatter . excerpt ?? '' )
6577
66- const frontMatter = extractFrontMatter ( file )
67- const description = removeMarkdown ( frontMatter . excerpt ?? '' )
78+ // Cache for 5 minutes on shared cache
79+ // Revalidate in the background
80+ setHeader ( 'Cache-Control' , 'public, max-age=0, must-revalidate' )
81+ setHeader (
82+ 'CDN-Cache-Control' ,
83+ 'max-age=300, stale-while-revalidate=300, durable'
84+ )
6885
69- // Cache for 5 minutes on shared cache
70- // Revalidate in the background
71- setHeader ( 'Cache-Control' , 'public, max-age=0, must-revalidate' )
72- setHeader (
73- 'CDN-Cache-Control' ,
74- 'max-age=300, stale-while-revalidate=300, durable'
75- )
86+ return {
87+ title : frontMatter . data ?. title ,
88+ description ,
89+ filePath ,
90+ content : frontMatter . content ,
91+ }
92+ }
7693
77- return {
78- title : frontMatter . data ?. title ,
79- description,
80- filePath,
81- content : frontMatter . content ,
82- }
83- } )
94+ export const fetchDocs = createServerFn ( { method : 'GET' } )
95+ . validator ( FetchDocsDataSchema )
96+ . handler ( fetchDocsFunction )
8497
8598export const fetchFile = createServerFn ( { method : 'GET' } )
8699 . validator (
0 commit comments