UNPKG

next

Version:

The React Framework

113 lines (112 loc) 4.4 kB
/** * Client-safe utilities for route matching that don't import server-side * utilities to avoid bundling issues with Turbopack */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 0 && (module.exports = { safeCompile: null, safePathToRegexp: null, safeRegexpToFunction: null, safeRouteMatcher: null }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { safeCompile: function() { return safeCompile; }, safePathToRegexp: function() { return safePathToRegexp; }, safeRegexpToFunction: function() { return safeRegexpToFunction; }, safeRouteMatcher: function() { return safeRouteMatcher; } }); const _pathtoregexp = require("next/dist/compiled/path-to-regexp"); const _routepatternnormalizer = require("../../../../lib/route-pattern-normalizer"); function safePathToRegexp(route, keys, options) { if (typeof route !== 'string') { return (0, _pathtoregexp.pathToRegexp)(route, keys, options); } // Check if normalization is needed and cache the result const needsNormalization = (0, _routepatternnormalizer.hasAdjacentParameterIssues)(route); const routeToUse = needsNormalization ? (0, _routepatternnormalizer.normalizeAdjacentParameters)(route) : route; try { return (0, _pathtoregexp.pathToRegexp)(routeToUse, keys, options); } catch (error) { // Only try normalization if we haven't already normalized if (!needsNormalization) { try { const normalizedRoute = (0, _routepatternnormalizer.normalizeAdjacentParameters)(route); return (0, _pathtoregexp.pathToRegexp)(normalizedRoute, keys, options); } catch (retryError) { // If that doesn't work, fall back to original error throw error; } } throw error; } } function safeCompile(route, options) { // Check if normalization is needed and cache the result const needsNormalization = (0, _routepatternnormalizer.hasAdjacentParameterIssues)(route); const routeToUse = needsNormalization ? (0, _routepatternnormalizer.normalizeAdjacentParameters)(route) : route; try { const compiler = (0, _pathtoregexp.compile)(routeToUse, options); // If we normalized the route, wrap the compiler to strip separators from output // The normalization inserts _NEXTSEP_ as a literal string in the pattern to satisfy // path-to-regexp validation, but we don't want it in the final compiled URL if (needsNormalization) { return (params)=>{ return (0, _routepatternnormalizer.stripNormalizedSeparators)(compiler(params)); }; } return compiler; } catch (error) { // Only try normalization if we haven't already normalized if (!needsNormalization) { try { const normalizedRoute = (0, _routepatternnormalizer.normalizeAdjacentParameters)(route); const compiler = (0, _pathtoregexp.compile)(normalizedRoute, options); // Wrap the compiler to strip separators from output return (params)=>{ return (0, _routepatternnormalizer.stripNormalizedSeparators)(compiler(params)); }; } catch (retryError) { // If that doesn't work, fall back to original error throw error; } } throw error; } } function safeRegexpToFunction(regexp, keys) { const originalMatcher = (0, _pathtoregexp.regexpToFunction)(regexp, keys || []); return (pathname)=>{ const result = originalMatcher(pathname); if (!result) return false; // Clean parameters before returning return { ...result, params: (0, _routepatternnormalizer.stripParameterSeparators)(result.params) }; }; } function safeRouteMatcher(matcherFn) { return (pathname)=>{ const result = matcherFn(pathname); if (!result) return false; // Clean parameters before returning return (0, _routepatternnormalizer.stripParameterSeparators)(result); }; } //# sourceMappingURL=route-match-utils.js.map