Update Astro v5.15.4

This commit is contained in:
ricocc
2025-11-08 16:20:26 +08:00
parent f1bcb1489f
commit 4cf082c589
22 changed files with 181 additions and 236 deletions

View File

@@ -1,18 +1,39 @@
---
import { getCollection } from "astro:content";
import { getCollection, render } from "astro:content";
import PostLayout from "@/layouts/PostLayout.astro";
import Toc from '@/components/widgets/Toc.astro';
// 根据 Astro v5/v6对于 SSG 模式下的动态路由,需要使用 getStaticPaths()
// 来告诉 Astro 在构建时生成哪些页面
export async function getStaticPaths() {
const postEntries = await getCollection("post");
return postEntries.map((entry) => ({
params: { slug: entry.slug },
props: { entry },
}));
return postEntries.map((entry) => {
// 根据 Astro v5 官方文档entry.id 就是条目的唯一标识符
// 对于 [...slug] 路由,需要将 entry.id 转换为 slug 数组
// entry.id 已经是相对于集合根目录的路径,例如 "dir/index" 或 "dir"
// 注意:对于 [...slug] 路由params.slug 应该是字符串Astro 会自动处理
const slugString = entry.id;
return {
params: { slug: slugString },
props: { entry },
};
});
}
// 使用 [...slug] 路由来处理包含斜杠的路径(官方推荐)
// slug 参数是数组格式,需要转换为字符串来匹配 entry.id
const { slug } = Astro.params;
const slugString = Array.isArray(slug) ? slug.join('/') : (slug || '');
// 从 props 获取 entry在 SSG 模式下通过 getStaticPaths 传递)
const { entry } = Astro.props;
const { Content, headings } = await entry.render();
// 如果找不到文章,返回 404
if (!entry) {
return Astro.redirect("/404");
}
const { Content, headings } = await render(entry);
const filtered = headings.filter(h => h.depth <= 2);
---

View File

@@ -7,7 +7,9 @@ import { getCollection } from "astro:content";
// 定义每页显示的文章数量
export const POSTS_PER_PAGE = 6;
// 生成分页路径
// 根据 Astro v5/v6对于 SSG 模式下的分页路由,仍然需要使用 getStaticPaths()
// 这是因为需要告诉 Astro 在构建时生成哪些页面
// 注意params 必须是字符串类型(符合 v6 要求)
export async function getStaticPaths() {
const allPosts = await getCollection("post");
@@ -25,21 +27,18 @@ export async function getStaticPaths() {
// 使用移除特色文章后的文章数量计算总页数
const totalPages = Math.ceil(posts.length / POSTS_PER_PAGE);
// 返回所有分页路径,确保 params.page 是字符串类型v6 要求)
return Array.from({ length: totalPages }, (_, i) => {
const page = i + 1;
return {
params: { page: page.toString() },
props: { page }
props: { page: page.toString() }
};
});
}
// 添加类型注解
interface Props {
page: string;
}
const { page }: Props = Astro.props;
// 从 props 获取页码(在 SSG 模式下通过 getStaticPaths 传递)
const { page } = Astro.props;
const currentPage = parseInt(page);
---

View File

@@ -16,7 +16,7 @@ description="Welcome to Rico's portfolio site! Explore my work as a web/UI desig
keywords="Rico, Portfolio, Web Design, UI Design, Frontend Development, Creative Designer"
>
<div
class="relative site-container z-20 w-full mx-auto mt-16 px-7 md:mt-18 lg:mt-20 xl:px-0"
class="relative site-container z-20 w-full mx-auto mt-16 px-4 md:mt-18 lg:mt-20 xl:px-0"
>
<div class="relative w-full px-4 flex flex-col items-center justify-between md:flex-row mb-16 ">
<div class="relative w-full md:max-w-[420px] md:w-1/2 text-center sm:text-left sm:-mt-8">

View File

@@ -7,13 +7,15 @@ export async function GET(context) {
title: 'Rico Portfolio Template Astro',
description: 'Astro Blog Template by Rico UI',
site: context.site,
items: blog.map((post) => ({
title: post.data.title,
pubDate: post.data.pubDate,
description: post.data.description,
// ...post.data,
link: `/blog/${post.slug}/`,
stylesheet: '/rss/pretty-feed-v3.xsl',
})),
items: blog.map((post) => {
const link = `/blog/${post.id}/`;
return {
title: post.data.title,
pubDate: post.data.publishDate,
description: post.data.description,
link,
stylesheet: '/rss/pretty-feed-v3.xsl',
};
}),
});
}

View File

@@ -6,7 +6,8 @@ import ActionBar from "@/components/widgets/ActionBar.astro";
import SeparatorLine from "@/components/elements/SeparatorLine.astro";
//自动导入 Image 组件和图像
const allImages = await Astro.glob('../../assets/work/luonmodels/*.{jpg,png,webp}');
const allImagesModules = import.meta.glob('../../assets/work/luonmodels/*.{jpg,png,webp}', { eager: true });
const allImages = Object.values(allImagesModules) as Array<{ default: any }>;
const logoUrl = "https://luonmodels.vercel.app/favicon.png";
---
<PostLayout

View File

@@ -5,8 +5,6 @@ import PageHeader from "@/components/elements/PageHeader.astro";
import ActionBar from "@/components/widgets/ActionBar.astro";
import SeparatorLine from "@/components/elements/SeparatorLine.astro";
//自动导入 Image 组件和图像
// const allImages = await Astro.glob('../../assets/work/ricoblog2024/*.{jpg,png,webp}');
// 手动导入图片
import P_01 from "../../assets/work/ricoblog2024/P_01.jpg";
@@ -29,7 +27,7 @@ import logoUrl from "../../assets/work/ricoblog2024/logo.png";
<main class="work-wrapper">
<PageHeader
title="Rico Designer Blog 2024"
tags={["Designer","Portfolio","Open Source"]}
tags={["Designer","Portfolio","Open Source"]}
className="mb-6 md:mb-8"
>
</PageHeader>
@@ -52,11 +50,7 @@ import logoUrl from "../../assets/work/ricoblog2024/logo.png";
<Image src={img} alt={`Rico Blog 2024 设计图 ${index + 1}`} class="w-full h-full object-cover" loading="lazy" decoding="async" quality={100}/>
</picture>
))}
<!-- {images.map((img, index) => (
<picture class="picture mt-8 md:mt-12 mb-8 md:mb-12 rounded-2xl overflow-hidden">
<Image src={img.default} alt={`Image ${index + 1}`} class="w-full h-full object-cover" loading="lazy" decoding="async" quality={100}/>
</picture>
))} -->
</div>
</section>
</main>