Next.js Discord

Discord Forum

Not getting latest data from db on Vercel | Neondb (postgresql)

Answered
Naeem posted this in #help-forum
Open in Discord
Avatar
NaeemOP
this is my route file
import prisma from "@/prisma/client"
import { NextResponse } from "next/server"

export const GET = async()=>{
try {
data = await prisma.docs.findMany({})

return NextResponse.json(data,{status: 200})}

 catch (error) {
   console.log(error)
return NextResponse.json({msg:"Something went wrong"},{status:503})}}

while hitting it from localhost I'm getting all the latest data but from Vercel I'm getting old data not the latest one
Answered by Risky did start the fire πŸ™ƒ
because layout config usually affects the nested page files, but im not sure about route files
View full answer

33 Replies

Avatar
SupremeDeity
Have you tried to actually test cache from npm run build && npm start ??
npm run dev isnt a good indicator of caching
Avatar
NaeemOP
yep
Avatar
SupremeDeity
or performance
Avatar
NaeemOP
I tried with build and start as well
before pushing to vercel
Avatar
SupremeDeity
check vercel datacache out and next docs guide on opting out of cache
Avatar
NaeemOP
But I'm fetching it like this
 const dynamic = await fetch('https://api.vercel.app/products', {
    cache: 'no-store',
  });
Avatar
Risky did start the fire πŸ™ƒ
prob change revalidate settings or make the route dynamic cache
Avatar
NaeemOP
with no-store cache
?
Avatar
Risky did start the fire πŸ™ƒ
i meant:
export const dynamic = 'force-dynamic' 
// or
export const revalidate = 0 // can me greater then 0 if you want it to last longer
i was meaning that nextjs has fetch cache as well as page... you have to make suree both doesn't cache
Avatar
NaeemOP
yeah I was thinking of revalidate = 0
let me give revalidate a shot
Avatar
NaeemOP
revalidation 0 is not working
πŸ₯²
Avatar
Risky did start the fire πŸ™ƒ
can you send your full code of route
Avatar
NaeemOP
 const [tableData,setTableData] = useState([])
    useEffect(()=>{
        const getData = async()=>{
            const response = await fetch(`/api/get-all-data`,{cache:"no-store"})
            const data = await response.json()
            console.log("useEffect called")
            console.log(data)
            setTableData(data)
        }
        getData()
    },[])

this is how I'm hitting from client component

And below is get-all-data/route.ts
import prisma from "@/prisma/client"
import { NextResponse } from "next/server"

export const GET = async()=>{
try {
data = await prisma.docs.findMany({})

return NextResponse.json(data,{status: 200})}

 catch (error) {
   console.log(error)
return NextResponse.json({msg:"Something went wrong"},{status:503})}}
Avatar
Risky did start the fire πŸ™ƒ
i don't see your export of revalidation
Avatar
NaeemOP
I did it in layout.tsx
Avatar
Risky did start the fire πŸ™ƒ
ahh can you also try manually here
Avatar
NaeemOP
in route?
Avatar
Risky did start the fire πŸ™ƒ
ye
Avatar
NaeemOP
lemme give a min
Avatar
Risky did start the fire πŸ™ƒ
because layout config usually affects the nested page files, but im not sure about route files
Answer
Avatar
NaeemOP
building...
Hmmmm
IT WORKED
@Risky did start the fire πŸ™ƒ
Avatar
Risky did start the fire πŸ™ƒ
yay!!!!
Avatar
NaeemOP
:all_the_things: