@@ -2,7 +2,7 @@ import type { Storage } from 'unstorage'
2
2
// @ts -ignore
3
3
import memoryDriver from 'unstorage/drivers/memory'
4
4
import { createStorage , prefixStorage } from 'unstorage'
5
- import { useRuntimeConfig , useCookie } from '#app'
5
+ import { useRuntimeConfig , useCookie , useNuxtApp } from '#app'
6
6
import { withBase } from 'ufo'
7
7
import { createPipelineFetcher } from '../query/match/pipeline'
8
8
import { createQuery } from '../query/query'
@@ -39,8 +39,8 @@ export function createDB (storage: Storage) {
39
39
}
40
40
}
41
41
}
42
-
43
- return Promise . all ( Array . from ( keys ) . map ( key => storage . getItem ( key ) as Promise < ParsedContent > ) )
42
+ const items = await Promise . all ( Array . from ( keys ) . map ( key => storage . getItem ( key ) as Promise < ParsedContent > ) )
43
+ return items
44
44
}
45
45
return {
46
46
storage,
@@ -50,24 +50,45 @@ export function createDB (storage: Storage) {
50
50
}
51
51
52
52
let contentDatabase
53
+ let contentDatabaseInitPromise
53
54
export async function useContentDatabase ( ) {
54
- if ( ! contentDatabase ) {
55
- const { clientDB } = useRuntimeConfig ( ) . public . content
56
- contentDatabase = createDB ( contentStorage )
57
- const integrity = await contentDatabase . storage . getItem ( 'integrity' )
58
- if ( clientDB . integrity !== + integrity ) {
59
- const { contents, navigation } = await $fetch ( withContentBase ( 'cache.json' ) )
60
-
61
- for ( const content of contents ) {
62
- await contentDatabase . storage . setItem ( `cache:${ content . _id } ` , content )
63
- }
55
+ if ( contentDatabaseInitPromise ) {
56
+ await contentDatabaseInitPromise
57
+ } else if ( ! contentDatabase ) {
58
+ contentDatabaseInitPromise = initContentDatabase ( )
59
+ contentDatabase = await contentDatabaseInitPromise
60
+ }
61
+ return contentDatabase
62
+ }
64
63
65
- await contentDatabase . storage . setItem ( 'navigation' , navigation )
64
+ /**
65
+ * Initialize content database
66
+ * - Fetch content from cache api
67
+ * - Call `content:storage` hook to allow plugins to fill storage
68
+ */
69
+ async function initContentDatabase ( ) {
70
+ const nuxtApp = useNuxtApp ( )
71
+ const { clientDB } = useRuntimeConfig ( ) . public . content
66
72
67
- await contentDatabase . storage . setItem ( 'integrity' , clientDB . integrity )
68
- }
73
+ const _contentDatabase = createDB ( contentStorage )
74
+ const integrity = await _contentDatabase . storage . getItem ( 'integrity' )
75
+ if ( clientDB . integrity !== + integrity ) {
76
+ const { contents, navigation } = await $fetch ( withContentBase ( 'cache.json' ) ) as any
77
+
78
+ await Promise . all (
79
+ contents . map ( content => _contentDatabase . storage . setItem ( `cache:${ content . _id } ` , content ) )
80
+ )
81
+
82
+ await _contentDatabase . storage . setItem ( 'navigation' , navigation )
83
+
84
+ await _contentDatabase . storage . setItem ( 'integrity' , clientDB . integrity )
69
85
}
70
- return contentDatabase
86
+
87
+ // call `content:storage` hook to allow plugins to fill storage
88
+ // @ts -ignore
89
+ await nuxtApp . callHook ( 'content:storage' , _contentDatabase . storage )
90
+
91
+ return _contentDatabase
71
92
}
72
93
73
94
export async function generateNavigation ( query ) : Promise < Array < NavItem > > {
0 commit comments