1
- import {
2
- component$ ,
3
- sync$ ,
4
- useComputed$ ,
5
- useContext ,
6
- useOnDocument ,
7
- useStyles$ ,
8
- } from '@builder.io/qwik' ;
9
- import { server$ , useContent , useLocation , type ContentMenu } from '@builder.io/qwik-city' ;
1
+ import { component$ , sync$ , useContext , useOnDocument , useStyles$ } from '@builder.io/qwik' ;
2
+ import { type ContentMenu , useContent , useLocation , routeLoader$ } from '@builder.io/qwik-city' ;
10
3
import { GlobalStore } from '../../context' ;
11
4
import { CloseIcon } from '../svgs/close-icon' ;
12
5
import styles from './sidebar.css?inline' ;
13
6
14
- let markdownItems : MarkdownItems | undefined ;
15
- let markdownItemsPromise : Promise < MarkdownItems > | undefined ;
16
- export const getMarkdownItems = server$ ( ( ) => {
17
- if ( markdownItems ) {
18
- return markdownItems ;
19
- }
20
-
21
- markdownItemsPromise ||= Promise . all (
7
+ export const useMarkdownItems = routeLoader$ ( async ( ) => {
8
+ const rawData = await Promise . all (
22
9
Object . entries ( import . meta. glob < { frontmatter ?: MDX } > ( '../../routes/**/*.{md,mdx}' ) ) . map (
23
10
async ( [ k , v ] ) => {
24
11
return [
@@ -33,22 +20,20 @@ export const getMarkdownItems = server$(() => {
33
20
] as const ;
34
21
}
35
22
)
36
- ) . then ( ( rawData ) => {
37
- markdownItems = { } ;
38
- rawData . map ( ( [ k , v ] ) => {
39
- if ( v . frontmatter ?. updated_at ) {
40
- markdownItems ! [ k ] = {
41
- title : v . frontmatter . title ,
42
- contributors : v . frontmatter . contributors ,
43
- created_at : v . frontmatter . created_at ,
44
- updated_at : v . frontmatter . updated_at ,
45
- } ;
46
- }
47
- } ) ;
48
-
49
- return markdownItems ;
23
+ ) ;
24
+ const markdownItems : MarkdownItems = { } ;
25
+ rawData . map ( ( [ k , v ] ) => {
26
+ if ( v . frontmatter ?. updated_at ) {
27
+ markdownItems [ k ] = {
28
+ title : v . frontmatter . title ,
29
+ contributors : v . frontmatter . contributors ,
30
+ created_at : v . frontmatter . created_at ,
31
+ updated_at : v . frontmatter . updated_at ,
32
+ } ;
33
+ }
50
34
} ) ;
51
- return markdownItemsPromise ;
35
+
36
+ return markdownItems ;
52
37
} ) ;
53
38
54
39
type MarkdownItems = Record < string , MDX > ;
@@ -79,7 +64,7 @@ export const SideBar = component$((props: { allOpen?: boolean }) => {
79
64
const globalStore = useContext ( GlobalStore ) ;
80
65
const { menu } = useContent ( ) ;
81
66
const { url } = useLocation ( ) ;
82
- const markdownItems = useComputed$ ( ( ) => getMarkdownItems ( ) ) ;
67
+ const markdownItems = useMarkdownItems ( ) ;
83
68
const allOpen = url . pathname . startsWith ( '/qwikcity/' ) || props . allOpen ;
84
69
85
70
useOnDocument (
@@ -93,7 +78,7 @@ export const SideBar = component$((props: { allOpen?: boolean }) => {
93
78
el . scrollTop = savedScroll ;
94
79
el . style . visibility = 'visible' ;
95
80
}
96
- } catch {
81
+ } catch ( err ) {
97
82
//
98
83
}
99
84
} )
@@ -118,7 +103,7 @@ export const SideBar = component$((props: { allOpen?: boolean }) => {
118
103
try {
119
104
const scrollTop = document . getElementById ( 'qwik-sidebar' ) ! . scrollTop ;
120
105
sessionStorage . setItem ( 'qwik-sidebar' , String ( scrollTop ) ) ;
121
- } catch {
106
+ } catch ( err ) {
122
107
//
123
108
}
124
109
} ) }
0 commit comments