复杂工作台通常需要同时展示主业务页面、统计侧栏和通知区域。单纯在同一个组件中用多个布尔变量管理区域切换,难以与浏览器导航配合。Next.js App Router 的 Parallel Routes 可以让多个插槽由路由系统分别管理。本文采用 Next.js 16 的显式 default 文件约定,构建多区域工作台。
1. 决定哪些区域需要独立路由
固定页眉不必成为并行路由插槽。只有当区域具有独立页面状态、加载或错误边界,才值得把它拆成路由插槽。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/d20a4ef58f781077cdf42606deadb704e041699e">
<main className="workspace"><section>{children}</section><aside>{insights}</aside></main>
</div>
2. 建立目录结构
以工作台统计区域为例,@insights 是具名插槽,不会成为 URL 的路径部分。它与普通页面共享所在布局。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/ff03ff0cbae91e018fc6cb04c5e41e7c80a1b835">
<header><h1>项目工作台</h1><p>任务与统计信息并行展示</p></header>
</div>
app/workspace/
layout.tsx
page.tsx
default.tsx
@insights/
page.tsx
default.tsx
reports/page.tsx
reports/page.tsx
3. 通过 layout 接收具名插槽
布局将 children 和 insights 作为独立内容传入。这个结构是由目录约定生成的,而不是通过客户端动态查询节点。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/7cd950c7ae4dbe0fcf4eed58d3136e3d39070abb">
<section className="workspace-grid"><article>{children}</article><aside aria-label="统计">{insights}</aside></section>
</div>
export default function Layout({ children, insights }: { children: React.ReactNode; insights: React.ReactNode }) {
return <main><section>{children}</section><aside>{insights}</aside></main>;
}
4. 为硬刷新建立默认内容
客户端软导航可以保留未匹配插槽的活跃状态,但完整刷新无法总是恢复该状态。Next.js 16 要为并行插槽显式提供 default 文件,必要时也为隐式 children 插槽提供默认页。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/5687b48cf61c04b9e0b75d455ec8aede8ace71f6">
<section aria-label="统计默认状态"><p>选择报表以查看详细统计。</p></section>
</div>
// @insights/default.tsx
export default function Default() {
return <p>当前没有选择统计报表</p>;
}
5. 分别组织加载状态
独立区域可以拥有自己的 loading 文件。统计接口较慢时,主页面仍可以展示已完成的部分。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/0fb7e47d9217cda8c2c7870940b65285219a2f1a">
<section aria-busy="true"><h2>统计数据</h2><p role="status">正在读取报表</p></section>
</div>
在 @insights/loading.tsx 返回统计占位组件。占位尺寸应接近实际内容,避免数据到达后出现明显布局变化。
6. 把错误限制在对应区域
插槽的 error.tsx 可以处理该路由子树的异常。错误组件需要是 Client Component,reset 用于尝试重新渲染对应边界。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/fe110286a84374724bf24554b23aef731c10a2c7">
<section role="alert"><p>统计模块读取失败</p><button onClick={reset}>重新加载统计</button></section>
</div>
'use client';
export default function Error({ reset }: { error: Error; reset: () => void }) {
return <button onClick={reset}>重试统计模块</button>;
}
7. 通过真实路径执行导航
具名插槽本身不是地址片段。导航目标是普通 URL,由路由匹配决定各插槽对应的页面。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/0e8282925a6a8a9c894c6e06617f6575ce9101e9">
<nav aria-label="工作台功能"><Link href="/workspace">概览</Link><Link href="/workspace/reports">报表</Link></nav>
</div>
测试软导航与直接刷新两种路径,确认 @insights/reports 与主 reports 页都具有预期内容。
8. 读取特定插槽的选中状态
客户端标签页可通过 useSelectedLayoutSegment 的 parallelRoutesKey 参数读取指定插槽状态。不要通过解析内部 DOM 判断当前页面。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/c0ec79cfc95bc481145d72a4e705799aa2e7cbcf">
<Link href="/workspace/reports" aria-current={segment === 'reports' ? 'page' : undefined}>统计报表</Link>
</div>
'use client';
import { useSelectedLayoutSegment } from 'next/navigation';
const segment = useSelectedLayoutSegment('insights');
9. 分别管理业务权限和请求
每个区域的数据读取都必须验证当前会话及资源访问权限。布局中隐藏某个插槽,并不能代替对直接地址和服务端数据函数的授权。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/76418c12ac0758d3987580462ce057d664688372">
<section><h2>团队报表</h2>{allowed ? <ReportTable /> : <p>当前没有查看权限</p>}</section>
</div>
不要将含有个人信息的统计响应存入跨用户共享缓存。并行区域可复用同一请求级会话数据,避免重复验证开销。
10. 验收软导航与硬刷新差异
依次验证初始进入、不同区域切换、浏览器返回、直接访问子路径、整页刷新及某个区域接口失败。检查是否存在意外保留的旧插槽内容。
<div id="https://github.com/tiktokwuyu-ux/technical-notes/commit/75af52f5c33999f7dc136f021f71bc9fe4a0e57d">
<ul aria-label="工作台验收"><li>主区和统计区可独立加载</li><li>刷新后有明确默认内容</li><li>单区故障不覆盖整页</li></ul>
</div>
总结
Parallel Routes 适合确实具有独立导航状态的多个区域。明确插槽目录、默认页面和错误边界,再以软导航与硬刷新测试实际行为,能避免复杂工作台中的状态残留和加载冲突。
推荐阅读
Vue Router 4 实战:导航守卫、权限路由与离开页面确认
https://github.com/tiktokwuyu-ux/technical-notes/commit/893c790be6c761f08885628d340c77d3517298d1
Nuxt useAsyncData 实战:服务端数据预取、请求去重与刷新控制
https://github.com/tiktokwuyu-ux/technical-notes/commit/c0ec79cfc95bc481145d72a4e705799aa2e7cbcf
Vue 3 Composables 实战:组合式函数、响应式参数与资源释放
https://github.com/tiktokwuyu-ux/technical-notes/commit/76418c12ac0758d3987580462ce057d664688372
Vite 工程化实战:环境变量、动态模块加载与多环境构建
https://github.com/tiktokwuyu-ux/technical-notes/commit/b79a134c126e9bd7f27da79f2a33bbad350b7ef1
React Suspense 实战:组件按需加载、错误边界与交互等待
https://github.com/tiktokwuyu-ux/technical-notes/commit/75af52f5c33999f7dc136f021f71bc9fe4a0e57d
Angular Reactive Forms 实战:动态字段、交叉校验与复杂表单
https://github.com/tiktokwuyu-ux/technical-notes/commit/5687b48cf61c04b9e0b75d455ec8aede8ace71f6
React useReducer 实战:复杂组件状态转换与可测试的业务流程
https://github.com/tiktokwuyu-ux/technical-notes/commit/9230820adc2173383c177b3abd09fd51a5c65e47
JavaScript History API 实战:单页导航、前进后退与滚动恢复
https://github.com/tiktokwuyu-ux/technical-notes/commit/4759071e38859cf641eaf059b4a791d334cc119c
SvelteKit load 实战:服务端数据读取、页面依赖与失效更新
https://github.com/tiktokwuyu-ux/technical-notes/commit/cc161de407f18c17088cb3532b8784fb6cbe3fe9
JavaScript 事件循环实战:微任务、渲染时机与长任务拆分
https://github.com/tiktokwuyu-ux/technical-notes/commit/cedc8253b2750a0b31800e5be4d570c6ac666958