Skip to content

Commit

Permalink
refactor: toPunycodeURL uses href in logic
Browse files Browse the repository at this point in the history
  • Loading branch information
digiwand committed Jan 8, 2025
1 parent 3e2aac9 commit 4c48863
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions app/util/url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ function removePathTrailingSlash(path: string) {
export const toPunycodeURL = (urlString: string) => {
try {
const url = new URL(urlString);
const punycodeHostname = punycode.toASCII(url.hostname);
const { protocol, port, search, hash } = url;
const pathname =
url.pathname === '/' && !urlString.endsWith('/') ? '' : url.pathname;
const punycodeUrl = punycode.toASCII(url.href);
const isWithoutEndSlash = url.pathname === '/' && !urlString.endsWith('/');

return `${protocol}//${punycodeHostname}${port}${pathname}${search}${hash}`;
return isWithoutEndSlash ? punycodeUrl.slice(0, -1) : punycodeUrl;
} catch (err: unknown) {
console.error(`Failed to convert URL to Punycode: ${err}`);
return urlString;
Expand Down

0 comments on commit 4c48863

Please sign in to comment.