new logic routes
This commit is contained in:
@@ -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 ? (
|
||||
@@ -186,6 +186,98 @@ export async function getProfileArticleById(databaseId) {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Получить Anews пост по databaseId
|
||||
*/
|
||||
export async function getAnewsById(databaseId: number): Promise<SingleAnewsPost | null> {
|
||||
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 для деталей постов
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,22 +1,33 @@
|
||||
---
|
||||
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;
|
||||
|
||||
try {
|
||||
response = await getNodeByURI(uri);
|
||||
node = response?.nodeByUri;
|
||||
} catch (error) {
|
||||
if (pageInfo.type === 'single') { //одиночная статья
|
||||
|
||||
try {
|
||||
article = await getProfileArticleById(pageInfo.postId); //получвем данные поста
|
||||
} catch (error) {
|
||||
console.error('Error fetching node:', error);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ISR кэширование
|
||||
//Astro.response.headers.set(
|
||||
// 'Cache-Control',
|
||||
@@ -24,6 +35,17 @@ try {
|
||||
//);
|
||||
---
|
||||
|
||||
Article
|
||||
<p>{uri}</p>
|
||||
<MainLayout
|
||||
title={article.title}
|
||||
description="Информационное агентство Деловой журнал Профиль"
|
||||
>
|
||||
|
||||
{pageInfo.type === 'single' && article ? (
|
||||
<NewsSingle post={article} pageInfo={pageInfo} />
|
||||
) : (
|
||||
// Можно добавить fallback контент
|
||||
<div>Загрузка или другой тип страницы...</div>
|
||||
)}
|
||||
|
||||
</MainLayout>
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
---
|
||||
|
||||
<MainLayout>
|
||||
|
||||
Reference in New Issue
Block a user