From 1893c9b8daacf06fa2822d428c885baac4e9c064 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 23 Feb 2024 20:16:11 -0800 Subject: [PATCH] Fix: Google search suggestions with accented characters (#2993) --- src/pages/api/search/searchSuggestion.js | 2 +- src/utils/proxy/cached-fetch.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/pages/api/search/searchSuggestion.js b/src/pages/api/search/searchSuggestion.js index c1c936c9d..fa8eba0d8 100644 --- a/src/pages/api/search/searchSuggestion.js +++ b/src/pages/api/search/searchSuggestion.js @@ -19,5 +19,5 @@ export default async function handler(req, res) { return res.json([query, []]); // Responde with the same array format but with no suggestions. } - return res.send(await cachedFetch(`${provider.suggestionUrl}${encodeURIComponent(query)}`, 5)); + return res.send(await cachedFetch(`${provider.suggestionUrl}${encodeURIComponent(query)}`, 5, "Mozilla/5.0")); } diff --git a/src/utils/proxy/cached-fetch.js b/src/utils/proxy/cached-fetch.js index 30b00f77e..ae3c46108 100644 --- a/src/utils/proxy/cached-fetch.js +++ b/src/utils/proxy/cached-fetch.js @@ -2,7 +2,7 @@ import cache from "memory-cache"; const defaultDuration = 5; -export default async function cachedFetch(url, duration) { +export default async function cachedFetch(url, duration, ua) { const cached = cache.get(url); // eslint-disable-next-line no-param-reassign @@ -13,7 +13,13 @@ export default async function cachedFetch(url, duration) { } // wrapping text in JSON.parse to handle utf-8 issues - const data = JSON.parse(await fetch(url).then((res) => res.text())); + const options = {}; + if (ua) { + options.headers = { + "User-Agent": ua, + }; + } + const data = await fetch(url, options).then((res) => res.json()); cache.put(url, data, duration * 1000 * 60); return data; }