fix: improve revalidation logic by decoding path and invalidating cache after updates
This commit is contained in:
parent
6ae1993e8d
commit
054b9eff9a
4 changed files with 15 additions and 3 deletions
|
|
@ -135,12 +135,20 @@ export const AnnuncioEditForm = ({ data }: { data: Annunci }) => {
|
|||
const { mutateAsync: revalidate } =
|
||||
api.settings.revalidateAnnuncio.useMutation({
|
||||
onSettled: async () => {
|
||||
await utils.annunci.getAnnuncio.invalidate({ cod: data.codice });
|
||||
await utils.annunci.getAnnuncioById_rawImgUrls.invalidate({
|
||||
id: data.id,
|
||||
});
|
||||
toast.success("Revalidazione completata");
|
||||
},
|
||||
});
|
||||
|
||||
const { mutateAsync: update } = api.settings.updateAnnuncio.useMutation({
|
||||
onSettled: async () => {
|
||||
await utils.annunci.getAnnuncio.invalidate({ cod: data.codice });
|
||||
await utils.annunci.getAnnuncioById_rawImgUrls.invalidate({
|
||||
id: data.id,
|
||||
});
|
||||
toast.success(
|
||||
"L'aggiornamento dell'annuncio è stata richiesta con successo,\n potrebbero volerci alcuni minuti prima che le modifiche siano visibili.",
|
||||
{
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ export default async function handler(
|
|||
if (req.query.secret !== env.REVALIDATION_SECRET) {
|
||||
return res.status(401).json({ message: "Invalid token" });
|
||||
}
|
||||
const path = req.query.path as string;
|
||||
const path = decodeURIComponent(req.query.path as string);
|
||||
|
||||
if (!path) {
|
||||
return res.status(400).json({ message: "Missing path parameter" });
|
||||
|
|
|
|||
|
|
@ -17,9 +17,10 @@ import { api } from "~/utils/api";
|
|||
|
||||
const Admin_Annunci: NextPageWithLayout = () => {
|
||||
const { data, isLoading, refetch } = api.annunci.getAnnunciList.useQuery();
|
||||
|
||||
const utils = api.useUtils();
|
||||
const { mutateAsync: update } = api.settings.updateAllAnnunci.useMutation({
|
||||
onSettled: async () => {
|
||||
await utils.annunci.getAnnunciList.invalidate();
|
||||
toast.success(
|
||||
"La revalidazione dell'annuncio è stata richiesta con successo,\n potrebbero volerci alcuni minuti prima che le modifiche siano visibili.",
|
||||
{
|
||||
|
|
@ -31,6 +32,7 @@ const Admin_Annunci: NextPageWithLayout = () => {
|
|||
const { mutateAsync: revalidate } =
|
||||
api.settings.revalidateAllAnnunci.useMutation({
|
||||
onSettled: async () => {
|
||||
await utils.annunci.getAnnunciList.invalidate();
|
||||
toast.success("Revalidazione completata");
|
||||
},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
import { env } from "~/env.mjs";
|
||||
|
||||
export const revalidate = async (path: string) => {
|
||||
try {
|
||||
const res = await fetch(
|
||||
`${process.env.NEXT_PUBLIC_BASE_URL}/api/revalidation?secret=${process.env.REVALIDATION_SECRET}&path=${path}`,
|
||||
`${env.NEXT_PUBLIC_BASE_URL}/api/revalidation?secret=${env.REVALIDATION_SECRET}&path=${encodeURIComponent(path)}`,
|
||||
{
|
||||
method: "GET",
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue