Internal Architecture Overview
Relevant source files
The following files were used as context for generating this wiki page:
wouter-vue is a minimalist, high-performance routing library for Vue 3, heavily inspired by the minimalist philosophy of the original React wouter. Its architecture is built around the Vue 3 Composition API and leverages a "hook-first" approach to manage navigation, route matching, and state. Unlike traditional routers that require a centralized configuration, wouter-vue allows for decentralized routing through components like <Route /> and <Link />, while maintaining high throughput and low memory overhead.
The system is designed to be highly modular, separating path resolution, reactive state management (location/params), and build-time optimization. It specifically targets performance-sensitive applications, achieving 72% higher throughput compared to standard routing solutions by minimizing dependencies and using efficient matching patterns via path-to-regexp.
Sources: README.md:1-50, examples/example/README.md:1-20
Core Routing Mechanism
The routing engine relies on the Vue 3 reactivity system to track the current URL and trigger re-renders. The architecture is decentralized, meaning individual <Route> components listen to location changes and determine their own visibility based on pattern matching.
High-Level Component Flow
The following diagram illustrates how a URL change propagates through the system to update the UI:
The system supports multiple location strategies including browser history (default), hash-based routing, and memory-based routing for SSR or testing.
Sources: README.md:52-70, package.json:60-80, types/helpers.d.ts:1-15
Key Composition API Hooks
The internal logic is exposed through a set of composable functions that interact with the router's reactive state. These hooks form the backbone of both the internal components and user-land custom routing logic.
| Hook | Purpose | Data Type |
|---|---|---|
useLocation | Returns the current path and a function to navigate. | [Ref<string>, Function] |
useParams | Extracts dynamic parameters from the current matching route. | ComputedRef<Record<string, string>> |
useSearchParams | Provides reactive access to URL query parameters. | Ref<URLSearchParams> |
useRouteData | Accesses hierarchical data passed down the routing tree. | Ref<any> |
Sources: examples/example/src/pages/AboutPage.vue:85-115, examples/example/README.md:45-65, types/helpers.d.ts:7-15
Hierarchical Data Passing
wouter-vue implements a unique feature for passing reactive data through the routing tree. This allows parent routes or layout wrappers to provide context that is automatically merged and made available to child components via useRouteData.
This data flow ensures that properties like themes, layouts, or page-specific metadata can be managed at the routing level and accessed deeply within the component tree without prop-drilling.
Sources: examples/example/src/pages/AboutPage.vue:8-30, README.md:65-72
Build-Time Optimization (Vite Plugin)
To maintain a small bundle size while supporting a large number of routes (e.g., hundreds of static pages), the project includes a specialized Vite plugin. The vitePluginRoutes automates the generation of route configurations, avoiding large manual objects and enabling efficient code splitting.
Route Generation Logic
- File Scanning: The plugin scans a directory (default
src/pages/routes) for files matching a pattern. - Template Extraction: It identifies numeric or lexicographical patterns to compress route definitions.
- Static Generation: It writes a generated file (e.g.,
generated-routes.js) containing static imports or dynamicimport()calls for Rollup's manual chunks.
The plugin can also compress routes using templates to reduce the size of the route manifest itself when many repetitive paths are present.
Sources: src/vite-plugin-routes.ts:85-150, src/vite-plugin-routes.ts:210-250
Server-Side Rendering (SSR) Support
wouter-vue provides a dedicated SSR module to handle routing in non-browser environments. The architecture abstracts location providers, allowing the server to inject a static path into the routing context.
parseSsrPath: A utility to normalize paths during server rendering.- Environment Detection: Uses
isBrowserandisSSRhelpers to conditionally attach event listeners (likepopstate) only when in a client context. - Path Normalization: Ensures that paths used for matching are consistent between the server-rendered HTML and the client-side hydration.
Sources: types/helpers.d.ts:13, package.json:80-85, examples/example/src/pages/AboutPage.vue:117-130
Internal Helper Architecture
The library organizes its internal logic into specialized helper modules to maintain backward compatibility and code clarity.
| Module | Responsibility |
|---|---|
| Path Helpers | Normalizing paths, resolving target URLs for Links, and validating props. |
| Vue Helpers | Resolving slots with parameters and unwrapping reactive values. |
| SSR Helpers | Environment detection and server-side path parsing. |
| Router Helpers | Internal state access and router value management. |
Sources: types/helpers.d.ts:1-15
Conclusion
The internal architecture of wouter-vue is characterized by its reliance on the Vue 3 Composition API to provide a lightweight yet powerful routing solution. By combining a decentralized component-based approach with sophisticated build-time optimizations (Vite plugin) and hierarchical data passing, it offers a high-performance alternative to more complex routers. The separation of location strategies and environment-aware helpers ensures stability across both client-side and server-side rendering scenarios.