Skip to content

Commit

Permalink
feat(api): cache api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz committed Dec 19, 2023
1 parent 27f0659 commit d2bcf70
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/runtime/server/api/navigation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { defineEventHandler } from 'h3'
import { cachedEventHandler } from 'nitropack/dist/runtime/cache'
import { cacheStorage, serverQueryContent } from '../storage'
import { createNav } from '../navigation'
import type { ParsedContent, ParsedContentMeta } from '../../types'
import { getContentQuery } from '../../utils/query'
import { isPreview } from '../preview'

export default defineEventHandler(async (event) => {
export default cachedEventHandler(async (event) => {
const query = getContentQuery(event)

// Read from cache if not preview and there is no query
Expand Down Expand Up @@ -50,4 +50,7 @@ export default defineEventHandler(async (event) => {
}, {} as Record<string, ParsedContentMeta>)

return createNav((contents?.result || contents) as ParsedContentMeta[], configs)
}, {
maxAge: 31536000,
shouldBypassCache: () => !!import.meta.dev
})
8 changes: 6 additions & 2 deletions src/runtime/server/api/query.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createError, defineEventHandler } from 'h3'
import { createError } from 'h3'
import { cachedEventHandler } from 'nitropack/dist/runtime/cache'
import { serverQueryContent } from '../storage'
import { getContentQuery } from '../../utils/query'
import { useRuntimeConfig } from '#imports'

export default defineEventHandler(async (event) => {
export default cachedEventHandler(async (event) => {
const query = getContentQuery(event)
const { advanceQuery } = useRuntimeConfig().public.content.experimental

Expand Down Expand Up @@ -39,4 +40,7 @@ export default defineEventHandler(async (event) => {
}

return serverQueryContent(event, query).find()
}, {
maxAge: 31536000,
shouldBypassCache: () => !!import.meta.dev
})

0 comments on commit d2bcf70

Please sign in to comment.