Redirect Client SDK
The Redirect Client (RedirectClient from @uniformdev/redirect) provides programmatic access to Uniform's redirect management system. Use it to resolve redirects for incoming URLs, manage redirect rules, export redirects to external CDN formats, and build redirect resolution into custom middleware or edge functions.
Installation#
Initializing the client#
Resolving redirects#
Best match#
processUrlBestMatch finds the single best redirect match for a URL. This is the most common use case for redirect resolution in middleware:
Using without trie (API-based lookup)#
By default, processUrlBestMatch queries the API directly. This is suitable for low-traffic scenarios or when you don't need the full redirect set in memory:
Using with trie (in-memory lookup)#
For high-traffic scenarios, pass true for useTrie to load all redirects into an in-memory trie structure for instant resolution:
warning
The trie approach loads all redirects into memory. This is fast but may use significant memory for large redirect sets (thousands of rules).
All matches#
processUrlAllMatches returns all redirect rules that match a URL, not just the best one:
Redirect result object#
Both processUrlBestMatch and processUrlAllMatches return RedirectResult objects:
| Property | Type | Description |
|---|---|---|
url | string | The resolved target URL |
definition | RedirectDefinition | The full redirect rule definition |
label | string | (Optional) URL with wildcard segments highlighted in <em> tags |
lastHop | RedirectResult | (Optional) Previous redirect in a chain |
Redirect definition#
Each redirect definition contains:
| Property | Type | Description |
|---|---|---|
redirect.sourceUrl | string | Source URL pattern |
redirect.targetUrl | string | Target URL |
redirect.targetStatusCode | number | HTTP status code (301, 302, etc.) |
redirect.sourceMustMatchDomain | boolean | Whether to enforce domain matching |
redirect.sourceRetainQuerystring | boolean | Whether to retain the source query string |
redirect.targetMergeQuerystring | boolean | Whether to merge query strings |
redirect.targetPreserveIncomingDomain | boolean | Whether to preserve the incoming domain |
redirect.targetPreserveIncomingProtocol | boolean | Whether to preserve the incoming protocol |
metadata.created | string | Creation timestamp |
metadata.modified | string | Last modified timestamp |
Redirect options#
Both processUrlBestMatch and processUrlAllMatches accept an options object:
| Option | Type | Description |
|---|---|---|
reverse | boolean | When true, finds the source URL that could have produced a given target URL |
label | boolean | When true, returns a label property with wildcard segments highlighted in <em> tags |
Reverse lookup#
Useful for finding what source URL redirects to a given target:
Managing redirects#
Get a redirect by ID#
Get redirects with search and filtering#
Search parameters#
| Parameter | Type | Description |
|---|---|---|
sourceUrl | string | Filter by source URL |
targetUrl | string | Filter by target URL |
search | string | Free-text search across URLs |
ids | string | Comma-separated list of IDs |
limit | number | Max results per page |
offset | number | Pagination offset |
orderBy | string | Sort order (e.g., "updated_at desc") |
Iterate all redirects#
For large redirect sets, use the async generator getAllRedirects which pages through results automatically:
Create or update a redirect#
Delete a redirect#
Wildcard redirects#
Uniform redirects support wildcard segments using the :paramName syntax in source URLs. The matched segments are automatically substituted into the target URL:
| Source | Target | Example |
|---|---|---|
/blog/:slug | /articles/:slug | /blog/hello -> /articles/hello |
/docs/:version/:page | /documentation/:version/:page | /docs/v2/intro -> /documentation/v2/intro |
Redirect chain resolution#
The client automatically follows redirect chains (where one redirect's target is another redirect's source) with built-in cycle detection. The lastHop property on the result provides the previous redirect in the chain:
Caching with WithMemoryCache#
For high-performance scenarios, use the WithMemoryCache cache to keep redirect data in process memory with automatic background refresh:
Cache options#
| Option | Type | Description |
|---|---|---|
prePopulate | boolean | Immediately load redirect data into cache on client creation |
refreshRate | number | Interval in milliseconds between automatic cache refreshes. The refresher pauses after 5 idle cycles with no cache reads. |
Manual cache refresh#
Exporting redirects for external CDNs#
Use RedirectFileConverter to export Uniform redirects into a format consumable by external CDN redirect systems (e.g., Vercel vercel.json, Netlify _redirects, Cloudflare):
UncachedRedirectClient#
For scenarios requiring bypassing the API cache (e.g., admin tools, testing):
URL validation#
The RedirectClient.validateRedirect static method checks whether a given URL matches a redirect definition, including domain and query string validation:
Import reference#
| Export | Package | Description |
|---|---|---|
RedirectClient | @uniformdev/redirect | Main redirect client |
UncachedRedirectClient | @uniformdev/redirect | Cache-bypassing redirect client |
WithMemoryCache | @uniformdev/redirect | In-memory cache with auto-refresh |
RedirectFileConverter | @uniformdev/redirect | Export redirects for external CDN formats |
ExtractWildcards | @uniformdev/redirect | Extract wildcard segments from URLs |
PathTrie | @uniformdev/redirect | Trie data structure for fast path matching |