@@ -45,38 +45,17 @@ const repoDirApiContentsQueryOptions = (
45
45
} )
46
46
47
47
export const Route = createFileRoute ( {
48
- head : ( { params } ) => {
49
- const library = getLibrary ( params . libraryId )
50
-
51
- return {
52
- meta : seo ( {
53
- title : `${ capitalize ( params . framework ) } ${ library . name } ${ slugToTitle (
54
- params . _splat || ''
55
- ) } Example | ${ library . name } Docs`,
56
- description : `An example showing how to implement ${ slugToTitle (
57
- params . _splat || ''
58
- ) } in ${ capitalize ( params . framework ) } using ${ library . name } .`,
59
- } ) ,
60
- }
61
- } ,
62
48
component : RouteComponent ,
63
49
validateSearch : z . object ( {
64
50
path : z . string ( ) . optional ( ) ,
65
51
panel : z . string ( ) . optional ( ) ,
66
52
} ) ,
67
- loader : async ( { params, context : { queryClient } } ) => {
53
+ loaderDeps : ( { search } ) => ( { path : search . path } ) ,
54
+ loader : async ( { params, context : { queryClient } , deps : { path } } ) => {
68
55
const library = getLibrary ( params . libraryId )
69
56
const branch = getBranch ( library , params . version )
70
57
const examplePath = [ params . framework , params . _splat ] . join ( '/' )
71
58
72
- // Used to determine the starting file name for the explorer
73
- // i.e. app.tsx, main.tsx, src/routes/__root.tsx, etc.
74
- // This value is not absolutely guaranteed to be available, so further resolution may be necessary
75
- const explorerCandidateStartingFileName = getExampleStartingPath (
76
- params . framework as Framework ,
77
- params . libraryId
78
- )
79
-
80
59
// Used to tell the github contents api where to start looking for files in the target repository
81
60
const repoStartingDirPath = `examples/${ examplePath } `
82
61
@@ -85,9 +64,17 @@ export const Route = createFileRoute({
85
64
repoDirApiContentsQueryOptions ( library . repo , branch , repoStartingDirPath )
86
65
)
87
66
67
+ // Used to determine the starting file name for the explorer
68
+ // It's either the selected path in the search params or a default we can derive
69
+ // i.e. app.tsx, main.tsx, src/routes/__root.tsx, etc.
70
+ // This value is not absolutely guaranteed to be available, so further resolution may be necessary
71
+ const explorerCandidateStartingFileName =
72
+ path ||
73
+ getExampleStartingPath ( params . framework as Framework , params . libraryId )
74
+
88
75
// Using the fetched contents, get the actual starting file-path for the explorer
89
76
// The `explorerCandidateStartingFileName` is used for matching, but the actual file-path may differ
90
- const repoStartingFilePath = determineStartingFilePath (
77
+ const currentPath = determineStartingFilePath (
91
78
githubContents ,
92
79
explorerCandidateStartingFileName ,
93
80
params . framework as Framework ,
@@ -97,12 +84,23 @@ export const Route = createFileRoute({
97
84
// Now that we've resolved the starting file path, we can
98
85
// fetching and caching the file content for the starting file path
99
86
await queryClient . ensureQueryData (
100
- fileQueryOptions ( library . repo , branch , repoStartingFilePath )
87
+ fileQueryOptions ( library . repo , branch , currentPath )
101
88
)
102
89
90
+ return { repoStartingDirPath, currentPath }
91
+ } ,
92
+ head : ( { params } ) => {
93
+ const library = getLibrary ( params . libraryId )
94
+
103
95
return {
104
- repoStartingDirPath,
105
- repoStartingFilePath,
96
+ meta : seo ( {
97
+ title : `${ capitalize ( params . framework ) } ${ library . name } ${ slugToTitle (
98
+ params . _splat || ''
99
+ ) } Example | ${ library . name } Docs`,
100
+ description : `An example showing how to implement ${ slugToTitle (
101
+ params . _splat || ''
102
+ ) } in ${ capitalize ( params . framework ) } using ${ library . name } .`,
103
+ } ) ,
106
104
}
107
105
} ,
108
106
staleTime : 1000 * 60 * 5 , // 5 minutes
@@ -114,9 +112,7 @@ function RouteComponent() {
114
112
}
115
113
116
114
function PageComponent ( ) {
117
- // Not sure why this inferred type is not working
118
- // @ts -expect-error
119
- const { repoStartingDirPath, repoStartingFilePath } = Route . useLoaderData ( )
115
+ const { repoStartingDirPath, currentPath } = Route . useLoaderData ( )
120
116
121
117
const navigate = Route . useNavigate ( )
122
118
const queryClient = useQueryClient ( )
@@ -158,12 +154,6 @@ function PageComponent() {
158
154
} )
159
155
}
160
156
161
- const currentPath = Route . useSearch ( {
162
- select : ( s ) => {
163
- return s . path || repoStartingFilePath
164
- } ,
165
- } )
166
-
167
157
const setCurrentPath = ( path : string ) => {
168
158
navigate ( {
169
159
search : { path, panel : undefined } ,
0 commit comments