add load-more-posts

This commit is contained in:
Profile Profile
2026-02-26 02:02:52 +03:00
parent b58b35bf47
commit 3da5b48c40
7 changed files with 389 additions and 222 deletions

View File

@@ -292,6 +292,7 @@ console.log('Fetching node for URI:', uri);
);
}
export async function getCategoryPosts(
categoryId: number,
page = 1,
@@ -386,6 +387,120 @@ export async function getCategoryPosts(
);
}
export async function getPostsBySlug(
slug: string,
page = 1,
postsPerPage = 12
) {
const offset = (page - 1) * postsPerPage;
const cacheKey = `posts-by-slug:${slug}:${page}:${postsPerPage}`;
return await cache.wrap(
cacheKey,
async () => {
const query = `
query GetPostsBySlug($slug: String!, $size: Int!, $offset: Int!) {
category(id: $slug, idType: SLUG) {
id
name
slug
databaseId
description
posts(
first: $size,
where: {
offsetPagination: { size: $size, offset: $offset },
orderby: { field: DATE, order: DESC }
}
) {
pageInfo {
offsetPagination {
total
hasMore
hasPrevious
}
}
nodes {
__typename
id
databaseId
title
excerpt
uri
date
featuredImage {
node {
sourceUrl(size: LARGE)
altText
}
}
author {
node {
id
name
firstName
lastName
avatar {
url
}
uri
}
}
categories {
nodes {
id
name
slug
uri
databaseId
}
}
tags {
nodes {
id
name
slug
uri
}
}
}
}
}
}
`;
const data = await fetchGraphQL(query, {
slug,
size: postsPerPage,
offset
});
return {
category: data?.category ? {
id: data.category.id,
databaseId: data.category.databaseId,
name: data.category.name,
slug: data.category.slug,
description: data.category.description
} : null,
posts: data?.category?.posts?.nodes || [],
pageInfo: data?.category?.posts?.pageInfo?.offsetPagination || {
total: 0,
hasMore: false,
hasPrevious: false
}
};
},
{ ttl: CACHE_TTL.POSTS }
);
}
export async function invalidateNodeCache(uri: string): Promise<void> {
const normalizedUri = uri.startsWith('/') ? uri : `/${uri}`;
const cacheKey = `node-by-uri:${normalizedUri}`;

View File

@@ -103,6 +103,98 @@ export async function getLatestPosts(first = 14, after = null) {
}
export async function getPostsByCategory(slug, first = 14, after = null) {
// Создаем уникальный ключ для кэша
const cacheKey = `category-posts:${slug}:${first}:${after || 'first-page'}`;
return await cache.wrap(
cacheKey,
async () => {
const query = `
query GetPostsByCategory($first: Int!, $after: String, $slug: String!) {
profileArticles(
first: $first
after: $after
where: {
orderby: { field: DATE, order: DESC }
categoryName: $slug
}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
cursor
node {
id
databaseId
title
uri
date
featuredImage {
node {
sourceUrl(size: LARGE)
altText
}
}
author {
node {
id
name
firstName
lastName
avatar {
url
}
uri
}
}
# Соавторы как массив
coauthors {
id
name
firstName
lastName
url
description
}
categories {
nodes {
id
name
color
slug
uri
databaseId
}
}
}
}
}
}
`;
const data = await fetchGraphQL(query, { first, after, slug });
const posts = data.profileArticles?.edges?.map(edge => edge.node) || [];
return {
posts,
pageInfo: data.profileArticles?.pageInfo || { hasNextPage: false, endCursor: null }
};
},
{ ttl: CACHE_TTL.POSTS }
);
}
//последние новости (кэшированная версия)
export async function getLatestAnews(count = 12): Promise<AnewsPost[]> {
const cacheKey = `latest-anews:${count}`;