sindresorhus / normalize-url

Normalize a URL
MIT License
840 stars 122 forks source link

Not working properly on Cloudflare Workers #143

Closed fnmendez closed 3 years ago

fnmendez commented 3 years ago

input: https://flyyer.io/render?hey=123

config:

{
    defaultProtocol: "https:",
    normalizeProtocol: true,
    stripHash: true,
    stripWWW: false,
    stripAuthentication: true,
    removeQueryParameters: [/^utm_\w+/i],
    removeTrailingSlash: false,
    sortQueryParameters: true,
  }

output : https://@flyyer.io/render?hey=123# expected: https://flyyer.io/render?hey=123

The URL object on Cloudflare Workers is different. It seems that only the href methods behaves differently.

fnmendez commented 3 years ago

BetterURL along with normalize-url do all the work nicely.

import { BetterURL } from "@flyyer/better-url";
import normalizeUrl from "normalize-url"; // normalizeUrl is failing because URL class is different on Cloudflare workers

export function NORMALIZE_URL(link: string): string {
  const betterURL = new BetterURL(
    normalizeUrl(link, {
      defaultProtocol: "https:",
      normalizeProtocol: true,
      stripHash: true,
      stripWWW: false,
      stripAuthentication: true,
      removeQueryParameters: [/^utm_\w+/i],
      removeTrailingSlash: false,
      // removeSingleSlash: true,
      sortQueryParameters: true,
    }),
  );
  return betterURL.format({ protocol: true, hostname: true, pathname: true, search: true });
}
sindresorhus commented 3 years ago

You'll need to open an issue on Cloudflare Workers instead. This package expects a compliant URL object.