Stop Next.js 13 from caching Firebase result/call.

huangapple go评论68阅读模式
英文:

Stop Nextjs 13 from caching Firebase result/call

问题

我有这个API路由:

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

在构建项目后,结果变为静态或缓存。所以,我需要一种方式在一定时间后强制进行revalidate

使用fetch的解决方法是使用fetch('https://...', { next: { revalidate: 60 } });,但是对于getDocs,我不知道。

谢谢。

英文:

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

答案1

得分: 1

通过添加 export const revalidate = 60; 我已解决了这个问题。

更多信息: https://nextjs.org/docs/app/building-your-application/data-fetching/revalidating#background-revalidation

英文:

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

huangapple
  • 本文由 发表于 2023年5月14日 17:38:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定