Skip to content

Commit

Permalink
fix(core): 🐛 fix sameUrl with query params
Browse files Browse the repository at this point in the history
Closes #389
  • Loading branch information
thierrymichel committed Apr 29, 2019
1 parent 640a837 commit fa79a6a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
6 changes: 4 additions & 2 deletions packages/core/__tests__/utils/url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ it('parse complex query', () => {
});

it('clean url', () => {
const result = url.clean('http://localhost/foo', window.location.origin);
// const result = url.clean('http://localhost/foo', window.location.origin);
// expect(result).toBe('/foo');
const result = url.clean('http://localhost/foo?bar=baz#qux');

expect(result).toBe('/foo');
expect(result).toBe('http://localhost/foo?bar=baz');
});
2 changes: 1 addition & 1 deletion packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export class Core {
// Check prevent sameURL against current history
if (trigger === 'popstate') {
self =
this.history &&
this.history.current &&
this.url.getPath(this.history.current.url) === this.url.getPath(href);
} else {
self = this.prevent.run('sameUrl', null, null, href);
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/modules/Prevent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ const preventAll: PreventCheck = ({ el }) =>
* > Not in the test suite.
*/
const sameUrl: PreventCheck = ({ href }) =>
url.getPath(href) === url.getPath(window.location.href);
// DEV
// url.getPath(href) === url.getPath(window.location.href);
url.clean(href) === url.clean(window.location.href);

export class Prevent extends Ignore {
public suite: string[] = [];
Expand Down
11 changes: 8 additions & 3 deletions packages/core/src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export const getPath = (url: string): string => parse(url).path;
* Parse URL for path, query and hash.
*/
export const parse = (url: string): IUrlBase => {
let path = clean(url);
// let path = clean(url);
let path = url.replace(getOrigin(), '');
let hash;
let query = {};

Expand Down Expand Up @@ -97,8 +98,12 @@ export const parseQuery = (str: string) =>
return acc;
}, {});

/**
* Clean URL, remove "hash".
*/
export const clean = (url: string) => url.replace(/#.*/, '');
/**
* Clean URL, remove "origin".
*/
export const clean = (url: string, origin = getOrigin()) =>
url.replace(origin, '');
// export const clean = (url: string, origin = getOrigin()) =>
// url.replace(origin, '');

0 comments on commit fa79a6a

Please sign in to comment.