diff --git a/src/runtime/server/api/cache.ts b/src/runtime/server/api/cache.ts index 375f47fc2..589c4dd08 100644 --- a/src/runtime/server/api/cache.ts +++ b/src/runtime/server/api/cache.ts @@ -1,18 +1,18 @@ import { defineEventHandler } from 'h3' +import { getContentIndex } from '../content-index' import { cacheStorage, serverQueryContent } from '../storage' // This route is used to cache all the parsed content export default defineEventHandler(async (event) => { const now = Date.now() // Fetch all content - const data = await serverQueryContent(event).find() + await serverQueryContent(event).find() // Generate Index - const index = data.reduce((acc, item) => { - acc[item._path!] = item._id - return acc - }, {} as Record) - await cacheStorage.setItem('index.json', index) + await getContentIndex(event) + + const navigation = await $fetch('/api/_content/navigation') + await cacheStorage.setItem('content-navigation.json', navigation) return { generatedAt: now, diff --git a/src/runtime/server/api/navigation.ts b/src/runtime/server/api/navigation.ts index 6bd04fc55..ef2a57c2c 100644 --- a/src/runtime/server/api/navigation.ts +++ b/src/runtime/server/api/navigation.ts @@ -1,12 +1,20 @@ import { defineEventHandler } from 'h3' -import { serverQueryContent } from '../storage' +import { cacheStorage, serverQueryContent } from '../storage' import { createNav } from '../navigation' import { ParsedContentMeta } from '../../types' import { getContentQuery } from '../../utils/query' +import { isPreview } from '../preview' export default defineEventHandler(async (event) => { const query = getContentQuery(event) + if (!isPreview(event) && Object.keys(query).length === 0) { + const cache = cacheStorage.getItem('content-navigation.json') + if (cache) { + return cache + } + } + const contents = await serverQueryContent(event, query) .where({ /**