From 9b884f19245fc205e2fb3bc3cb338334af5624ca Mon Sep 17 00:00:00 2001 From: Bill Thornton Date: Thu, 7 Apr 2022 16:06:26 -0400 Subject: [PATCH] Fix url search in hash --- src/components/appRouter.js | 3 ++- src/scripts/clientUtils.js | 3 ++- src/utils/url.ts | 29 ++++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/components/appRouter.js b/src/components/appRouter.js index ce10d6e93a..16248c4d80 100644 --- a/src/components/appRouter.js +++ b/src/components/appRouter.js @@ -12,6 +12,7 @@ import Dashboard from '../scripts/clientUtils'; import ServerConnections from './ServerConnections'; import alert from './alert'; import reactControllerFactory from './reactControllerFactory'; +import { getLocationSearch } from '../utils/url.ts'; class AppRouter { allRoutes = []; @@ -120,7 +121,7 @@ class AppRouter { const regexS = '[\\?&]' + name + '=([^&#]*)'; const regex = new RegExp(regexS, 'i'); - const results = regex.exec(url || window.location.search); + const results = regex.exec(url || getLocationSearch()); if (results == null) { return ''; } else { diff --git a/src/scripts/clientUtils.js b/src/scripts/clientUtils.js index 6d4e3be683..f36408d9af 100644 --- a/src/scripts/clientUtils.js +++ b/src/scripts/clientUtils.js @@ -10,6 +10,7 @@ import datetime from '../scripts/datetime'; import DirectoryBrowser from '../components/directorybrowser/directorybrowser'; import dialogHelper from '../components/dialogHelper/dialogHelper'; import itemIdentifier from '../components/itemidentifier/itemidentifier'; +import { getLocationSearch } from '../utils/url.ts'; export function getCurrentUser() { return window.ApiClient.getCurrentUser(false); @@ -112,7 +113,7 @@ export function navigate(url, preserveQueryString) { throw new Error('url cannot be null or empty'); } - const queryString = window.location.search; + const queryString = getLocationSearch(); if (preserveQueryString && queryString) { url += queryString; diff --git a/src/utils/url.ts b/src/utils/url.ts index 637f22fed8..1c65b2343a 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -1,6 +1,33 @@ +/** + * Gets the url search string. + * This function should be used instead of location.search alone, because the app router + * includes search parameters in the hash portion of the url. + * @returns The url search string. + */ +export const getLocationSearch = () => { + // Return location.search if it exists + if (window.location.search) { + return window.location.search; + } + + // Check the entire url in case the search string is in the hash + const index = window.location.href.indexOf('?'); + if (index !== -1) { + return window.location.href.substring(index); + } + + return ''; +}; + +/** + * Gets the value of a url search parameter by name. + * @param name The parameter name. + * @param url The url to search (optional). + * @returns The parameter value. + */ export const getParameterByName = (name: string, url?: string | null | undefined) => { if (!url) { - url = window.location.search; + url = getLocationSearch(); } // eslint-disable-next-line compat/compat