最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

caching - Next.js API on Vercel Returns Old Data Instead of Latest - Stack Overflow

programmeradmin2浏览0评论

I have created a Next.js API to fetch all histories from my database. The API implementation is as follows:

export async function GET(req: NextRequest) { 

await dbConnect();
try {
    const history = await getHistory();

    return new NextResponse(JSON.stringify(history), {
        status: 200,
        headers: { 
            "Content-Type": "application/json", 
            "Cache-Control": "no-store" 
        },
    });
} catch (error) {
    console.error(error);
    return NextResponse.json(
        { error: "an unexpected error occurred" },
        { status: 500 }
    );
}

}

The API worked as expected when I initially deploy it to Vercel. However, when I add new histories to the database and call the API again, it still returns the old response instead of fetching the latest data.

i am using nextjs14

I have already tried:

Adding "Cache-Control": "no-store" to prevent caching Confirming that new histories are added to the database Checking the API locally (it works fine and returns updated data)

Is there any additional caching mechanism in Vercel or Next.js that I should consider? How can I ensure that the API always fetches fresh data when called?

I have created a Next.js API to fetch all histories from my database. The API implementation is as follows:

export async function GET(req: NextRequest) { 

await dbConnect();
try {
    const history = await getHistory();

    return new NextResponse(JSON.stringify(history), {
        status: 200,
        headers: { 
            "Content-Type": "application/json", 
            "Cache-Control": "no-store" 
        },
    });
} catch (error) {
    console.error(error);
    return NextResponse.json(
        { error: "an unexpected error occurred" },
        { status: 500 }
    );
}

}

The API worked as expected when I initially deploy it to Vercel. However, when I add new histories to the database and call the API again, it still returns the old response instead of fetching the latest data.

i am using nextjs14

I have already tried:

Adding "Cache-Control": "no-store" to prevent caching Confirming that new histories are added to the database Checking the API locally (it works fine and returns updated data)

Is there any additional caching mechanism in Vercel or Next.js that I should consider? How can I ensure that the API always fetches fresh data when called?

Share Improve this question asked yesterday AswinAswin 111 silver badge1 bronze badge New contributor Aswin is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 1

Route Handlers are cached by default when using the GET method with the Response object in Next.js 14 .

You can opt out of caching by:

  • Using the Request object with the GET method.
  • Using any of the other HTTP methods.
  • Using Dynamic Functions like cookies and headers.
  • The Segment Config Options manually specifies dynamic mode.

You can read more about it in the Next.js docs here. This behavior was changed in Next.js 15, GET route handlers will no longer be cached by default, so you won't need any of this if you upgrade to Next.js 15.

For this particular case in Next.js 14 I would just add a segment config

export const dynamic = 'force-dynamic' //disable cache

export async function GET(req: NextRequest) { 

await dbConnect();
try {
    const history = await getHistory();

    return new NextResponse(JSON.stringify(history), {
        status: 200,
        headers: { 
            "Content-Type": "application/json", 
            "Cache-Control": "no-store" 
        },
    });
} catch (error) {
    console.error(error);
    return NextResponse.json(
        { error: "an unexpected error occurred" },
        { status: 500 }
    );
}
发布评论

评论列表(0)

  1. 暂无评论