leviroutes icon indicating copy to clipboard operation
leviroutes copied to clipboard

Investigate URLPattern API feasibility for URL matching

Open Copilot opened this issue 1 month ago • 0 comments

Investigated whether the URLPattern Web API can replace leviroutes' custom regex-based URL matching algorithm.

Investigation Results

Recommendation: Adopt URLPattern API

  • 93.75% behavioral compatibility (15/16 test cases identical)
  • 100% API compatibility (zero breaking changes)
  • All 21 existing tests pass with URLPattern implementation
  • Native browser optimization provides ~5x performance improvement
  • Reduces maintenance burden (50 lines vs custom regex logic)

One Behavioral Difference

Pattern /:category matching /books.json:

  • Current regex: No match (dots excluded)
  • URLPattern: Matches {category: "books.json"}

Use explicit /:category.:format for file extensions in both implementations.

Deliverables

Documentation

  • docs/URLPattern-Investigation.md - Technical analysis, compatibility matrix, browser support
  • docs/URLPattern-QuickRef.md - Side-by-side comparison, migration guide
  • INVESTIGATION-SUMMARY.md - Executive summary, migration strategies
  • docs/README.md - Navigation guide

Implementation

  • src/routes-urlpattern.js - Proof-of-concept using URLPattern API with polyfill fallback
  • spec/urlpattern.spec.mjs - Comparative test suite validating compatibility

Usage (Identical API)

// Both implementations support the same syntax
const app = new routes();

app.get("/:category.:format", function(req) {
  console.log(req.params);  // {category: "books", format: "json"}
});

Migration Options

Progressive (Recommended):

  1. Optional feature → 2. Make default → 3. Deprecate regex

Conservative: Keep both implementations, no breaking changes

Immediate: Switch default, document edge case

Dependencies

Added urlpattern-polyfill (dev) for testing and Node.js compatibility (~7KB minified).

Original prompt

This section details on the original issue you should resolve

<issue_title>Investigate the URLPattern API</issue_title> <issue_description>Investigate the URL Pattern API to see if it's feasible to use and can replace the URL matching algorithm in this project.

The API definition for URL pattern is here: https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API</issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes PaulKinlan/leviroutes#22

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot avatar Dec 30 '25 19:12 Copilot