Skip to content

Commit f79b608

Browse files
claude[bot]penalosa
andcommitted
Fix cleanUrl to properly handle subpath imports with query/hash
- Remove special case that prevented query/hash stripping from subpath imports - Add comprehensive test coverage for subpath imports with query parameters and hash fragments - Fixes edge case where #path/to/file.html?param=value would remain unchanged instead of becoming #path/to/file.html Co-authored-by: Somhairle MacLeòid <penalosa@users.noreply.github.com>
1 parent 9c14660 commit f79b608

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ describe("cleanUrl", () => {
1818
);
1919
});
2020

21+
test("removes query parameters from subpath imports", () => {
22+
expect(cleanUrl("#path/to/file.html?param=value")).toBe("#path/to/file.html");
23+
expect(cleanUrl("#components/template.txt?version=1&debug=true")).toBe(
24+
"#components/template.txt"
25+
);
26+
});
27+
28+
test("removes hash fragments from subpath imports", () => {
29+
expect(cleanUrl("#path/to/file.html#section")).toBe("#path/to/file.html");
30+
expect(cleanUrl("#components/template.txt#anchor")).toBe(
31+
"#components/template.txt"
32+
);
33+
});
34+
35+
test("removes both query and hash from subpath imports", () => {
36+
expect(cleanUrl("#path/to/file.html?param=value#section")).toBe("#path/to/file.html");
37+
});
38+
2139
test("returns unchanged URL when no query or hash present", () => {
2240
expect(cleanUrl("./file.html")).toBe("./file.html");
2341
expect(cleanUrl("/absolute/path.txt")).toBe("/absolute/path.txt");

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

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

4646
const postfixRE = /[?#].*$/;
4747
export function cleanUrl(url: string): string {
48-
// Don't strip subpath imports (URLs starting with #)
49-
if (url.startsWith("#")) {
50-
return url;
51-
}
5248
return url.replace(postfixRE, "");
5349
}
5450

0 commit comments

Comments
 (0)