Skip to content

Commit 18fee7c

Browse files
samoussHaroenv
authored andcommitted
fix(history): avoid empty query string (#4130)
**Summary** This PR fixes an issue with the `historyRouter` + `simpleStateMapping`. They push an empty query string into the URL. The problem arise because we now always have an object with at least on key which is the top level index. The condition to add or not the query string is done on the `routeState` but it could be done on the query string itself since we already have computed it. **Before** ![before](https://user-images.githubusercontent.com/6513513/65231669-2f1d1a80-dad0-11e9-8184-bd0ab3a8430d.gif) **After** ![after](https://user-images.githubusercontent.com/6513513/65231680-32b0a180-dad0-11e9-9928-6a5231dfee60.gif)
1 parent 9cbd24a commit 18fee7c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/lib/__tests__/RoutingManager-test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -564,4 +564,32 @@ describe('RoutingManager', () => {
564564
expect(parsedUrl.refinementList.brand).toBeInstanceOf(Array);
565565
});
566566
});
567+
568+
describe('createURL', () => {
569+
it('returns an URL for a `routeState` with refinements', () => {
570+
const router = historyRouter();
571+
const actual = router.createURL({
572+
query: 'iPhone',
573+
page: 5,
574+
});
575+
576+
expect(actual).toBe('https://website.com/?query=iPhone&page=5');
577+
});
578+
579+
it('returns an URL for an empty `routeState` with index', () => {
580+
const router = historyRouter();
581+
const actual = router.createURL({
582+
indexName: {},
583+
});
584+
585+
expect(actual).toBe('https://website.com/');
586+
});
587+
588+
it('returns an URL for an empty `routeState`', () => {
589+
const router = historyRouter();
590+
const actual = router.createURL({});
591+
592+
expect(actual).toBe('https://website.com/');
593+
});
594+
});
567595
});

src/lib/routers/history.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const defaultCreateURL: CreateURL = ({ qsModule, routeState, location }) => {
3232
const portWithPrefix = port === '' ? '' : `:${port}`;
3333

3434
// IE <= 11 has no proper `location.origin` so we cannot rely on it.
35-
if (!routeState || Object.keys(routeState).length === 0) {
35+
if (!queryString) {
3636
return `${protocol}//${hostname}${portWithPrefix}${pathname}${hash}`;
3737
}
3838

0 commit comments

Comments
 (0)