diff --git a/src/templates/NewsSingle.astro b/src/components/NewsSingle.astro similarity index 63% rename from src/templates/NewsSingle.astro rename to src/components/NewsSingle.astro index c39caf0..f1cf3b9 100644 --- a/src/templates/NewsSingle.astro +++ b/src/components/NewsSingle.astro @@ -1,18 +1,12 @@ --- -import { getPostById } from '@lib/api/all'; interface Props { - postId?: number; + post: any; pageInfo?: any; } -const { postId, pageInfo } = Astro.props; +const { post, pageInfo } = Astro.props; -let post = null; -if (postId) { - const response = await getPostById(postId); - post = response?.post; -} --- {post ? ( diff --git a/src/lib/api/posts.ts b/src/lib/api/posts.ts index 1781803..c44e7fa 100644 --- a/src/lib/api/posts.ts +++ b/src/lib/api/posts.ts @@ -186,6 +186,98 @@ export async function getProfileArticleById(databaseId) { +/** + * Получить Anews пост по databaseId + */ +export async function getAnewsById(databaseId: number): Promise { + const cacheKey = `anews:${databaseId}`; + + return await cache.wrap( + cacheKey, + async () => { + const query = ` + query GetAnewsById($id: ID!) { + aNews(id: $id, idType: DATABASE_ID) { + id + databaseId + title + content + excerpt + uri + slug + date + modified + status + featuredImage { + node { + id + sourceUrl + altText + caption + mediaDetails { + width + height + } + } + } + author { + node { + id + name + firstName + lastName + avatar { + url + } + description + uri + } + } + categories { + nodes { + id + name + slug + uri + description + } + } + tags { + nodes { + id + name + slug + uri + } + } + seo { + title + metaDesc + canonical + opengraphTitle + opengraphDescription + opengraphImage { + sourceUrl + } + } + } + } + `; + + try { + const data = await fetchGraphQL(query, { id: databaseId }); + return data?.aNews || null; + } catch (error) { + console.error(`Error fetching Anews post with ID ${databaseId}:`, error); + return null; + } + }, + { ttl: CACHE_TTL.POST_DETAIL } // Можно использовать отдельный TTL для деталей постов + ); +} + + + diff --git a/src/pages/[...slug].astro b/src/pages/[...slug].astro index 638bab7..48b295e 100644 --- a/src/pages/[...slug].astro +++ b/src/pages/[...slug].astro @@ -1,21 +1,32 @@ --- import MainLayout from '@layouts/MainLayout.astro'; +import NewsSingle from '@components/NewsSingle.astro'; + import { getNodeByURI, getCategoryPosts } from '@lib/api/all'; +import { getProfileArticleById } from '@lib/api/posts'; //логика + +import { detectPageType } from '@lib/detect-page-type'; export const prerender = false; -const { slug } = Astro.params; -const uri = slug ? `/${slug}` : '/'; +const pathname = Astro.url.pathname; // "/news/society/chto-sluchilos-nochju-27-oktyabrya-2025-goda-1772178/" +const pageInfo = detectPageType(pathname); //определяем тип страницы + let response; -let node = null; +let article = null; + +if (pageInfo.type === 'single') { //одиночная статья + + try { + article = await getProfileArticleById(pageInfo.postId); //получвем данные поста + } catch (error) { + console.error('Error fetching node:', error); + } + + +} -try { - response = await getNodeByURI(uri); - node = response?.nodeByUri; -} catch (error) { - console.error('Error fetching node:', error); -} // ISR кэширование //Astro.response.headers.set( @@ -24,6 +35,17 @@ try { //); --- -Article -

{uri}

+ + +{pageInfo.type === 'single' && article ? ( + + ) : ( + // Можно добавить fallback контент +
Загрузка или другой тип страницы...
+)} + +
diff --git a/src/pages/news/[...slug].astro b/src/pages/news/[...slug].astro index 0882203..3bf2495 100644 --- a/src/pages/news/[...slug].astro +++ b/src/pages/news/[...slug].astro @@ -1,16 +1,49 @@ --- import MainLayout from '@layouts/MainLayout.astro'; +import NewsSingle from '@components/NewsSingle.astro'; + import { detectPageType } from '@lib/detect-page-type'; +import { getAnewsById } from '@lib/api/posts'; //логика + + export const prerender = false; const pathname = Astro.url.pathname; // "/news/society/chto-sluchilos-nochju-27-oktyabrya-2025-goda-1772178/" const pageInfo = detectPageType(pathname); +//console.log(pageInfo); + // Или для полного URL const fullUrl = Astro.url.href; // Или для origin + pathname const fullPath = Astro.url.origin + Astro.url.pathname; + + +if (pageInfo.type === 'single' && pageInfo.contentType === 'news') { + + if (pageInfo.postId && typeof pageInfo.postId === 'number') { + + const response = await getAnewsById(pageInfo.postId); + console.log(response); + + //pageData = response?.post; + + } + +} + + +if (import.meta.env.DEV) { + + const currentFile = import.meta.url + .replace('file://', '') + .replace(process.cwd(), '') + .replace(/^\//, ''); + + console.log('Файл:', currentFile); +} + ---