Command Palette

Search for a command to run...

Back to skills
skill № 57851

astro-routing

Astro 7 文件路由:页面、getStaticPaths 动态路由、paginate 分页、txt/xml 生成型端点、重定向与 404,本框架为纯静态 + trailingSlash always。触发词:路由、页面、动态路由、getStaticPaths、分页、paginate、重定向、trailingSla…

FreeNo executable scripts
bkywksjby bkywksj★ 55 GitHub stars

Install astro-routing in Claude Code & Claude Desktop

Sign up to install this skill

Create a free account to reveal the install command and save the skill to your library.

  • Reveal the one-line install command
  • Save skills to your synced library
  • Get notified when skills update
Sign up freeI already have an account

About this skill

Astro 路由(next-ai-astro)

何时使用

  • 新增页面 / 栏目,确定 URL 放在哪
  • 需要按集合数据批量生成详情页、分类页、标签页
  • 列表太长需要分页(/works/page/2/ 这类)
  • 需要生成 txt / xml / json 等非 HTML 文件(robots、llms.txt、feed)
  • 改了 URL 结构,需要把旧地址重定向到新地址
  • 页面 404、路由冲突、参数取不到

概述

Astro 是文件即路由:src/pages/ 下每个 .astro / .md / .ts 文件对应一个 URL。 本框架是纯静态站(output: 'static'),所以所有路由都在构建期展开:动态路由必须用 getStaticPaths() 把全部参数列出来,构建后变成 dist/ 里的一个个 index.html。

🔴 写路由相关 API 前,先对照 CLAUDE.md「Astro 7 版本红线」;拿不准查 context7 /withastro/docs。


本框架现状

路由清单

文件 URL 类型 说明
src/pages/index.astro / 静态 首页
src/pages/works/index.astro /works/ 静态 + island 列表,筛选交给 WorksExplorer
src/pages/works/[id].astro /works/<id>/ 动态 每条作品一页
src/pages/works/category/[id].astro /works/category/<id>/ 动态 每个分类一页
src/pages/posts/index.astro /posts/ 静态 文章列表
src/pages/posts/[...id].astro /posts/<id>/ 动态(rest) id 可能含子目录,所以用 [...id]
src/pages/about.astro /about/ 静态
src/pages/404.astro /404.html 静态 带 noindex
src/pages/robots.txt.ts /robots.txt 端点 动态生成,Sitemap 地址跟随 site.url
src/pages/llms.txt.ts /llms.txt 端点 由集合数据生成
(@astrojs/sitemap) /sitemap-index.xml 集成生成 过滤掉 404

全局约束(astro.config.ts)

export default defineConfig({
  site: site.url,          // 来自 src/site.config.ts,canonical / sitemap 都基于它
  trailingSlash: 'always', // 🔴 所有页面 URL 带结尾斜杠
  compressHTML: true,
  // ...
});
  • trailingSlash: 'always':/works/a/ 合法;/works/a 在 dev 下不匹配路由,只显示一张提醒你配置了 always 的警告页 (官方 configuration reference)。线上预渲染页的斜杠由托管方处理(本框架 nginx try_files $uri $uri/ 会 301 补斜杠)。站内链接一律带结尾斜杠。
  • 带扩展名的端点(/robots.txt、/llms.txt)不带结尾斜杠(Astro 6 起带扩展名的端点不能有结尾斜杠)。
  • 链接统一用 src/lib/works.ts 的 workPath() / categoryPath() 生成,不手拼字符串。

标准做法

1. 新增一个静态页面

---
// 联系我们页
import BaseLayout from '@/layouts/BaseLayout.astro';
import Container from '@/components/ui/Container.astro';
import Breadcrumbs from '@/components/ui/Breadcrumbs.astro';
import { breadcrumbJsonLd, type Crumb } from '@/lib/seo';

const title = '联系我们';
const description = '通过邮件或社交平台联系站点作者,说明合作、投稿与勘误的处理方式。';
const crumbs: Crumb[] = [
  { name: '首页', path: '/' },
  { name: title, path: '/contact/' },
];
---

