Next.js Discord

Discord Forum

How to avoid server side caching?

Unanswered
Transvaal lion posted this in #help-forum
Open in Discord
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.

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

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?
@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 }) } }