Skip to content

Commit

Permalink
chore: optimize queries for specific user
Browse files Browse the repository at this point in the history
Signed-off-by: Yash Khare <[email protected]>
  • Loading branch information
khareyash05 committed Dec 10, 2024
1 parent b6991b5 commit d2b6fb4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
49 changes: 48 additions & 1 deletion lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,54 @@ export async function getMoreStoriesForSlugs(tags, slug) {
};
}


export async function getPostsByAuthorName(authorName) {
let allEdges = [];
const data = await fetchAPI(
`query MyQuery3 {
posts(where: {authorName: "${authorName}"}) {
edges {
node {
title
excerpt
slug
date
postId
featuredImage {
node {
sourceUrl
}
}
author {
node {
name
firstName
lastName
avatar {
url
}
}
}
ppmaAuthorName
categories {
edges {
node {
name
}
}
}
seo {
metaDesc
title
}
}
}
}
}`
);
const edges = data.posts.edges;
allEdges = [...allEdges, ...edges];
return { edges: allEdges };
}


export async function getPostAndMorePosts(slug, preview, previewData) {
Expand Down
32 changes: 18 additions & 14 deletions pages/authors/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ import Header from "../../components/header";
import Container from "../../components/container";
import {
getAllAuthors,
getAllPostsForCommunity,
getAllPostsForTechnology,
getContent,
getPostsByAuthor,
getPostsByAuthorName,
} from "../../lib/api";
import { GetStaticPaths, GetStaticProps } from "next";
import PostByAuthorMapping from "../../components/postByAuthorMapping";
Expand Down Expand Up @@ -59,26 +57,32 @@ export const getStaticProps: GetStaticProps = async ({
}) => {
const { slug } = params as { slug: string };

// Fetch posts from both sources
const postsByTechnology = await getAllPostsForTechnology(preview);
const postsByCommunity = await getAllPostsForCommunity(preview);
// these are users mapped by first name on the username
const usersMappedByFirstName = ["Animesh Pathak","Shubham Jain","Yash Khare"]

// Combine posts from both sources
const allPosts = [...postsByTechnology.edges, ...postsByCommunity.edges];
// if slug is in above array then take only first name and pass it to getPostsByAuthorName
// else pass the slug as it is to getPostsByAuthorName
let userName =slug
if(usersMappedByFirstName.includes(slug)){
userName = slug.split(" ")[0]
}

// Filter the combined posts by the given slug
const filteredPosts = allPosts.filter(
(item) => item.node.ppmaAuthorName === slug
);
const posts = await getPostsByAuthorName(userName)

const allPosts = posts.edges

// Extract postId from the first matching post (if any)
const postId = filteredPosts[0]?.node?.postId;
const postId = allPosts[0].node?.postId;

// Fetch content using postId (if available)
const content = postId ? await getContent(postId) : null;

return {
props: { preview, filteredPosts, content },
props: {
preview,
filteredPosts: allPosts,
content
},
revalidate: 10, // ISR with 10 seconds revalidation
};
};

0 comments on commit d2b6fb4

Please sign in to comment.