Skip to content

Commit

Permalink
perf: set cache header to livindex GET responses
Browse files Browse the repository at this point in the history
  • Loading branch information
itsjavi committed Jun 27, 2024
1 parent 2308526 commit 33f1902
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
10 changes: 2 additions & 8 deletions public/manifest.webmanifest

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/config/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const cacheConfig = {
apiResponseCacheSeconds: 30,
}
2 changes: 2 additions & 0 deletions src/pages/api/livingdexes/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { removeDexApi } from '@/features/livingdex/commands/removeDexApi'
import { apiGuard } from '@/features/users/auth/serverside/apiGuard'
import { getSession } from '@/features/users/auth/serverside/getSession'
import { apiErrors } from '@/lib/utils/types'
import { cacheConfig } from '@/config/cache'

const getDexHandler = async (req: NextApiRequest) => {
const { id } = req.query
Expand Down Expand Up @@ -36,6 +37,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
case 'GET':
{
const result = await getDexHandler(req)
res.setHeader('Cache-Control', 'public, s-maxage=' + cacheConfig.apiResponseCacheSeconds)
res.status(result.statusCode).json(result.data)
}
break
Expand Down
2 changes: 2 additions & 0 deletions src/pages/api/livingdexes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NextApiRequest, NextApiResponse } from 'next'

import { cacheConfig } from '@/config/cache'
import { getDexesApi } from '@/features/livingdex/commands/getDexesApi'
import { saveDexApi } from '@/features/livingdex/commands/saveDexApi'
import { apiGuard } from '@/features/users/auth/serverside/apiGuard'
Expand Down Expand Up @@ -40,6 +41,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
Object.entries(result.headers || {}).forEach(([key, value]) => {
res.setHeader(key, value)
})
res.setHeader('Cache-Control', 'public, s-maxage=' + cacheConfig.apiResponseCacheSeconds)
res.status(result.statusCode).json(result.data)
break
}
Expand Down

0 comments on commit 33f1902

Please sign in to comment.