* Constructs a normalized and serialized URL string from its components. * * This function uses the provided `Router` instance to parse and serialize the URL, * ensuring that the resulting string is consistent with the router's configuration. * It also handles the optional `prefix` parameter to
(
router: Router,
url: { pathname: string; search: string; hash: string },
prefix?: string | null,
)
| 209 | * This is important for the URL to be correctly parsed and serialized by the router as it might have different encodings. |
| 210 | */ |
| 211 | function constructSerializedUrl( |
| 212 | router: Router, |
| 213 | url: { pathname: string; search: string; hash: string }, |
| 214 | prefix?: string | null, |
| 215 | ): string { |
| 216 | const { pathname, hash, search } = url; |
| 217 | const urlParts: string[] = []; |
| 218 | if (prefix && !addTrailingSlash(pathname).startsWith(addTrailingSlash(prefix))) { |
| 219 | urlParts.push(joinUrlParts(prefix, pathname)); |
| 220 | } else { |
| 221 | urlParts.push(stripTrailingSlash(pathname)); |
| 222 | } |
| 223 | |
| 224 | urlParts.push(search, hash); |
| 225 | |
| 226 | const urlTree = router.parseUrl(urlParts.join('')); |
| 227 | |
| 228 | return router.serializeUrl(urlTree); |
| 229 | } |
no test coverage detected