Skip to content

Commit 6f06f35

Browse files
committed
fix: use runtimeconfig only when necessary
1 parent 80b4cc1 commit 6f06f35

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

src/runtime/server/storage.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type StorageValue, prefixStorage } from 'unstorage'
1+
import { type StorageValue, prefixStorage, type Storage } from 'unstorage'
22
import { joinURL, withLeadingSlash, withoutTrailingSlash } from 'ufo'
33
import { hash as ohash } from 'ohash'
44
import type { H3Event } from 'h3'
@@ -33,19 +33,32 @@ interface ParseContentOptions {
3333
[key: string]: any
3434
}
3535

36-
export const sourceStorage = () => prefixStorage(useStorage(), 'content:source')
37-
export const cacheStorage = () => prefixStorage(useStorage(), 'cache:content')
38-
export const cacheParsedStorage = () => prefixStorage(useStorage(), 'cache:content:parsed')
36+
let _sourceStorage: Storage
37+
let _cacheStorage: Storage
38+
let _cacheParsedStorage: Storage
39+
export const sourceStorage = () => {
40+
if (!_sourceStorage) {
41+
_sourceStorage = prefixStorage(useStorage(), 'content:source')
42+
}
43+
return _sourceStorage
44+
}
45+
export const cacheStorage = () => {
46+
if (!_cacheStorage) {
47+
_cacheStorage = prefixStorage(useStorage(), 'cache:content')
48+
}
49+
return _cacheStorage
50+
}
51+
export const cacheParsedStorage = () => {
52+
if (!_cacheParsedStorage) {
53+
_cacheParsedStorage = prefixStorage(useStorage(), 'cache:content:parsed')
54+
}
55+
return _cacheParsedStorage
56+
}
3957

4058
const isProduction = process.env.NODE_ENV === 'production'
4159
const isPrerendering = import.meta.prerender
4260

43-
const contentConfig = useRuntimeConfig().content
44-
45-
/**
46-
* Content ignore patterns
47-
*/
48-
const isIgnored = makeIgnored(contentConfig.ignores)
61+
const contentConfig = () => useRuntimeConfig().content
4962

5063
/**
5164
* Invalid key characters
@@ -56,6 +69,7 @@ const invalidKeyCharacters = "'\"?#/".split('')
5669
* Filter predicate for ignore patterns
5770
*/
5871
const contentIgnorePredicate = (key: string) => {
72+
const isIgnored = makeIgnored(contentConfig().ignores)
5973
if (key.startsWith('preview:') || isIgnored(key)) {
6074
return false
6175
}
@@ -174,6 +188,7 @@ export const getContent = async (event: H3Event, id: string): Promise<ParsedCont
174188
return cached.parsed
175189
}
176190

191+
const config = contentConfig()
177192
const meta = await source.getMeta(id)
178193
const mtime = meta.mtime
179194
const size = meta.size || 0
@@ -183,8 +198,8 @@ export const getContent = async (event: H3Event, id: string): Promise<ParsedCont
183198
// File size
184199
size,
185200
// Add Content version to the hash, to revalidate the cache on content update
186-
version: contentConfig.cacheVersion,
187-
integrity: contentConfig.cacheIntegrity
201+
version: config.cacheVersion,
202+
integrity: config.cacheIntegrity
188203
})
189204
if (cached?.hash === hash) {
190205
return cached.parsed as ParsedContent
@@ -218,20 +233,21 @@ export const getContent = async (event: H3Event, id: string): Promise<ParsedCont
218233
*/
219234
export const parseContent = async (id: string, content: StorageValue, opts: ParseContentOptions = {}) => {
220235
const nitroApp = useNitroApp()
236+
const config = contentConfig()
221237
const options = defu(
222238
opts,
223239
{
224240
markdown: {
225-
...contentConfig.markdown,
226-
highlight: contentConfig.highlight
241+
...config.markdown,
242+
highlight: config.highlight
227243
},
228-
csv: contentConfig.csv,
229-
yaml: contentConfig.yaml,
244+
csv: config.csv,
245+
yaml: config.yaml,
230246
transformers: customTransformers,
231247
pathMeta: {
232-
defaultLocale: contentConfig.defaultLocale,
233-
locales: contentConfig.locales,
234-
respectPathCase: contentConfig.respectPathCase
248+
defaultLocale: config.defaultLocale,
249+
locales: config.locales,
250+
respectPathCase: config.respectPathCase
235251
}
236252
}
237253
)
@@ -259,6 +275,7 @@ export function serverQueryContent<T = ParsedContent>(event: H3Event, params?: C
259275
export function serverQueryContent<T = ParsedContent>(event: H3Event, query?: string, ...pathParts: string[]): ContentQueryBuilder<T>;
260276
export function serverQueryContent<T = ParsedContent> (event: H3Event, query?: string | ContentQueryBuilderParams, ...pathParts: string[]) {
261277
const { advanceQuery } = useRuntimeConfig().public.content.experimental
278+
const config = contentConfig()
262279
const queryBuilder = advanceQuery
263280
? createQuery<T>(createServerQueryFetch(event), { initialParams: typeof query !== 'string' ? query || {} : {}, legacy: false })
264281
: createQuery<T>(createServerQueryFetch(event), { initialParams: typeof query !== 'string' ? query || {} : {}, legacy: true })
@@ -299,11 +316,11 @@ export function serverQueryContent<T = ParsedContent> (event: H3Event, query?: s
299316
// Filter by locale if:
300317
// - locales are defined
301318
// - query doesn't already have a locale filter
302-
if (contentConfig.locales.length) {
319+
if (config.locales.length) {
303320
const queryLocale = params.where?.find(w => w._locale)?._locale
304321
if (!queryLocale) {
305322
params.where = params.where || []
306-
params.where.push({ _locale: contentConfig.defaultLocale })
323+
params.where.push({ _locale: config.defaultLocale })
307324
}
308325
}
309326

0 commit comments

Comments
 (0)