Help with speeding up API route response
Answered
English Angora posted this in #help-forum
English AngoraOP
I have this API route that returns the number of views for a particular blog post. I'm using a KV database. Chat GPT is recommending I use a memoize function but that doesn't seem right.
https://chat.openai.com/share/c578a384-dd6d-4614-98c0-788429624719
I'm thinking this could all just be the result of cold starts.
API route code:
https://github.com/M-Valentino/M-Valentino_NextJS/blob/main/src/pages/api/blog-post-views.js
Component that calls the API route:
https://github.com/M-Valentino/M-Valentino_NextJS/blob/main/src/components/blogComponents/BlogPostHeader.tsx
https://chat.openai.com/share/c578a384-dd6d-4614-98c0-788429624719
I'm thinking this could all just be the result of cold starts.
API route code:
https://github.com/M-Valentino/M-Valentino_NextJS/blob/main/src/pages/api/blog-post-views.js
Component that calls the API route:
https://github.com/M-Valentino/M-Valentino_NextJS/blob/main/src/components/blogComponents/BlogPostHeader.tsx
Answered by Scottish Fold
https://nextjs.org/docs/app/building-your-application/caching a better approach is to use caching
5 Replies
English AngoraOP
Here is the blog webpage:
https://mark-valentino.vercel.app/blog
https://mark-valentino.vercel.app/blog
English AngoraOP
bump
Scottish Fold
https://nextjs.org/docs/app/building-your-application/caching a better approach is to use caching
Answer
Scottish Fold
or you can cache permanently with
Then only revalidate when a new view is added using
fetch('https://...', { next: revalidate: false, tags: ["blog-views"] })Then only revalidate when a new view is added using
revalidateTag("blog-views")English AngoraOP
I didn't know this was a thing. Thanks!