[Production] Disable cache
Unanswered
West African Lion posted this in #help-forum
West African LionOP
Hi, first of all thank you in advance for your time.
I'm building an application helping me manage my finances. The problem I have is with data displayed in my application, which is not up to date with what is in DB. This works perfectly in local, which makes me think that this is more a problem with the cache system. But even when I purge the cache, it doesn't sync my data.
Here are some details about my environment:
- App deployed on Vercel
- I use
- My backend use NeonDB
- All my requests are formatted like that (the important thing to note is the
- I use appDir directory
- Both my page.tsx and layout.tsx file have
Here are a few parts of my code in order to ease you to help me find a solution to my problem:
In order to fetch data, I use a hook that looks like this:
When I update data and want my view to be up to date, I use this syntax
I'm building an application helping me manage my finances. The problem I have is with data displayed in my application, which is not up to date with what is in DB. This works perfectly in local, which makes me think that this is more a problem with the cache system. But even when I purge the cache, it doesn't sync my data.
Here are some details about my environment:
- App deployed on Vercel
- I use
swr to make request- My backend use NeonDB
- All my requests are formatted like that (the important thing to note is the
cache: 'no-store' thing which is supposed to always fetch latest data).- I use appDir directory
- Both my page.tsx and layout.tsx file have
export const revalidate = 0 , export const dynamic = 'force-dynamic', export const fetchCache = 'force-no-store'Here are a few parts of my code in order to ease you to help me find a solution to my problem:
vercel.json{
"crons": [{
"path": "/api/cron",
"schedule": "0 4 * * *"
}],
"headers": [
{
"source": "/(.*)",
"headers": [
{
"key": "Cache-Control",
"value": "no-cache, no-store, must-revalidate"
},
{
"key": "Pragma",
"value": "no-cache"
},
{
"key": "Expires",
"value": "0"
}
]
}
]
}next.config.js/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
appDir: true,
},
}
module.exports = nextConfigIn order to fetch data, I use a hook that looks like this:
const { data, error, isLoading } = useSWR<number>('/api/amountLeft', (url) =>
fetch(`${baseUrl}${url}`, {
cache: 'no-store',
}).then((res) => res.json())
)When I update data and want my view to be up to date, I use this syntax
await mutate('/api/amountLeft')9 Replies
all the other stuff (page.tsx and layout.tsx segment config exports, vercel.json headers) are not relevant, you can remove it
West African LionOP
Thanks a lot for your answer, so in every hooks that make request, I should add this
export ?@West African Lion Thanks a lot for your answer, so in every hooks that make request, I should add this `export` ?
i just edited. the problem is that
/api/amountLeft is statically generated, add that export to the route.ts of that API route to disable static generationWest African LionOP
Ok, we're talking of the file where I define my endpoint, right ? Ie:
export async function GET(request: NextRequest) {
...
}yes that place exactly
West African LionOP
Ok, I need to define it at the top of the file or inside the GET function ?
@West African Lion Ok, I need to define it at the top of the file or inside the GET function ?
like other segment config options
export async function GET() {...}
export const dynamic = 'force-dynamic'West African LionOP
Thanks a lot, it seems to be working now