1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

Fix getLocationSearch when search and hash search exist

This commit is contained in:
Bill Thornton 2024-06-25 10:47:00 -04:00
parent 4ffa90cdd7
commit c89846c039
2 changed files with 68 additions and 2 deletions

60
src/utils/url.test.ts Normal file
View file

@ -0,0 +1,60 @@
import { describe, expect, it, vi } from 'vitest';
import { getLocationSearch } from './url';
const mockLocation = (urlString: string) => {
// eslint-disable-next-line compat/compat
const url = new URL(urlString);
vi.spyOn(window, 'location', 'get')
.mockReturnValue({
...window.location,
hash: url.hash,
host: url.host,
hostname: url.hostname,
href: url.href,
origin: url.origin,
pathname: url.pathname,
port: url.port,
protocol: url.protocol,
search: url.search
});
};
describe('getLocationSearch', () => {
it('Should work with standard url search', () => {
mockLocation('https://example.com/path?foo#bar');
expect(getLocationSearch()).toBe('?foo');
});
it('Should work with search in the url hash', () => {
mockLocation('https://example.com/path#bar?foo');
expect(getLocationSearch()).toBe('?foo');
});
it('Should work with search in the url hash and standard url search', () => {
mockLocation('https://example.com/path?baz#bar?foo');
expect(getLocationSearch()).toBe('?foo');
});
it('Should return an empty string if there is no search', () => {
mockLocation('https://example.com');
expect(getLocationSearch()).toBe('');
});
it('Should fallback to the href if there is no hash or search', () => {
vi.spyOn(window, 'location', 'get')
.mockReturnValue({
...window.location,
hash: '',
host: '',
hostname: '',
href: 'https://example.com/path#bar?foo',
origin: '',
pathname: '',
port: '',
protocol: '',
search: ''
});
expect(getLocationSearch()).toBe('?foo');
});
});

View file

@ -5,13 +5,19 @@
* @returns The url search string. * @returns The url search string.
*/ */
export const getLocationSearch = () => { export const getLocationSearch = () => {
// Check location.hash for a search string (this should be the case for our routing library)
let index = window.location.hash.indexOf('?');
if (index !== -1) {
return window.location.hash.substring(index);
}
// Return location.search if it exists // Return location.search if it exists
if (window.location.search) { if (window.location.search) {
return window.location.search; return window.location.search;
} }
// Check the entire url in case the search string is in the hash // Fallback to checking the entire url
const index = window.location.href.indexOf('?'); index = window.location.href.indexOf('?');
if (index !== -1) { if (index !== -1) {
return window.location.href.substring(index); return window.location.href.substring(index);
} }