Route Matching Strategies
You'll learn how routers decide which route handler matches the current URL.
When building client-side routers (using hash-based or History API patterns you've learned), you need a way to determine which route should handle the current URL. There are three main matching strategies, each with different trade-offs.
Exact Matches
An exact match requires the entire path to match perfectly. If your route is /about, only /about triggers it—not /about/team or /about/.
Think of this like a precise address: "123 Main Street" won't match "123 Main Street, Apartment 5."
// Pseudo-code pattern
if (currentPath === '/about') {
showAboutPage();
}
Use when: You want strict control and no ambiguity.
Prefix Matches
A prefix match triggers if the URL starts with the route pattern. If your route is /docs, it matches /docs, /docs/api, /docs/getting-started, etc.
This is like mail sorting: anything starting with "123 Main Street" goes to that building, regardless of apartment number.
Use when: You're building nested navigation or section-based layouts where child routes share a common parent.
Pattern-Based Matches
Pattern-based matching uses wildcards or placeholders. A route like /user/:id matches /user/42, /user/alice, capturing the dynamic part.
Think of this as a template: "User _____" where the blank can be filled with any value.
// Conceptual example
// Route: /product/:productId
// URL: /product/laptop-pro
// Extracted: { productId: 'laptop-pro' }
Use when: You have dynamic content where IDs or slugs appear in URLs.
Key Takeaway: Choose exact matches for precision, prefix matches for hierarchical navigation, and pattern-based matches for dynamic URLs—often you'll combine all three strategies in a single router.