Merge pull request #1402 from guangyang1206/fix/extract-domain-helper-1368

Fix/extract domain helper 1368
This commit is contained in:
Rohan Verma 2026-05-17 18:17:25 -07:00 committed by GitHub
commit 8fc4b98593
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 24 additions and 38 deletions

14
surfsense_web/lib/url.ts Normal file
View file

@ -0,0 +1,14 @@
/**
* Extract a normalized hostname from a URL. Strips a leading `www.`.
* Returns `undefined` if the input is not a parseable URL.
*
* This is the canonical replacement for the four previously-duplicated
* `extractDomain` helpers that had subtly different error fallbacks.
*/
export function tryGetHostname(url: string): string | undefined {
try {
return new URL(url).hostname.replace(/^www\./, "");
} catch {
return undefined;
}
}