{"record":{"id":"79ef97768828b076","repo":"remix-run/remix","slug":"cannot-compare-matches-for-different-urls-a-url","errorCode":null,"errorMessage":"Cannot compare matches for different URLs: ${a.url.href} vs ${b.url.href}","messagePattern":"Cannot compare matches for different URLs: (.+?) vs (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/route-pattern/src/lib/specificity.ts","lineNumber":66,"sourceCode":" * Comparator function for sorting matches from most specific to least specific.\n *\n * @param a the first match to compare\n * @param b the second match to compare\n * @returns positive if `a` is less specific, negative if more specific, 0 if equal\n */\nexport const descending = (a: Match, b: Match): number => compare(a, b) * -1\n\n/**\n * Compare two matches by specificity.\n * Passing to `.sort()` will sort matches from least specific to most specific.\n *\n * @param a the first match to compare\n * @param b the second match to compare\n * @returns -1 if `a` is less specific, 1 if `a` is more specific, 0 if tied.\n */\nexport function compare(a: Match, b: Match): -1 | 0 | 1 {\n  if (a.url.href !== b.url.href) {\n    throw new Error(`Cannot compare matches for different URLs: ${a.url.href} vs ${b.url.href}`)\n  }\n  let aParts = a.pattern._parts\n  let bParts = b.pattern._parts\n\n  let protocolResult = compareProtocol(aParts.protocol, bParts.protocol)\n  if (protocolResult !== 0) return protocolResult\n\n  let portResult = comparePort(aParts.port, bParts.port)\n  if (portResult !== 0) return portResult\n\n  let hostname = decodeHostname(a.url.hostname)\n\n  let hostnameResult = compareHostname(hostname, a.paramsMeta.hostname, b.paramsMeta.hostname)\n  if (hostnameResult !== 0) return hostnameResult\n\n  let pathnameResult = comparePathname(a.paramsMeta.pathname, b.paramsMeta.pathname)\n  if (pathnameResult !== 0) return pathnameResult\n","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/remix-run/remix/blob/9696913134be3a4423513d2775f7b31d6917c049/packages/route-pattern/src/lib/specificity.ts#L48-L84","documentation":"The specificity compare() function throws when asked to compare two Match objects that were produced against different URLs. Specificity comparison is only meaningful for matches on the same URL because it compares which pattern matches that URL more specifically.","triggerScenarios":"Calling compare(a, b) (or helpers like lessThan, greaterThan, equal, assertCompare, or sorting with ascending/descending) where a.url.href !== b.url.href, e.g. matches from two different requests or two different test URLs.","commonSituations":"Sorting an array of matches collected across multiple requests; mixing matches from tests fixtures with different URLs; caching matches and comparing stale ones against new ones.","solutions":["Only compare matches produced from the same URL","Re-match the patterns against a single URL before comparing","Group matches by URL before sorting"],"exampleFix":"// before\ncompare(matchA, matchB) // different URLs\n// after\nlet matches = patterns.map((p) => p.match(url)).filter((m) => m !== null)\nsort(ascending)(matches)","handlingStrategy":"validation","validationCode":"function sameUrl(a: Match, b: Match): boolean {\n  return a.url.href === b.url.href\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Group matches by URL before sorting","Always re-match patterns against a single URL right before comparing"],"tags":["route-pattern","specificity","compare"],"backgroundTag":"invalid-argument-state","analyzedSha":"9696913134be3a4423513d2775f7b31d6917c049","analyzedAt":"2026-08-27T19:55:01.024Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}