Skip to content

Commit f43ad7e

Browse files
claude[bot]penalosa
andcommitted
Simplify cleanUrl function using elegant regex solution
Replace complex 20-line implementation with a 3-line regex-based solution. The new regex (?<!^#[^?#]*)[?#].*$ uses negative lookbehind to preserve the leading # in subpath imports while still removing query parameters and hash fragments. Co-authored-by: Somhairle MacLeòid <penalosa@users.noreply.github.com>
1 parent e674640 commit f43ad7e

1 file changed

Lines changed: 2 additions & 19 deletions

File tree

  • packages/vite-plugin-cloudflare/src

packages/vite-plugin-cloudflare/src/utils.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,9 @@ export function getOutputDirectory(
4343
);
4444
}
4545

46-
const postfixRE = /[?#].*$/;
46+
// Removes query parameters (?...) and hash fragments (#...), but preserves the leading # in subpath imports
47+
const postfixRE = /(?<!^#[^?#]*)[?#].*$/;
4748
export function cleanUrl(url: string): string {
48-
if (url.startsWith("#")) {
49-
// For subpath imports, find the first query param (?) or hash fragment after the subpath
50-
// and remove everything from there
51-
const queryIndex = url.indexOf("?");
52-
const hashIndex = url.indexOf("#", 1); // Start search from index 1 to skip the leading #
53-
54-
if (queryIndex === -1 && hashIndex === -1) {
55-
// No query params or hash fragments to remove
56-
return url;
57-
}
58-
59-
// Find the first occurrence of either ? or # (after position 0)
60-
const cutIndex = queryIndex !== -1 && (hashIndex === -1 || queryIndex < hashIndex)
61-
? queryIndex
62-
: hashIndex;
63-
64-
return url.substring(0, cutIndex);
65-
}
6649
return url.replace(postfixRE, "");
6750
}
6851

0 commit comments

Comments
 (0)