Skip to content

Commit

Permalink
Merge pull request #399 from cah-briangantzler/fix-question
Browse files Browse the repository at this point in the history
fix: do not show question mark when there are no query params
  • Loading branch information
NullVoxPopuli authored Nov 15, 2021
2 parents 2952069 + d419281 commit 848dbd3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions addon/services/query-params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { tracked } from '@glimmer/tracking';
import Service, { inject as service } from '@ember/service';
import { isPresent } from '@ember/utils';

import * as qs from 'qs';

Expand Down Expand Up @@ -67,7 +68,7 @@ export default class QueryParamsService extends Service {
const queryParams = this.byPath[path];
const existing = qs.parse(search.split('?')[1]);
const query = qs.stringify({ ...existing, ...queryParams });
const newUrl = `${protocol}//${host}${pathname}${hash}?${query}`;
const newUrl = `${protocol}//${host}${pathname}${hash}${isPresent(query) ? '?' : ''}${query}`;

window.history.replaceState({ path: newUrl }, '', newUrl);
}
Expand Down Expand Up @@ -101,7 +102,7 @@ const queryParamHandler = {
set(obj: any, key: string, value: any, ...rest: any[]) {
let { protocol, host, pathname } = window.location;
let query = qs.stringify({ ...obj, [key]: value });
let newUrl = `${protocol}//${host}${pathname}?${query}`;
let newUrl = `${protocol}//${host}${pathname}${isPresent(query) ? '?' : ''}${query}`;

window.history.pushState({ path: newUrl }, '', newUrl);

Expand Down
6 changes: 6 additions & 0 deletions tests/acceptance/navigation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ module('Acceptance | Navigation', function (hooks) {
assert.equal(service.current.a, '1');
assert.equal(service.byPath['/'].a, '1');
});

test('the question mark is not displayed if there are no query params', function (assert) {
assert.ok(window.location.search.includes('?'));
getQPService().current.a = undefined;
assert.notOk(window.location.search.includes('?'));
});
});
});

Expand Down

0 comments on commit 848dbd3

Please sign in to comment.