@@W-20607771@@ Add fuzzyPathMatching option to optimize route configuration - #3530
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
| .filter(Boolean) | ||
|
|
||
| // Remove duplicates and join into regex pattern | ||
| const sitePattern = [...new Set(siteRefs)].join('|') |
There was a problem hiding this comment.
nit, could siteRefs contain regex sensitive characters like (., +, *, (, etc.) and cause regex matching issues? like site.uk, should we sanitize and escape those chars?
There was a problem hiding this comment.
I don't think Site ID can have special characters but site name can have
There was a problem hiding this comment.
I think that comment applies to the site id locale id since what is what those values are. I don't think we are currently validating those values in our code as of yet. Although I con't think there will be too many customer using + as a site id. It's possible, but a fringe case. This should be something we validate in Storefront Next tho.
| const sitePattern = 'uk|site-1|us|site-2' | ||
| const localePattern = 'en-GB|fr|fr-FR|it-IT|en-US|en-CA' |
There was a problem hiding this comment.
nit, what happens if there are invalid input like missing sitePattern or localePattern, does the code catch it and show warnings?
There was a problem hiding this comment.
As per my previous comment, these values are using the site and locale id's. If we want to, we can have a follow up to make sure that those values are validated, but that might be a breaking change ?
| return configureRoutes(routes, config, { | ||
| ignoredRoutes: ['/callback', '*'] | ||
| ignoredRoutes: ['/callback', '*'], | ||
| fuzzyPathMatching: true |
There was a problem hiding this comment.
Are we setting to true by default?
PR: Add
fuzzyPathMatchingoption to optimize route configuration🎯 Summary
This PR introduces an optional
fuzzyPathMatchingflag toconfigureRoutes()that dramatically reduces the number of generated routes by using parameterized paths with regex constraints instead of explicit route enumeration.🚨 The Problem
When configuring routes with multiple sites and locales, the current implementation generates explicit routes for every possible combination. This leads to exponential route growth:
This impacts:
✅ The Solution
Instead of generating explicit routes like:
We now support parameterized routes with regex constraints:
This reduces route count from O(sites × locales × routes) to O(routes × 4) maximum.
🔧 Changes Made
fuzzyPathMatchingoption — Opt-in flag in the options objectconfigureRoutes()— Split into two internal functions:configureRoutesWithExplicitMatching()— Original behaviorconfigureRoutesWithFuzzyMatching()— New optimized approachbuildRoutePatterns()— Builds regex patterns from site/locale refs📖 Usage
Important: With fuzzy matching enabled, invalid site/locale combinations (e.g., a locale not supported by a specific site) may match. Runtime validation should be performed after route matching:
🛡️ Not a Breaking Change
fuzzyPathMatchingdefaults tofalse📊 Performance Comparison
For a real-world config with 2 sites, 4 locales each, and 20 base routes:
✅ Testing
fuzzyPathMatchingnpm test -- --testPathPattern=routes-utils.test.js📝 Checklist