Skip to content

Commit

Permalink
clear cache when like
Browse files Browse the repository at this point in the history
  • Loading branch information
GraemeFulton committed Jun 13, 2024
1 parent 477a078 commit c90eaa2
Showing 1 changed file with 88 additions and 40 deletions.
128 changes: 88 additions & 40 deletions pages/api/revalidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,59 @@ export default async function handler(req, res) {
const { entry } = req.body;

// revalidate posts
if (entry.type=='article' && (entry.status === "publish" || entry.publishedAt)) {
if (
entry.type == "article" &&
(entry.status === "publish" || entry.publishedAt)
) {
const url = `/post/${entry.slug}`;

await res.revalidate(url);
if(process.env.NODE_ENV=='production'){
if (process.env.NODE_ENV == "production") {
await purgeCloudFlareCache(url);
await clearAuthCache(entry.id);
}
return res.json({ revalidated: true });
}
}
//revalidate jobs
else if(entry.type=='job' && entry.publishedAt){
else if (entry.type == "job" && entry.publishedAt) {
console.log("revalidating job post :", entry.slug);
const url = `/jobs/${entry.id}`;
await res.revalidate(url);
//if production
if(process.env.NODE_ENV=='production'){
if (process.env.NODE_ENV == "production") {
await purgeCloudFlareCache(url);
await clearAuthCache(entry.id);
}
return res.json({ revalidated: true });
}
//revalidate news
else if(entry.type=='bite' && entry.publishedAt){
else if (entry.type == "bite" && entry.publishedAt) {
console.log("revalidating news post :", entry.slug);
const url = `/news/${entry.slug}`;
await res.revalidate(url);
if(process.env.NODE_ENV=='production'){
if (process.env.NODE_ENV == "production") {
await purgeCloudFlareCache(url);
await clearAuthCache(entry.id);
}
return res.json({ revalidated: true });
}
// revalidate tools
else if(entry.type=='tool' && entry.publishedAt){
else if (entry.type == "tool" && entry.publishedAt) {
console.log("revalidating tool post :", entry.slug);
const url = `/toolbox/${entry.slug}`;
await res.revalidate(url);
if(process.env.NODE_ENV=='production'){
if (process.env.NODE_ENV == "production") {
await purgeCloudFlareCache(url);
await clearAuthCache(entry.id);
}
return res.json({ revalidated: true });
}
else {
} else if (entry.total) {
//it's a like
console.log("revalidating liked post by like id:", entry.id);

await clearPostCacheByLike(entry.id, res);
return res.json({ revalidated: true });
} else {
return res.json({ revalidated: false });
}
} catch (err) {
Expand All @@ -67,43 +75,83 @@ export default async function handler(req, res) {
}
}


const clearAuthCache = async (postId) => {

const clearAuthCache = async postId => {
//fetch the user of the post by the postId from strapi using the admin secret key


var config = {
method: 'get',
url: process.env.NEXT_PUBLIC_STRAPI_API_URL+`/api/posts/${postId}?populate=user`,
headers: {
'authorization': `Bearer ${process.env.STRAPI_READONLY_TOKEN}`,
'Content-Type': 'application/json'
}
method: "get",
url:
process.env.NEXT_PUBLIC_STRAPI_API_URL +
`/api/posts/${postId}?populate=user`,
headers: {
authorization: `Bearer ${process.env.STRAPI_READONLY_TOKEN}`,
"Content-Type": "application/json",
},
};

axios(config)
.then(async function (response) {
try{
let userSlug = (response?.data?.data?.attributes?.user?.data?.attributes?.slug)
if(userSlug){
let url = `/people/${userSlug}`
await purgeCloudFlareCache(url);
}
// return res.status(200).json({ views: response.data?.results?.visitors?.value})
}catch(e){
console.log(e)
}
})
.catch(function (error) {
console.log(error.message);
return res.status(500).send("Error checking analytics");
});
.then(async function (response) {
try {
let userSlug =
response?.data?.data?.attributes?.user?.data?.attributes?.slug;
if (userSlug) {
let url = `/people/${userSlug}`;
await purgeCloudFlareCache(url);
}
// return res.status(200).json({ views: response.data?.results?.visitors?.value})
} catch (e) {
console.log(e);
}
})
.catch(function (error) {
console.log(error.message);
return res.status(500).send("Error checking analytics");
});

const url = "/api/auth/user";
await res.revalidate(url);
if(process.env.NODE_ENV=='production'){
if (process.env.NODE_ENV == "production") {
await purgeCloudFlareCache(url);
}
return res.json({ revalidated: true });
};
};



////////
const clearPostCacheByLike = async (likeId, res) => {
var config = {
method: "get",
url:
process.env.NEXT_PUBLIC_STRAPI_API_URL +
`/api/likes/${likeId}?populate=post`,
headers: {
authorization: `Bearer ${process.env.STRAPI_READONLY_TOKEN}`,
"Content-Type": "application/json",
},
};

axios(config)
.then(async function (response) {
try {
let postSlug =
response?.data?.data?.attributes?.post?.data?.attributes?.slug;

if (postSlug) {
let url = `/post/${postSlug}`;
await res.revalidate(url);
if (process.env.NODE_ENV == "production") {
await purgeCloudFlareCache(url);
}
}
// return res.status(200).json({ views: response.data?.results?.visitors?.value})
} catch (e) {
console.log(e);

}
})
.catch(function (error) {
console.log(error.message);
return res.status(500).send("Error checking analytics");
});
};

0 comments on commit c90eaa2

Please sign in to comment.