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