How to avoid server side caching?
Unanswered
Transvaal lion posted this in #help-forum
Transvaal lionOP
Hello! I am facing an issue in production, In development, my code works fine on some trigger, the request is sent to the server and then it brings back the desired data. However, in production, data once loaded, even on API request does not return the desired data. It is doing some sort of server caching. How to handle this. Please tell me I am new to NextJs.
Following is the API code in app/api/users
type AllUsersResponse = {
success: boolean
users: User[]
}
export async function getAllUsers() {
try {
const {data: {users}} = await axios.get<AllUsersResponse>("/api/users")
return users
} catch (error: any) {
console.error({ error: error.message })
}
}Following is the API code in app/api/users
import { connect } from "@/dbConfig/dbConfig"
import members from "@/models/member"
import { NextRequest, NextResponse } from "next/server"
connect()
export async function GET() {
try {
const users: any = await members.find({role: "USER"})
return NextResponse.json({users, success: true}, {status: 200})
} catch (error: any) {
return NextResponse.json({ error: error.message }, { status: 500 })
}
}4 Replies
Australian Freshwater Crocodile
Sorry headers doesn't provide a way to set headers, which is kinda stupid.
https://stackoverflow.com/questions/61812597/how-to-set-custom-http-response-headers-in-next-js-without-using-a-custom-server
Looks like you can do it with context.res?
https://stackoverflow.com/questions/61812597/how-to-set-custom-http-response-headers-in-next-js-without-using-a-custom-server
Looks like you can do it with context.res?
@Transvaal lion Hello! I am facing an issue in production, In development, my code works fine on some trigger, the request is sent to the server and then it brings back the desired data. However, in production, data once loaded, even on API request does not return the desired data. It is doing some sort of server caching. How to handle this. Please tell me I am new to NextJs.
ts
type AllUsersResponse = {
success: boolean
users: User[]
}
export async function getAllUsers() {
try {
const {data: {users}} = await axios.get<AllUsersResponse>("/api/users")
return users
} catch (error: any) {
console.error({ error: error.message })
}
}
Following is the API code in app/api/users
ts
import { connect } from "@/dbConfig/dbConfig"
import members from "@/models/member"
import { NextRequest, NextResponse } from "next/server"
connect()
export async function GET() {
try {
const users: any = await members.find({role: "USER"})
return NextResponse.json({users, success: true}, {status: 200})
} catch (error: any) {
return NextResponse.json({ error: error.message }, { status: 500 })
}
}
That’s due to [server side static rendering](https://nextjs.org/docs/app/building-your-application/routing/route-handlers#caching). Opt out by [
export const dynamic = "force-dynamic"](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic) or [export const revalidate = 0](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#revalidate)@Australian Freshwater Crocodile Sorry headers doesn't provide a way to *set* headers, which is kinda stupid.
https://stackoverflow.com/questions/61812597/how-to-set-custom-http-response-headers-in-next-js-without-using-a-custom-server
Looks like you can do it with context.res?
You can set headers in route handlers. But the cache here is a server side cache not a client side cache, the header won’t help