From 7674dfef07f02dc1cb52ea41d1ea60e5fca7ba4e Mon Sep 17 00:00:00 2001 From: Valerij Fredriksen Date: Mon, 21 Oct 2024 11:07:35 +0200 Subject: [PATCH] Encode spaces as %20 --- js/search.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/js/search.js b/js/search.js index a812a30580..b355328589 100644 --- a/js/search.js +++ b/js/search.js @@ -26,6 +26,12 @@ const debounce = (func, timeout = 200) => { }; }; +const encode = (params) => { + return new URLSearchParams(params) + .toString() + .replaceAll('+', '%20'); // CloudFront requires that spaces are encoded as %20 +}; + const handleQuery = (query) => { if (query.length > 0) { @@ -33,8 +39,7 @@ const handleQuery = (query) => { document.getElementById("hits").innerHTML = ""; result.innerHTML = `Searching for '${escapeHtml(query)}' ...`; - const searchParams = new URLSearchParams({term: query}); - fetch("https://api.search.vespa.ai/search/?" + searchParams.toString()) + fetch("https://api.search.vespa.ai/search/?" + encode({term: query})) .then((res) => res.json()) .then((res) => { const children = (res.root.children)? res.root.children : []; handleSuggestionResults(children.filter(child => child.fields.sddocname === "term")); @@ -55,15 +60,15 @@ const handleLocationQuery = () => { document.getElementById("searchinput").value = query; result.innerHTML = `Searching for '${escapeHtml(query)}' ...`; - const searchParams = new URLSearchParams({ + const searchParams = { yql: 'select * from doc where {grammar: "weakAnd"}userInput(@userinput)', hits: 25, ranking: 'documentation', locale: 'en-US', userinput: query, - }); + }; - fetch("https://api.search.vespa.ai/search/?" + searchParams.toString()) + fetch("https://api.search.vespa.ai/search/?" + encode(searchParams)) .then((res) => res.json()) .then((res) => handleResults(res.root.children, escapeHtml(query))) }