From 65d409409545da2e18822f466efc6259a323ac21 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sun, 21 Nov 2021 17:47:18 -0800 Subject: [PATCH] Added safeDecodeURI method. Closes #848. --- components/metrics/PagesTable.js | 3 ++- components/metrics/ReferrersTable.js | 5 +++-- lib/url.js | 9 +++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/components/metrics/PagesTable.js b/components/metrics/PagesTable.js index c137589f..b500e270 100644 --- a/components/metrics/PagesTable.js +++ b/components/metrics/PagesTable.js @@ -4,6 +4,7 @@ import classNames from 'classnames'; import Link from 'next/link'; import FilterButtons from 'components/common/FilterButtons'; import { urlFilter } from 'lib/filters'; +import { safeDecodeURI } from 'lib/url'; import usePageQuery from 'hooks/usePageQuery'; import MetricsTable from './MetricsTable'; import styles from './PagesTable.module.css'; @@ -35,7 +36,7 @@ export default function PagesTable({ websiteId, websiteDomain, showFilters, ...p [styles.active]: x === url, })} > - {decodeURI(x)} + {safeDecodeURI(x)} ); diff --git a/components/metrics/ReferrersTable.js b/components/metrics/ReferrersTable.js index cbd4c9ba..4dad8655 100644 --- a/components/metrics/ReferrersTable.js +++ b/components/metrics/ReferrersTable.js @@ -3,6 +3,7 @@ import { FormattedMessage } from 'react-intl'; import MetricsTable from './MetricsTable'; import FilterButtons from 'components/common/FilterButtons'; import { refFilter } from 'lib/filters'; +import { safeDecodeURI } from 'lib/url'; export const FILTER_DOMAIN_ONLY = 0; export const FILTER_COMBINED = 1; @@ -26,10 +27,10 @@ export default function ReferrersTable({ websiteId, websiteDomain, showFilters, const renderLink = ({ w: href, x: url }) => { return (href || url).startsWith('http') ? ( - {decodeURI(url)} + {safeDecodeURI(url)} ) : ( - decodeURI(url) + safeDecodeURI(url) ); }; diff --git a/lib/url.js b/lib/url.js index deec7fe1..9bd8a3ab 100644 --- a/lib/url.js +++ b/lib/url.js @@ -32,3 +32,12 @@ export function getQueryString(params = {}) { export function makeUrl(url, params) { return `${url}${getQueryString(params)}`; } + +export function safeDecodeURI(s) { + try { + return decodeURI(s); + } catch (e) { + console.error(e); + } + return s; +}