Skip to content

Commit

Permalink
Handle edge case of URL hashes in query param script (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymo111 authored Nov 27, 2024
1 parent 96f8637 commit d9962b1
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,22 +360,18 @@ pub static TEMPLATE_QUERY_PARAM_SCRIPT: &str = r#"
if (queryParams.size === 0) return url; // No query params, return original URL
function appendQueryParams(url) {
const parsedUrl = url;
const noOrigParams = parsedUrl.lastIndexOf('?') == -1;
const newSearchParams = new URLSearchParams();
const newURL = new URL((new Request(url)).url); // new URL(<relative URL>) but it actually works
const newSearchParams = new URLSearchParams(newURL.searchParams);
console.log(newURL.searchParams);
console.log(newSearchParams);
// Append query parameters
for (const [key, value] of queryParams) {
newSearchParams.set(key, value);
}
let updatedURL = url;
if (noOrigParams) { // Original URL has no params
return parsedUrl + "?" + newSearchParams.toString();
}
// Original URL has params - append to end
return parsedUrl + "&" + newSearchParams.toString();
newURL.search = newSearchParams;
return newURL;
}
// Select all relative links on the page
Expand All @@ -387,4 +383,4 @@ pub static TEMPLATE_QUERY_PARAM_SCRIPT: &str = r#"
});
});
</script>
"#;
"#;

0 comments on commit d9962b1

Please sign in to comment.