{"data":{"slug":"github-lobehub-lobehub-agents-skills-spa-routes-1187fe25b54d","title":"SPA Route and Feature Organization","summary":"Organize SPA routes and features using the roots vs features split. Route files contain layout and page entry points, while features hold domain-specific logic and UI. Deliverable includes structured route and feature files under `src\/routes\/` and `src\/features\/`.","url":"https:\/\/skillshunter.net\/skills\/github-lobehub-lobehub-agents-skills-spa-routes-1187fe25b54d","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-lobehub-lobehub-agents-skills-spa-routes-1187fe25b54d","markdown_url":"https:\/\/skillshunter.net\/skills\/github-lobehub-lobehub-agents-skills-spa-routes-1187fe25b54d.md","download_url":"https:\/\/skillshunter.net\/skills\/github-lobehub-lobehub-agents-skills-spa-routes-1187fe25b54d\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Before adding a new route, ensure the route group is selected from `(main)\/`, `(mobile)\/`, `(desktop)\/`, or special flows like `onboarding\/`, `share\/`. Create only segment files under `src\/routes\/` and implement layout and page content in `src\/features\/` by domain. Use `desktopRouter.shared.tsx` for shared Web\/Electron route definitions and avoid duplicating routes in platform adapters.","instructions":"---\nname: spa-routes\ndescription: 'Use for SPA routes and page segments: Web\/desktop\/mobile\/popup routers, redirects and src\/routes-to-features boundaries.'\nuser-invocable: false\n---\n\n# SPA Routes and Features Guide\n\nSPA structure:\n\n- **`src\/spa\/`** \u2013 Entry points (`entry.web.tsx`, `entry.mobile.tsx`, `entry.desktop.tsx`) and router config (`router\/`). Router lives here to avoid confusion with `src\/routes\/`.\n- **`src\/routes\/`** \u2013 Page segments only (roots).\n- **`src\/features\/`** \u2013 Business logic and UI by domain.\n\nThis project uses a **roots vs features** split: `src\/routes\/` only holds page segments; business logic and UI live in `src\/features\/` by domain.\n\n**Agent constraint \u2014 shared desktop router:** Common Web\/Electron paths, nesting, metadata, lazy loaders, and preload groups belong in `src\/spa\/router\/desktopRouter.shared.tsx`. The two `desktopRouter.config*` files are thin platform adapters; change them only for genuine runtime differences. Do not duplicate a common route in both adapters.\n\n## When to Use This Skill\n\n- Adding a new SPA route or route segment\n- Defining or refactoring layout\/page files under `src\/routes\/`\n- Moving route-specific components or logic into `src\/features\/`\n- Deciding where to put a new component (route folder vs feature folder)\n\n---\n\n## 1. What Belongs in `src\/routes\/` (roots)\n\nEach route directory should contain **only**:\n\n| File \/ folder                                 | Purpose                                                                                                                                                      |\n| --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| `_layout\/index.tsx` or `layout.tsx`           | Layout for this segment: wrap with `<Outlet \/>`, optional shell (e.g. sidebar + main). Should be thin: prefer re-exporting or composing from `@\/features\/*`. |\n| `index.tsx` or `page.tsx`                     | Page entry for this segment. Only import from features and render; no business logic.                                                                        |\n| `[param]\/index.tsx` (e.g. `[id]`, `[cronId]`) | Dynamic segment page. Same rule: thin, delegate to features.                                                                                                 |\n\n**Rule:** Route files should only **import and compose**. No new `features\/` folders or heavy components inside `src\/routes\/`.\n\n---\n\n## 2. What Belongs in `src\/features\/`\n\nPut **domain-oriented** UI and logic here:\n\n- Layout building blocks: sidebars, headers, body panels, drawers\n- Hooks and store usage for that domain\n- Domain-specific forms, lists, modals, etc.\n\nOrganize by **domain** (e.g. `Pages`, `Home`, `Agent`, `PageEditor`), not by route path. One route can use several features; one feature can be used by several routes.\n\nEach feature should:\n\n- Live under `src\/features\/<FeatureName>\/`\n- Export a clear public API via `index.ts` or `index.tsx`\n- Use `@\/features\/<FeatureName>\/...` for internal imports when needed\n\n---\n\n## 3. How to Add a New SPA Route\n\n1. **Choose the route group**\n   - `(main)\/` \u2013 desktop main app\n   - `(mobile)\/` \u2013 mobile\n   - `(desktop)\/` \u2013 Electron-specific\n   - `onboarding\/`, `share\/` \u2013 special flows\n\n2. **Create only segment files under `src\/routes\/`**\n   - e.g. `src\/routes\/(main)\/my-feature\/_layout\/index.tsx` and `src\/routes\/(main)\/my-feature\/index.tsx` (and optional `[id]\/index.tsx`).\n\n3. **Implement layout and page content in `src\/features\/`**\n   - Create or reuse a domain (e.g. `src\/features\/MyFeature\/`).\n   - Put layout (sidebar, header, body) and page UI there; export from the feature\u2019s `index`.\n\n4. **Keep route files thin**\n   - Layout: `export { default } from '@\/features\/MyFeature\/MyLayout'` or compose a few feature components + `<Outlet \/>`.\n   - Page: import from `@\/features\/MyFeature` (or a specific subpath) and render; no business logic in the route file.\n\n5. **Register the route in the correct definition layer**\n   - **Shared Web\/Electron route:** add the segment once in `desktopRouter.shared.tsx` with `dynamicElement` \/ `dynamicLayout`. Put its `preloadId` there as part of the shared lazy-loader definition.\n   - **Web-only or Electron-only route:** add it to the corresponding thin `desktopRouter.config.tsx` adapter. Keep platform-only differences explicit and small.\n   - **Mobile counterpart (required for every desktop main-area route):** see \u00a73c. `mobileRouter.parity.test.ts` fails until the mobile router resolves the new URL to the same pattern, or the route is listed in `MOBILE_UNSUPPORTED` with a reason.\n   - **Mobile-only flow:** use `mobileRouter.config.tsx`.\n\n6. **Register a route skeleton (REQUIRED for every lazy route)**\n   - Every lazy route inside the main area renders `RouteSegmentSkeleton` (`src\/components\/Skeleton\/RouteSegment.tsx`) while its chunk loads. It resolves via `handle.meta.Skeleton` only (deepest match wins, walking up through parent routes). Omitting `Skeleton` renders a blank pane \u2014 use `NoRouteSkeleton` when that is intentional.\n   - Pick the skeleton when adding or restructuring a route:\n     - A close-enough generic shape exists \u2192 `Skeleton: createSurfaceSkeleton('list' | 'form' | 'grid' | 'editor' | 'detail')` from `@\/components\/Skeleton\/Surface`.\n     - The page has a distinctive layout (dashboard, multi-panel, conversation) \u2192 author a bespoke component under `src\/components\/Skeleton\/` and register it (see `ResourceHome.tsx`, `Generation.tsx`, `Conversation\/`).\n   - Where to put it: on the route's `routeMeta` (feature `routeMeta.ts` or inline in `desktopRouter.shared.tsx`). A whole subtree sharing one shape can register once on the parent\/layout route's `handle` \u2014 children with their own `Skeleton` still override.\n   - When changing a page's layout, update its registered skeleton in the same PR \u2014 a stale skeleton that no longer matches the page is a regression.\n   - Skeleton-only parent handles are safe for titles: title\/icon resolution also walks deepest-first, and leaf metas keep winning.\n   - The same deepest skeleton renders at every pending boundary of the chain (layout chunk, page chunk, then the page's own data wait). Make the page's in-component loading state the registered skeleton, or a body that continues it; a different shape there reads as a second, unrelated skeleton.\n   - Route skeletons are always static imports: a fallback has to paint on its first render, and a `lazy()` skeleton (preloaded or not) renders blank until its chunk lands. That puts the skeleton's whole import graph in the entry chunk, so it may only reach UI primitives, other skeletons, constants and `import type`. Keep stores, services, feature components and value imports from barrels (`@\/types\/*` re-exports zod schemas) out of it; move the minimal piece next to the skeleton instead (see `ResourceHome\/Skeleton` and `Explorer\/viewMode.ts`).\n   - A custom fallback (`fallback:` option, in-page `Suspense`, or an early `return <XSkeleton \/>`) keeps the handover continuous only through `delayed()` \/ `DelayedFallback` or `RouteLoading`, which register the skeleton as visible (`retainSkeleton`). Rendering a skeleton any other way lets the next boundary wait out its 200ms delay on a blank pane.\n\n---\n\n## 3a. Shared desktop route definition and platform adapters\n\n| File                               | Role                                                                                                                               |\n| ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |\n| `desktopRouter.shared.tsx`         | Single source of truth for common paths, nesting, metadata, lazy imports, and prioritized route preload groups.                    |\n| `desktopRouter.config.tsx`         | Thin Web adapter: mounts the common content tree at `\/` and adds Web-only routes.                                                  |\n| `desktopRouter.config.desktop.tsx` | Thin Electron adapter: injects per-tab Home behavior, TabHost root stubs, and the Electron-only `\/desktop-onboarding` local setup. |\n\nAdd or remove common routes only in `desktopRouter.shared.tsx`, or in a subtree module it imports (`taskWorkspaceRoutes.tsx`, `projectWorkspaceRoutes.tsx`, `agentPageRoutes.tsx`, `memoryRoutes.tsx`, `resourceRoutes.tsx`, `workspaceSettingsRoutes.tsx` for the padded `\/:slug\/settings\/*` tabs) \u2014 the shared file is over the 1200-line limit, so large subtrees move out instead of growing it. Add any new subtree module to `readRouterSources` in `desktopRouter.sync.test.tsx`. Keep `desktopRouter.sync.test.tsx` passing so path behavior, lazy boundaries, preload ownership, and the intentional platform differences remain verified.\n\n---\n\n## 3b. Other `.desktop.{ts,tsx}` variants inside `src\/routes\/`\n\nThe thin router adapters are not duplicated trees. Other route modules may still colocate a `<name>.desktop.{ts,tsx}` next to a base `<name>.{ts,tsx}`; Vite's resolver swaps in the `.desktop` file for Electron builds. Those paired module implementations still carry a drift risk.\n\nKnown variants today:\n\n| Base file (web)                                  | Desktop file (Electron)                                  | Purpose                                                                                                                                    |\n| ------------------------------------------------ | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |\n| `src\/features\/Settings\/features\/componentMap.ts` | `src\/features\/Settings\/features\/componentMap.desktop.ts` | Settings tab \u2192 component map. Web uses dynamic `import()`; desktop uses sync imports. `componentMap.sync.test.ts` enforces identical keys. |\n| `src\/routes\/(main)\/agent\/index.tsx`              | `src\/routes\/(main)\/agent\/index.desktop.tsx`              | Page entry. Desktop variant overrides the web page wholesale (e.g. extra popup guards).                                                    |\n| `src\/routes\/(main)\/group\/index.tsx`              | `src\/routes\/(main)\/group\/index.desktop.tsx`              | Same pattern as agent.                                                                                                                     |\n\n**Rules:**\n\n1. After editing **any** `.ts`\/`.tsx` under `src\/routes\/`, glob the same directory for a `<filename>.desktop.{ts,tsx}` sibling. If one exists, apply the equivalent change there in the same commit.\n2. When adding a new SettingsTab, register it in **both** `componentMap.ts` (with `dynamic(...)`) and `componentMap.desktop.ts` (with a sync `import`). `componentMap.sync.test.ts` will fail the build otherwise.\n3. When adding a new desktop-only page wholesale-override, prefer a single base file with platform-aware code over introducing a new `.desktop.tsx` variant \u2014 only add a new variant when the two trees genuinely diverge (different store wiring, different popup guards, etc.).\n4. When deleting, remove **both** files together.\n\n---\n\n## 3c. Mobile counterparts of desktop routes\n\nThe mobile web build has its own router (`mobileRouter.config.tsx`, `__MOBILE__`), but it mounts the **same page segments** as desktop. Do not copy a desktop route into the mobile files: reuse the subtree module and adjust only what differs with `overlayRoutes` (`routeOverlay.ts`; `withHandle`, `dropRoute`, or a mobile `element`).\n\n| Mobile file                         | Reuses                                                                                                                   |\n| ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |\n| `mobileAgentRoutes.tsx`             | `agentPageChildren` under the agent scope layout; group profile \/ permission                                             |\n| `mobileWorkspaceRoutes.tsx`         | `createTaskWorkspaceChildren`, `projectWorkspaceRoute`, `resourceRoute`, `memoryChildren`, inside the mobile page layout |\n| `mobileWorkspaceSettingsRoutes.tsx` | `workspaceSettingsContentRoute` children plus the mobile settings menu and full-bleed tabs                               |\n\n- A desktop subtree added to one of those modules reaches mobile automatically. A new top-level desktop surface needs one entry in the matching mobile file; wrap full-screen pages in `pageLayout` (`(mobile)\/_page-layout`).\n- Mobile `NavHeader` draws back + the route's `NavPanelPortal` sidebar as a drawer, and falls back to the route `titleKey` when the page header has no title. A page that paints no `NavHeader` gets one from the route handle `mobileShellHeader: true | 'untitled'` (and `mobileShellTitle` for a record name). Do not add a second back button inside the page; opt out with `showTogglePanelButton={false}` when the page keeps its own.\n- Desktop-only surfaces go in `mobileUnsupportedRoutes` and `MOBILE_UNSUPPORTED` in `mobileRouter.parity.test.ts`, so they redirect home instead of being read as a `\/:workspaceSlug`.\n\n---\n\n## 4. How to Divide Files (route vs feature)\n\n| Question                                                 | Put in `src\/routes\/`                                     | Put in `src\/features\/`       |\n| -------------------------------------------------------- | -------------------------------------------------------- | ---------------------------- |\n| Is it the route\u2019s layout wrapper or page entry?          | Yes \u2013 `_layout\/index.tsx`, `index.tsx`, `[id]\/index.tsx` | No                           |\n| Does it contain business logic or non-trivial UI?        | No                                                       | Yes \u2013 under the right domain |\n| Is it a reusable layout piece (sidebar, header, body)?   | No                                                       | Yes                          |\n| Is it a hook, store usage, or domain logic?              | No                                                       | Yes                          |\n| Is it only re-exporting or composing feature components? | Yes                                                      | No                           |\n\n**Examples**\n\n- **Route (thin):**\\\n  `src\/routes\/(main)\/page\/_layout\/index.tsx` \u2192 `export { default } from '@\/features\/Pages\/PageLayout'`\n- **Feature (real implementation):**\\\n  `src\/features\/Pages\/PageLayout\/` \u2192 Sidebar, DataSync, Body, Header, styles, etc.\n- **Route (thin):**\\\n  `src\/routes\/(main)\/page\/index.tsx` \u2192 Import `PageTitle`, `PageExplorerPlaceholder` from `@\/features\/Pages` and `@\/features\/PageExplorer`; render with `<PageTitle \/>` and placeholder.\n- **Feature:**\\\n  Page list, actions, drawers, and hooks live under `src\/features\/Pages\/`.\n\n---\n\n## 5. Progressive Migration (existing code)\n\nExisting routes follow this structure unevenly: some are already thin segment files (e.g. `src\/routes\/(main)\/page\/`, `src\/routes\/(main)\/settings\/`), while others still hold logic or a `features\/` folder under `src\/routes\/`. Check the route's current state instead of assuming.\n\nWhen touching an old route that still has logic or `features\/` inside `src\/routes\/`:\n\n1. Prefer adding **new** code in `src\/features\/<Domain>\/` and importing from routes.\n2. For larger refactors, move existing route-only logic into the right feature and then thin out the route files (re-export or compose from features).\n3. Use `git mv` when moving files so history is preserved.\n\n---\n\n## 6. Reference Structure (after Phase 1)\n\n**Route (thin):**\n\n```\nsrc\/routes\/(main)\/page\/\n\u251c\u2500\u2500 _layout\/index.tsx   \u2192 re-export or compose from @\/features\/Pages\/PageLayout\n\u251c\u2500\u2500 index.tsx          \u2192 import from @\/features\/Pages, @\/features\/PageExplorer\n\u2514\u2500\u2500 [id]\/index.tsx     \u2192 import from @\/features\/Pages, @\/features\/PageExplorer\n```\n\n**Feature (implementation):**\n\n```\nsrc\/features\/Pages\/\n\u251c\u2500\u2500 index.ts            \u2192 export PageLayout, PageTitle\n\u251c\u2500\u2500 PageTitle.tsx\n\u2514\u2500\u2500 PageLayout\/\n    \u251c\u2500\u2500 index.tsx       \u2192 Sidebar + Outlet + DataSync\n    \u251c\u2500\u2500 DataSync.tsx\n    \u251c\u2500\u2500 Sidebar.tsx\n    \u251c\u2500\u2500 style.ts\n    \u251c\u2500\u2500 Body\/           \u2192 list, actions, drawer, etc.\n    \u2514\u2500\u2500 Header\/         \u2192 breadcrumb, add button, etc.\n```\n\nRouter config continues to point at **route** paths (e.g. `@\/routes\/(main)\/page`, `@\/routes\/(main)\/page\/_layout`); route files then delegate to features.\n","use_cases":["Adding a new SPA route or route segment","Defining or refactoring layout\/page files under src\/routes\/","Moving route-specific components or logic into src\/features\/","Deciding where to put a new component (route folder vs feature folder)"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/lobehub\/lobehub\/blob\/54382b5c8e81544da454fa6d7b50f461a562c1ee\/.agents\/skills\/spa-routes\/SKILL.md","source_commit":"54382b5c8e81544da454fa6d7b50f461a562c1ee","license":null,"version":null,"listing_approved_at":"2026-11-22","created_at":"2026-11-21T11:32:52.000000Z","updated_at":"2026-11-22T11:17:19.000000Z","seo_title":"SPA Route and Feature Organization Guide","seo_description":"Learn how to organize SPA routes and features using the roots vs features split. Route files contain layout and page entry points, while features hold domain-specific UI.","seo_keywords":["SPA route organization","feature vs route structure","SPA layout and page structure","SPA routing best practices","SPA feature organization"]}}