fix(frontend): encode special characters in search input to prevent crashing router

re #252
pull/240/head
sct 4 years ago
parent 8cb05c413a
commit 15013d6c5d

@ -7,6 +7,10 @@ import type { Nullable } from '../utils/typeHelpers';
type Url = string | UrlObject; type Url = string | UrlObject;
const encodeURIExtraParams = (string: string): string => {
return encodeURIComponent(string).replace(/!/g, '%21');
};
interface SearchObject { interface SearchObject {
searchValue: string; searchValue: string;
searchOpen: boolean; searchOpen: boolean;
@ -35,14 +39,17 @@ const useSearchInput = (): SearchObject => {
if (router.pathname.startsWith('/search')) { if (router.pathname.startsWith('/search')) {
router.replace({ router.replace({
pathname: router.pathname, pathname: router.pathname,
query: { ...router.query, query: debouncedValue }, query: {
...router.query,
query: encodeURIExtraParams(debouncedValue),
},
}); });
} else { } else {
setLastRoute(router.asPath); setLastRoute(router.asPath);
router router
.push({ .push({
pathname: '/search', pathname: '/search',
query: { query: debouncedValue }, query: { query: encodeURIExtraParams(debouncedValue) },
}) })
.then(() => window.scrollTo(0, 0)); .then(() => window.scrollTo(0, 0));
} }
@ -85,8 +92,12 @@ const useSearchInput = (): SearchObject => {
* is on /search * is on /search
*/ */
useEffect(() => { useEffect(() => {
if (router.query.query !== debouncedValue) { if (router.query.query !== encodeURIExtraParams(debouncedValue)) {
setSearchValue((router.query.query as string) ?? ''); setSearchValue(
router.query.query
? decodeURIComponent(router.query.query as string)
: ''
);
if (!router.pathname.startsWith('/search') && !router.query.query) { if (!router.pathname.startsWith('/search') && !router.query.query) {
setIsOpen(false); setIsOpen(false);

Loading…
Cancel
Save