Spa routes

From lobehub/lobehub · View interactive version

Spa routes is an AI agent skill from the GitHub repository lobehub/lobehub. Use for SPA routes and page segments: Web/desktop/mobile/popup routers, redirects and src/routes-to-features boundaries.

How to use this skill

Skill file

SPA Routes and Features Guide

SPA structure:

This project uses a roots vs features split: src/routes/ only holds page segments; business logic and UI live in src/features/ by domain.

Agent constraint — 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.

When to Use This Skill


1. What Belongs in `src/routes/` (roots)

Each route directory should contain only:

| File / folder | Purpose |

| --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ |

| _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/*. |

| index.tsx or page.tsx | Page entry for this segment. Only import from features and render; no business logic. |

| [param]/index.tsx (e.g. [id], [cronId]) | Dynamic segment page. Same rule: thin, delegate to features. |

Rule: Route files should only import and compose. No new features/ folders or heavy components inside src/routes/.


2. What Belongs in `src/features/`

Put domain-oriented UI and logic here:

Organize 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.

Each feature should:


3. How to Add a New SPA Route

  1. Choose the route group
  1. Create only segment files under src/routes/
  1. Implement layout and page content in src/features/
  1. Keep route files thin
  1. Register the route in the correct definition layer
  1. Register a route skeleton (REQUIRED for every lazy route)

3a. Shared desktop route definition and platform adapters

| File | Role |

| ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |

| desktopRouter.shared.tsx | Single source of truth for common paths, nesting, metadata, lazy imports, and prioritized route preload groups. |

| desktopRouter.config.tsx | Thin Web adapter: mounts the common content tree at / and adds Web-only routes. |

| desktopRouter.config.desktop.tsx | Thin Electron adapter: injects per-tab Home behavior, TabHost root stubs, and the Electron-only /desktop-onboarding local setup. |

Add 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) — 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.


3b. Other `.desktop.{ts,tsx}` variants inside `src/routes/`

The 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.

Known variants today:

| Base file (web) | Desktop file (Electron) | Purpose |

| ------------------------------------------------ | -------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |

| src/features/Settings/features/componentMap.ts | src/features/Settings/features/componentMap.desktop.ts | Settings tab → component map. Web uses dynamic import(); desktop uses sync imports. componentMap.sync.test.ts enforces identical keys. |

| 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). |

| src/routes/(main)/group/index.tsx | src/routes/(main)/group/index.desktop.tsx | Same pattern as agent. |

Rules:

  1. 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.
  2. 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.
  3. 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 — only add a new variant when the two trees genuinely diverge (different store wiring, different popup guards, etc.).
  4. When deleting, remove both files together.

3c. Mobile counterparts of desktop routes

The 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).

| Mobile file | Reuses |

| ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------ |

| mobileAgentRoutes.tsx | agentPageChildren under the agent scope layout; group profile / permission |

| mobileWorkspaceRoutes.tsx | createTaskWorkspaceChildren, projectWorkspaceRoute, resourceRoute, memoryChildren, inside the mobile page layout |

| mobileWorkspaceSettingsRoutes.tsx | workspaceSettingsContentRoute children plus the mobile settings menu and full-bleed tabs |


4. How to Divide Files (route vs feature)

| Question | Put in src/routes/ | Put in src/features/ |

| -------------------------------------------------------- | -------------------------------------------------------- | ---------------------------- |

| Is it the route’s layout wrapper or page entry? | Yes – _layout/index.tsx, index.tsx, [id]/index.tsx | No |

| Does it contain business logic or non-trivial UI? | No | Yes – under the right domain |

| Is it a reusable layout piece (sidebar, header, body)? | No | Yes |

| Is it a hook, store usage, or domain logic? | No | Yes |

| Is it only re-exporting or composing feature components? | Yes | No |

Examples

src/routes/(main)/page/_layout/index.tsx → export { default } from '@/features/Pages/PageLayout'

src/features/Pages/PageLayout/ → Sidebar, DataSync, Body, Header, styles, etc.

src/routes/(main)/page/index.tsx → Import PageTitle, PageExplorerPlaceholder from @/features/Pages and @/features/PageExplorer; render with <PageTitle /> and placeholder.

Page list, actions, drawers, and hooks live under src/features/Pages/.


5. Progressive Migration (existing code)

Existing 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.

When touching an old route that still has logic or features/ inside src/routes/:

  1. Prefer adding new code in src/features/<Domain>/ and importing from routes.
  2. 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).
  3. Use git mv when moving files so history is preserved.

6. Reference Structure (after Phase 1)

Route (thin):

src/routes/(main)/page/
├── _layout/index.tsx   → re-export or compose from @/features/Pages/PageLayout
├── index.tsx          → import from @/features/Pages, @/features/PageExplorer
└── [id]/index.tsx     → import from @/features/Pages, @/features/PageExplorer

Feature (implementation):

src/features/Pages/
├── index.ts            → export PageLayout, PageTitle
├── PageTitle.tsx
└── PageLayout/
    ├── index.tsx       → Sidebar + Outlet + DataSync
    ├── DataSync.tsx
    ├── Sidebar.tsx
    ├── style.ts
    ├── Body/           → list, actions, drawer, etc.
    └── Header/         → breadcrumb, add button, etc.

Router config continues to point at route paths (e.g. @/routes/(main)/page, @/routes/(main)/page/_layout); route files then delegate to features.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.