new logic routes
This commit is contained in:
@@ -1,18 +1,12 @@
|
|||||||
---
|
---
|
||||||
import { getPostById } from '@lib/api/all';
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
postId?: number;
|
post: any;
|
||||||
pageInfo?: 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 ? (
|
{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 MainLayout from '@layouts/MainLayout.astro';
|
||||||
|
import NewsSingle from '@components/NewsSingle.astro';
|
||||||
|
|
||||||
import { getNodeByURI, getCategoryPosts } from '@lib/api/all';
|
import { getNodeByURI, getCategoryPosts } from '@lib/api/all';
|
||||||
|
import { getProfileArticleById } from '@lib/api/posts'; //логика
|
||||||
|
|
||||||
|
import { detectPageType } from '@lib/detect-page-type';
|
||||||
|
|
||||||
export const prerender = false;
|
export const prerender = false;
|
||||||
|
|
||||||
const { slug } = Astro.params;
|
const pathname = Astro.url.pathname; // "/news/society/chto-sluchilos-nochju-27-oktyabrya-2025-goda-1772178/"
|
||||||
const uri = slug ? `/${slug}` : '/';
|
const pageInfo = detectPageType(pathname); //определяем тип страницы
|
||||||
|
|
||||||
|
|
||||||
let response;
|
let response;
|
||||||
let node = null;
|
let article = null;
|
||||||
|
|
||||||
|
if (pageInfo.type === 'single') { //одиночная статья
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response = await getNodeByURI(uri);
|
article = await getProfileArticleById(pageInfo.postId); //получвем данные поста
|
||||||
node = response?.nodeByUri;
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching node:', error);
|
console.error('Error fetching node:', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ISR кэширование
|
// ISR кэширование
|
||||||
//Astro.response.headers.set(
|
//Astro.response.headers.set(
|
||||||
// 'Cache-Control',
|
// 'Cache-Control',
|
||||||
@@ -24,6 +35,17 @@ try {
|
|||||||
//);
|
//);
|
||||||
---
|
---
|
||||||
|
|
||||||
Article
|
<MainLayout
|
||||||
<p>{uri}</p>
|
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 MainLayout from '@layouts/MainLayout.astro';
|
||||||
|
import NewsSingle from '@components/NewsSingle.astro';
|
||||||
|
|
||||||
import { detectPageType } from '@lib/detect-page-type';
|
import { detectPageType } from '@lib/detect-page-type';
|
||||||
|
|
||||||
|
import { getAnewsById } from '@lib/api/posts'; //логика
|
||||||
|
|
||||||
|
|
||||||
export const prerender = false;
|
export const prerender = false;
|
||||||
|
|
||||||
const pathname = Astro.url.pathname; // "/news/society/chto-sluchilos-nochju-27-oktyabrya-2025-goda-1772178/"
|
const pathname = Astro.url.pathname; // "/news/society/chto-sluchilos-nochju-27-oktyabrya-2025-goda-1772178/"
|
||||||
const pageInfo = detectPageType(pathname);
|
const pageInfo = detectPageType(pathname);
|
||||||
|
|
||||||
|
//console.log(pageInfo);
|
||||||
|
|
||||||
// Или для полного URL
|
// Или для полного URL
|
||||||
const fullUrl = Astro.url.href;
|
const fullUrl = Astro.url.href;
|
||||||
// Или для origin + pathname
|
// Или для origin + pathname
|
||||||
const fullPath = Astro.url.origin + Astro.url.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>
|
<MainLayout>
|
||||||
|
|||||||
Reference in New Issue
Block a user