Skip to content

Commit

Permalink
fix(clerk-js): Ensure #/ prefix for hashes
Browse files Browse the repository at this point in the history
Ensure the #/ prefix in hashes when setting or deleting a hash query parameter so that ClerkJS Hash router works as expected.
  • Loading branch information
SokratisVidros committed Nov 25, 2022
1 parent 6a788b7 commit 0243403
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
19 changes: 11 additions & 8 deletions packages/clerk-js/src/utils/__tests__/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,13 @@ describe('getSearchParameterFromHash(options)', () => {

describe('setSearchParameterInHash(options)', () => {
const testCases: Array<[string, string, string, string]> = [
['#a-hash', 'foo', '42', 'a-hash?foo=42'],
['a-hash', 'foo', '', 'a-hash?foo='],
['#a-hash?foo=42', 'bar', '84', 'a-hash?foo=42&bar=84'],
['#a-hash?foo=42', 'foo', '84', 'a-hash?foo=84'],
['', 'foo', '42', '/?foo=42'],
['#', 'foo', '42', '/?foo=42'],
['#a-hash', 'foo', '42', '/a-hash?foo=42'],
['a-hash', 'foo', '', '/a-hash?foo='],
['#a-hash?foo=42', 'bar', '84', '/a-hash?foo=42&bar=84'],
['#a-hash?foo=42', 'foo', '84', '/a-hash?foo=84'],
['#/a-hash?foo=42', 'foo', '84', '/a-hash?foo=84'],
];

test.each(testCases)(
Expand All @@ -332,10 +335,10 @@ describe('setSearchParameterInHash(options)', () => {

describe('removeSearchParameterFromHash(options)', () => {
const testCases: Array<[string, string, string | null]> = [
['#random-hash', 'foo', 'random-hash'],
['random-hash', 'foo', 'random-hash'],
['#random-hash?foo=42', 'foo', 'random-hash'],
['random-hash?foo=42&bar=84', 'bar', 'random-hash?foo=42'],
['#random-hash', 'foo', '/random-hash'],
['random-hash', 'foo', '/random-hash'],
['#random-hash?foo=42', 'foo', '/random-hash'],
['random-hash?foo=42&bar=84', 'bar', '/random-hash?foo=42'],
];

test.each(testCases)('hash=(%s), paramName=(%s), expected value=(%s)', (hash, paramName, expectedParamValue) => {
Expand Down
16 changes: 14 additions & 2 deletions packages/clerk-js/src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ export const appendAsQueryParams = (
const sameOrigin = base.origin === url.origin;
params.append(camelToSnake(key), sameOrigin ? stripOrigin(url) : url + '');
}

// The following line will prepend the hash with a `/`.
// This is required for ClerkJS Components Hash router to work as expected
// as it treats the hash as sub-path with its nested querystring parameters.
return base + (params.toString() ? '#/?' + params.toString() : '');
};

Expand Down Expand Up @@ -256,7 +260,11 @@ export function setSearchParameterInHash({
const h = hash.startsWith('#') ? hash.substring(1) : hash;
const dummyUrlForHash = new URL(h, DUMMY_URL_BASE);
dummyUrlForHash.searchParams.set(paramName, paramValue);
return dummyUrlForHash.href.replace(DUMMY_URL_BASE, '').replace('/', '');

// The following line will prepend the hash with a `/`.
// This is required for ClerkJS Components Hash router to work as expected
// as it treats the hash as sub-path with its nested querystring parameters.
return dummyUrlForHash.href.replace(DUMMY_URL_BASE, '');
}

export function removeSearchParameterFromHash({
Expand All @@ -269,5 +277,9 @@ export function removeSearchParameterFromHash({
const h = hash.startsWith('#') ? hash.substring(1) : hash;
const dummyUrlForHash = new URL(h, DUMMY_URL_BASE);
dummyUrlForHash.searchParams.delete(paramName);
return dummyUrlForHash.href.replace(DUMMY_URL_BASE, '').replace('/', '');

// The following line will prepend the hash with a `/`.
// This is required for ClerkJS Components Hash router to work as expected
// as it treats the hash as sub-path with its nested querystring parameters.
return dummyUrlForHash.href.replace(DUMMY_URL_BASE, '');
}

0 comments on commit 0243403

Please sign in to comment.