firebasenext.jscaching

Stop Nextjs 13 from caching Firebase result/call


i have this api route:

app/api/notes/route.js

import db from "@/utils/firebaseDB"
import { collection, getDocs } from "firebase/firestore";

export const GET = async (request) => {
    let posts = []

    try {
        const querySnapshot = await getDocs(collection(db, "Notes"));
        querySnapshot.docs.map((doc) => {
            const postData = { ...doc.data(), id: doc.id };
            posts.push(postData);
        });

        return new Response(JSON.stringify(posts), { status: 200 })
    } catch (error) {
        return new Response("Failed to fetch all data", { status: 500 })
    }
} 

After building the project the result becomes static or cached.. So, I need a way to force the revalidate after a certain period.

Using fetch the solution is to use fetch('https://...', { next: { revalidate: 60 } }); but with getDocs I don't know.

Thank you


Solution

  • Just by adding export const revalidate = 60; I have resolved the issue.

    More info: https://nextjs.org/docs/app/building-your-application/data-fetching/revalidating#background-revalidation