英文:
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;
我已解决了这个问题。
英文:
Just by adding export const revalidate = 60;
I have resolved the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论