client-side-routing
它能做什么
这项技能说明如何在 Agent-Native 应用中添加页面,同时保持共享外壳处于挂载状态。外壳包括智能体侧边栏和导航。适合为这些 React 应用添加路由的开发者。
适用场景
- ✓当你要为 Agent-Native 应用添加新页面的时候。
- ✓当用户每次切换页面,智能体侧边栏都会重新加载的时候。
- ✓当你需要在根布局和无路径布局路由之间做选择的时候。
技能链接
https://github.com/BuilderIO/agent-native/blob/main/.agents/skills/client-side-routing/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“你能为我的应用添加一个新的报告页面,并确保我跳转过去时智能体侧边栏不会重新加载吗?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体把共享外壳挂载一次,位于页面出口之上;当部分页面公开、部分页面私有时,它会使用无路径布局路由。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Client-Side Routing
Rule
All templates are single-page apps using React Router. Navigation must not remount the app shell. The agent sidebar, document tree, and any other persistent chrome must survive route changes.
Why
If the shell unmounts on every navigation, the agent chat reconnects/reloads, destroying in-progress work and hammering the backend.
Hard Rule
The app shell (AgentSidebar + any top-level navigation) must be mounted ONCE, above the <Outlet />. Never wrap each page in its own <AppLayout> / <Layout>. React sees a different component at the outlet position on each nav and unmounts the entire subtree.
Two Correct Patterns
1. All routes need the shell
Mount <AppLayout> in root.tsx around <Outlet />:
// app/root.tsx
<AppLayout>
<Outlet />
</AppLayout>
2. Mix of protected and public routes
Use a React Router pathless layout route:
app/routes/
_app.tsx # renders <AppLayout><Outlet /></AppLayout>
_app._index.tsx # / → under AppLayout
_app.settings.tsx # /settings → under AppLayout
_app.team.tsx # /team → under AppLayout
book.$slug.tsx # /book/:slug → no layout (public)
f.$.tsx # /f/* → no layout (public form filler)
The _ prefix on _app.tsx makes it a pathless parent — it contributes the layout but no URL segment. Route files prefixed with _app. nest under it and share the layout instance across navigations.
Anti-Pattern
// ❌ BAD — each route wraps its own Layout, causing full remount on every nav
export default function Settings() {
return (
<AppLayout>
<SettingsContent />
</AppLayout>
);
}
If a page needs per-route data (e.g. sidebar highlighting the active document), derive it inside the layout from useParams() / useLocation() — don't pass it as a prop through every route file.
Chat-First Routes
If / is a full-page chat such as AgentChatHome, keep the app shell mounted
around it when possible so AgentSidebar URL sync and route warmup stay active.
If the chat route intentionally lives outside the shell, add a tiny app-owned
prewarm for the routes agents commonly open from that chat. In local dev,
React Router can update the address bar before a cold Vite route chunk commits,
which makes navigation look broken even when the URL is correct.
Adding a New Route
- Pattern #1 (AppLayout in
root.tsx): just render page content — nothing else. - Pattern #2 (pathless
_app.tsx): name the file_app.<segment>.tsxfor authed routes, or bare<segment>.tsxfor public routes.
Related Skills
adding-a-feature— The four-area checklist referencing route layout patternscontext-awareness— Navigation state written on every route change
常见问题
为什么智能体侧边栏会重新加载?
通常是因为每个页面都用自己的布局包裹自身,导致导航时外壳被重新挂载。
什么是无路径布局路由?
它是一个父级路由,用于添加布局,但不会在网址中增加路径部分。
使用的是哪个框架?
技能说明这些应用以单页应用的形式使用 React Router。
详情
作者的原始描述
How to add routes without remounting the app shell. Use when adding a new route, fixing agent sidebar reloads on navigation, or choosing between `root.tsx` layout and pathless `_app.tsx` layout patterns.
repo: BuilderIO/agent-native
热度
- GitHub 星标
- 7.6k
- 排名
- #13146
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …