Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions core/src/components/router/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ export class Router implements ComponentInterface {
console.debug('[ion-router] URL pushed -> updating nav', url, direction);

const path = parsePath(url);
this.setPath(path, direction);
const queryString = url.split('?')[1];
this.setPath(path, direction, queryString);
return this.writeNavStateRoot(path, direction);
}

Expand Down Expand Up @@ -264,9 +265,9 @@ export class Router implements ComponentInterface {
return changed;
}

private setPath(path: string[], direction: RouterDirection) {
private setPath(path: string[], direction: RouterDirection, queryString?: string) {
this.state++;
writePath(window.history, this.root, this.useHash, path, direction, this.state);
writePath(window.history, this.root, this.useHash, path, direction, this.state, queryString);
}

private getPath(): string[] | null {
Expand Down
5 changes: 4 additions & 1 deletion core/src/components/router/test/parser.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ describe('parser', () => {
const r4 = mockRouteElement(win, '/5/hola', '4');
const r5 = mockRouteElement(win, '/path/to/five', '5');
const r6 = mockRouteElement(win, '/path/to/five2', '6');
const r7 = mockRouteElement(win, '/path?flag=true', '6');

root.appendChild(r1);
root.appendChild(r2);
root.appendChild(r3);
root.appendChild(r7);
r3.appendChild(r4);
r4.appendChild(r5);
r4.appendChild(r6);
Expand All @@ -30,7 +32,8 @@ describe('parser', () => {
{ path: ['path', 'to', 'five'], id: '5', children: [], params: undefined },
{ path: ['path', 'to', 'five2'], id: '6', children: [], params: undefined }
] }
] }
] },
{ path: ['path'], id: '6', children: [], params: undefined }
];
expect(readRouteNodes(root)).toEqual(expected);
});
Expand Down
12 changes: 12 additions & 0 deletions core/src/components/router/test/path.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ describe('writePath', () => {

writePath(history, '/', false, ['to', 'schedule'], ROUTER_INTENT_FORWARD, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '/to/schedule');

writePath(history, '/', false, ['to', 'schedule'], ROUTER_INTENT_FORWARD, 123, 'flag=true');
expect(history.pushState).toHaveBeenCalledWith(123, '', '/to/schedule?flag=true');
});

it('should write non root path (no hash)', () => {
Expand All @@ -204,6 +207,9 @@ describe('writePath', () => {

writePath(history, '/path/to/', false, ['second', 'page'], ROUTER_INTENT_FORWARD, 2);
expect(history.pushState).toHaveBeenCalledWith(2, '', '/path/to/second/page');

writePath(history, '/path/to/', false, ['second', 'page'], ROUTER_INTENT_FORWARD, 2, 'flag=true');
expect(history.pushState).toHaveBeenCalledWith(2, '', '/path/to/second/page?flag=true');
});

it('should write root path (no hash)', () => {
Expand All @@ -219,6 +225,9 @@ describe('writePath', () => {

writePath(history, '/', true, ['to', 'schedule'], ROUTER_INTENT_FORWARD, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/to/schedule');

writePath(history, '/', true, ['to', 'schedule'], ROUTER_INTENT_FORWARD, 123, 'flag=true');
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/to/schedule?flag=true');
});

it('should write non root path (no hash)', () => {
Expand All @@ -234,6 +243,9 @@ describe('writePath', () => {

writePath(history, '/path/to/', true, ['second', 'page'], ROUTER_INTENT_FORWARD, 123);
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/path/to/second/page');

writePath(history, '/path/to/', true, ['second', 'page'], ROUTER_INTENT_FORWARD, 123, 'flag=true');
expect(history.pushState).toHaveBeenCalledWith(123, '', '#/path/to/second/page?flag=true');
});
});

Expand Down
8 changes: 6 additions & 2 deletions core/src/components/router/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ export const chainToPath = (chain: RouteChain): string[] | null => {
return path;
};

export const writePath = (history: History, root: string, useHash: boolean, path: string[], direction: RouterDirection, state: number) => {
export const writePath = (history: History, root: string, useHash: boolean, path: string[], direction: RouterDirection, state: number, queryString?: string) => {
let url = generatePath([
...parsePath(root),
...path
]);
if (useHash) {
url = '#' + url;
}
if (queryString !== undefined) {
url = url + '?' + queryString;
}
if (direction === ROUTER_INTENT_FORWARD) {
history.pushState(state, '', url);
} else {
Expand Down Expand Up @@ -79,7 +82,8 @@ export const parsePath = (path: string | undefined | null): string[] => {
if (path == null) {
return [''];
}
const segments = path.split('/')
const removeQueryString = path.split('?')[0];
const segments = removeQueryString.split('/')
.map(s => s.trim())
.filter(s => s.length > 0);

Expand Down