<BaseLayout title={title} description={description} jsonLd={[breadcrumbJsonLd(crumbs)]}>
  <Container size="narrow" class="py-10 sm:py-14">
    <Breadcrumbs crumbs={crumbs} />
    <h1 class="mt-6 text-3xl font-semibold tracking-tight">{title}</h1>
  </Container>
</BaseLayout>

放在 src/pages/contact.astro → URL /contact/。然后在 src/site.config.ts 的 nav 里加入口(如需要)。

2. 动态路由:按集合生成详情页

框架标准写法(参考 src/pages/works/[id].astro):

---
import type { GetStaticPaths, InferGetStaticPropsType } from 'astro';
import { getWorks } from '@/lib/works';

export const getStaticPaths = (async () => {
  const works = await getWorks();
  // params 的值只能是字符串(URL 编码后进路径);props 把整条数据直接带给页面,省一次查询
  return works.map((work) => ({ params: { id: work.id }, props: { work } }));
}) satisfies GetStaticPaths;

type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const { work } = Astro.props as Props;
---

要点:

  • 用 satisfies GetStaticPaths + InferGetStaticPropsType 拿到 props 类型,不要 any。
  • params 只能是字符串(官方:params are encoded into the URL, so only strings are supported)。数字 id 要 String(id)。
  • getStaticPaths 里不要用 Astro 对象(Astro 6 起已弃用),需要站点地址用 import.meta.env.SITE。
  • id 会直接进 URL:集合 schema 已用正则限制为 [A-Za-z0-9_-],新集合照做(见 content-collections)。

3. [id] 还是 [...id]

写法 匹配 适用
[id].astro 单段:/works/a/ JSON 集合、id 不含斜杠
[...id].astro 多段:/posts/2026/a/,也能匹配空 glob 集合(Markdown 放子目录时 id 带 /)

posts 用 [...id] 就是因为 src/content/posts/2026/xxx.md 的 id 是 2026/xxx。

4. 分页(paginate)

列表数据量大、又想让每一页都能被收录时,用 paginate() 生成真实分页 URL(和 island 的「加载更多」互补:island 管交互,分页页管收录)。

---
// src/pages/works/page/[...page].astro → /works/page/ 、/works/page/2/ ……
import type { GetStaticPaths, InferGetStaticPropsType } from 'astro';
import { getCategories, getWorks, toCardList } from '@/lib/works';

export const getStaticPaths = (async ({ paginate }) => {
  const works = await getWorks();
  return paginate(works, { pageSize: 24 });
}) satisfies GetStaticPaths;

type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const { page } = Astro.props as Props;
const cards = await toCardList(page.data, await getCategories());
---
<!-- page.currentPage / page.lastPage / page.url.prev / page.url.next / page.url.first / page.url.last -->

page 属性(官方 routing reference):data、start、end、size、total、currentPage(从 1 开始)、lastPage、 url.current / url.prev / url.next / url.first / url.last。

[page] vs [...page](官方原话):/posts/[page].astro 生成 /posts/1、/posts/2…; /posts/[...page].astro 生成 /posts、/posts/2…(第一页落在根路径)。**本框架用 `[.

Full SKILL.md on GitHub

Allowed tools

Tools this skill is permitted to call.

No restriction — this skill can use any tool.

FAQ

What does the astro-routing skill do?

Astro 7 文件路由:页面、getStaticPaths 动态路由、paginate 分页、txt/xml 生成型端点、重定向与 404,本框架为纯静态 + trailingSlash always。触发词:路由、页面、动态路由、getStaticPaths、分页、paginate、重定向、trailingSlash、404、端点、生成txt或xml、URL结构

How do I install the astro-routing skill?

Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.

Does the astro-routing skill run scripts?

No, this skill is instructions only (SKILL.md) with no executable scripts.

Related skills

Compare astro-routing with