Skip to content

Commit e674640

Browse files
claude[bot]penalosa
andcommitted
Fix cleanUrl function to properly handle subpath imports with query/hash
- Preserve # prefix and path for subpath imports - Remove query parameters and hash fragments after the path - Handle edge cases: #path?query, #path#hash, #path?query#hash Co-authored-by: Somhairle MacLeòid <penalosa@users.noreply.github.com>
1 parent f79b608 commit e674640

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

  • packages/vite-plugin-cloudflare/src

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ export function getOutputDirectory(
4545

4646
const postfixRE = /[?#].*$/;
4747
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+
}
4866
return url.replace(postfixRE, "");
4967
}
5068

0 commit comments

Comments
 (0)