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' ;
103import { GlobalStore } from '../../context' ;
114import { CloseIcon } from '../svgs/close-icon' ;
125import styles from './sidebar.css?inline' ;
136
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 (
229 Object . entries ( import . meta. glob < { frontmatter ?: MDX } > ( '../../routes/**/*.{md,mdx}' ) ) . map (
2310 async ( [ k , v ] ) => {
2411 return [
@@ -33,22 +20,20 @@ export const getMarkdownItems = server$(() => {
3320 ] as const ;
3421 }
3522 )
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+ }
5034 } ) ;
51- return markdownItemsPromise ;
35+
36+ return markdownItems ;
5237} ) ;
5338
5439type MarkdownItems = Record < string , MDX > ;
@@ -79,7 +64,7 @@ export const SideBar = component$((props: { allOpen?: boolean }) => {
7964 const globalStore = useContext ( GlobalStore ) ;
8065 const { menu } = useContent ( ) ;
8166 const { url } = useLocation ( ) ;
82- const markdownItems = useComputed$ ( ( ) => getMarkdownItems ( ) ) ;
67+ const markdownItems = useMarkdownItems ( ) ;
8368 const allOpen = url . pathname . startsWith ( '/qwikcity/' ) || props . allOpen ;
8469
8570 useOnDocument (
@@ -93,7 +78,7 @@ export const SideBar = component$((props: { allOpen?: boolean }) => {
9378 el . scrollTop = savedScroll ;
9479 el . style . visibility = 'visible' ;
9580 }
96- } catch {
81+ } catch ( err ) {
9782 //
9883 }
9984 } )
@@ -118,7 +103,7 @@ export const SideBar = component$((props: { allOpen?: boolean }) => {
118103 try {
119104 const scrollTop = document . getElementById ( 'qwik-sidebar' ) ! . scrollTop ;
120105 sessionStorage . setItem ( 'qwik-sidebar' , String ( scrollTop ) ) ;
121- } catch {
106+ } catch ( err ) {
122107 //
123108 }
124109 } ) }
0 commit comments