Better getStaticPaths structure for blog endpoints
This commit is contained in:
@ -7,7 +7,9 @@ import Pagination from '~/components/blog/Pagination.astro';
|
|||||||
|
|
||||||
import { blogListRobots, getStaticPathsBlogList } from '~/utils/blog';
|
import { blogListRobots, getStaticPathsBlogList } from '~/utils/blog';
|
||||||
|
|
||||||
export const getStaticPaths = getStaticPathsBlogList();
|
export async function getStaticPaths ({ paginate }) {
|
||||||
|
return await getStaticPathsBlogList({ paginate });
|
||||||
|
}
|
||||||
|
|
||||||
const { page } = Astro.props;
|
const { page } = Astro.props;
|
||||||
const currentPage = page.currentPage ?? 1;
|
const currentPage = page.currentPage ?? 1;
|
||||||
|
@ -6,7 +6,9 @@ import BlogList from '~/components/blog/List.astro';
|
|||||||
import Headline from '~/components/blog/Headline.astro';
|
import Headline from '~/components/blog/Headline.astro';
|
||||||
import Pagination from '~/components/blog/Pagination.astro';
|
import Pagination from '~/components/blog/Pagination.astro';
|
||||||
|
|
||||||
export const getStaticPaths = getStaticPathsBlogCategory();
|
export async function getStaticPaths ({ paginate }) {
|
||||||
|
return await getStaticPathsBlogCategory({ paginate });
|
||||||
|
}
|
||||||
|
|
||||||
const { page, category } = Astro.props;
|
const { page, category } = Astro.props;
|
||||||
|
|
||||||
|
@ -6,7 +6,9 @@ import BlogList from '~/components/blog/List.astro';
|
|||||||
import Headline from '~/components/blog/Headline.astro';
|
import Headline from '~/components/blog/Headline.astro';
|
||||||
import Pagination from '~/components/blog/Pagination.astro';
|
import Pagination from '~/components/blog/Pagination.astro';
|
||||||
|
|
||||||
export const getStaticPaths = getStaticPathsBlogTag();
|
export async function getStaticPaths ({ paginate }) {
|
||||||
|
return await getStaticPathsBlogTag({ paginate });
|
||||||
|
}
|
||||||
|
|
||||||
const { page, tag } = Astro.props;
|
const { page, tag } = Astro.props;
|
||||||
|
|
||||||
|
@ -9,7 +9,9 @@ import { getCanonical, getPermalink } from '~/utils/permalinks';
|
|||||||
import { getStaticPathsBlogPost, blogPostRobots } from '~/utils/blog';
|
import { getStaticPathsBlogPost, blogPostRobots } from '~/utils/blog';
|
||||||
import { findImage } from '~/utils/images';
|
import { findImage } from '~/utils/images';
|
||||||
|
|
||||||
export const getStaticPaths = getStaticPathsBlogPost();
|
export async function getStaticPaths () {
|
||||||
|
return await getStaticPathsBlogPost();
|
||||||
|
}
|
||||||
|
|
||||||
const { post } = Astro.props;
|
const { post } = Astro.props;
|
||||||
|
|
||||||
|
@ -162,18 +162,16 @@ export const findLatestPosts = async ({ count }: { count?: number }): Promise<Ar
|
|||||||
};
|
};
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
export const getStaticPathsBlogList =
|
export const getStaticPathsBlogList = async ({ paginate }) => {
|
||||||
() =>
|
if (!isBlogEnabled || !isBlogListRouteEnabled) return [];
|
||||||
async ({ paginate }) => {
|
return paginate(await fetchPosts(), {
|
||||||
if (!isBlogEnabled || !isBlogListRouteEnabled) return [];
|
params: { blog: BLOG_BASE || undefined },
|
||||||
return paginate(await fetchPosts(), {
|
pageSize: blogPostsPerPage,
|
||||||
params: { blog: BLOG_BASE || undefined },
|
});
|
||||||
pageSize: blogPostsPerPage,
|
};
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
export const getStaticPathsBlogPost = () => async () => {
|
export const getStaticPathsBlogPost = async () => {
|
||||||
if (!isBlogEnabled || !isBlogPostRouteEnabled) return [];
|
if (!isBlogEnabled || !isBlogPostRouteEnabled) return [];
|
||||||
return (await fetchPosts()).map((post) => ({
|
return (await fetchPosts()).map((post) => ({
|
||||||
params: {
|
params: {
|
||||||
@ -184,49 +182,45 @@ export const getStaticPathsBlogPost = () => async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
export const getStaticPathsBlogCategory =
|
export const getStaticPathsBlogCategory = async ({ paginate }) => {
|
||||||
() =>
|
if (!isBlogEnabled || !isBlogCategoryRouteEnabled) return [];
|
||||||
async ({ paginate }) => {
|
|
||||||
if (!isBlogEnabled || !isBlogCategoryRouteEnabled) return [];
|
|
||||||
|
|
||||||
const posts = await fetchPosts();
|
const posts = await fetchPosts();
|
||||||
const categories = new Set();
|
const categories = new Set();
|
||||||
posts.map((post) => {
|
posts.map((post) => {
|
||||||
typeof post.category === 'string' && categories.add(post.category.toLowerCase());
|
typeof post.category === 'string' && categories.add(post.category.toLowerCase());
|
||||||
});
|
});
|
||||||
|
|
||||||
return Array.from(categories).map((category: string) =>
|
return Array.from(categories).map((category: string) =>
|
||||||
paginate(
|
paginate(
|
||||||
posts.filter((post) => typeof post.category === 'string' && category === post.category.toLowerCase()),
|
posts.filter((post) => typeof post.category === 'string' && category === post.category.toLowerCase()),
|
||||||
{
|
{
|
||||||
params: { category: category, blog: CATEGORY_BASE || undefined },
|
params: { category: category, blog: CATEGORY_BASE || undefined },
|
||||||
pageSize: blogPostsPerPage,
|
pageSize: blogPostsPerPage,
|
||||||
props: { category },
|
props: { category },
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/** */
|
/** */
|
||||||
export const getStaticPathsBlogTag =
|
export const getStaticPathsBlogTag = async ({ paginate }) => {
|
||||||
() =>
|
if (!isBlogEnabled || !isBlogTagRouteEnabled) return [];
|
||||||
async ({ paginate }) => {
|
|
||||||
if (!isBlogEnabled || !isBlogTagRouteEnabled) return [];
|
|
||||||
|
|
||||||
const posts = await fetchPosts();
|
const posts = await fetchPosts();
|
||||||
const tags = new Set();
|
const tags = new Set();
|
||||||
posts.map((post) => {
|
posts.map((post) => {
|
||||||
Array.isArray(post.tags) && post.tags.map((tag) => tags.add(tag.toLowerCase()));
|
Array.isArray(post.tags) && post.tags.map((tag) => tags.add(tag.toLowerCase()));
|
||||||
});
|
});
|
||||||
|
|
||||||
return Array.from(tags).map((tag: string) =>
|
return Array.from(tags).map((tag: string) =>
|
||||||
paginate(
|
paginate(
|
||||||
posts.filter((post) => Array.isArray(post.tags) && post.tags.find((elem) => elem.toLowerCase() === tag)),
|
posts.filter((post) => Array.isArray(post.tags) && post.tags.find((elem) => elem.toLowerCase() === tag)),
|
||||||
{
|
{
|
||||||
params: { tag: tag, blog: TAG_BASE || undefined },
|
params: { tag: tag, blog: TAG_BASE || undefined },
|
||||||
pageSize: blogPostsPerPage,
|
pageSize: blogPostsPerPage,
|
||||||
props: { tag },
|
props: { tag },
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user