API: Cache markets for 15 seconds at least

This commit is contained in:
James Grugett 2022-10-01 13:37:55 -05:00
parent 2f1221f094
commit b53e4acea6
3 changed files with 7 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import { listAllComments } from 'web/lib/firebase/comments'
import { getContractFromId } from 'web/lib/firebase/contracts'
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
import { FullMarket, ApiError, toFullMarket } from '../../_types'
import { marketCacheStrategy } from '../../markets'
export default async function handler(
req: NextApiRequest,
@ -24,6 +25,6 @@ export default async function handler(
return
}
res.setHeader('Cache-Control', 'max-age=0')
res.setHeader('Cache-Control', marketCacheStrategy)
return res.status(200).json(toFullMarket(contract, comments, bets))
}

View File

@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { getContractFromId } from 'web/lib/firebase/contracts'
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
import { ApiError, toLiteMarket, LiteMarket } from '../../_types'
import { marketCacheStrategy } from '../../markets'
export default async function handler(
req: NextApiRequest,
@ -18,6 +19,6 @@ export default async function handler(
return
}
res.setHeader('Cache-Control', 'max-age=0')
res.setHeader('Cache-Control', marketCacheStrategy)
return res.status(200).json(toLiteMarket(contract))
}

View File

@ -6,6 +6,8 @@ import { toLiteMarket, ValidationError } from './_types'
import { z } from 'zod'
import { validate } from './_validate'
export const marketCacheStrategy = 's-maxage=15, stale-while-revalidate=45'
const queryParams = z
.object({
limit: z
@ -39,7 +41,7 @@ export default async function handler(
try {
const contracts = await listAllContracts(limit, before)
// Serve from Vercel cache, then update. see https://vercel.com/docs/concepts/functions/edge-caching
res.setHeader('Cache-Control', 's-maxage=1, stale-while-revalidate')
res.setHeader('Cache-Control', marketCacheStrategy)
res.status(200).json(contracts.map(toLiteMarket))
} catch (e) {
res.status(400).json({