Skip to content

Function: extractRouteParams()

API / @xmachines/play-router / extractRouteParams

function extractRouteParams(pathname, pattern): Record<string, string>;

Defined in: router-sync.ts:101

Extract named path parameters from a URL using the URLPattern API.

Takes the pattern string directly — use this when the pattern is already known. For extraction via a state ID lookup, see RouterBridgeBase.extractParams.

Undefined values (unmatched optional segments, e.g. /settings against /settings/:section?) are omitted from the returned object.

Parameters

ParameterTypeDescription
pathnamestringThe concrete URL pathname (e.g. /profile/alice)
patternstringThe URL pattern template (e.g. /profile/:username)

Returns

Record<string, string>

A record of extracted parameter values, or {} for static patterns.

Throws

When URLPattern is absent and the pattern is parameterized.

Example

import { extractRouteParams } from "@xmachines/play-router";
extractRouteParams("/profile/alice", "/profile/:username");
// → { username: "alice" }
extractRouteParams("/settings", "/settings/:section?");
// → {} (optional param absent — not included)