Skip to content

Commit 0f03a72

Browse files
authored
fix: Ignore preserveQuery option in Location.goto() for shallow routing (#84)
As it was, it was processing the URL (an empty string), therefore losing the URL's pathname.
1 parent ed236eb commit 0f03a72

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/lib/core/LocationLite.svelte.test.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,25 +160,36 @@ describe("LocationLite", () => {
160160
text: 'preserve',
161161
expectedQs: '?plus=another&extra=thing',
162162
},
163-
])
164-
("Should $text the query string when instructed by the value $preserveQuery in the preserveQuery option.", ({ qs, preserveQuery, expectedQs }) => {
165-
// Arrange.
166-
location.url.search = window.location.search = qs;
167-
const newPath = '/new/path';
163+
])("Should $text the query string when instructed by the value $preserveQuery in the preserveQuery option.", ({ qs, preserveQuery, expectedQs }) => {
164+
// Arrange.
165+
location.url.search = window.location.search = qs;
166+
const newPath = '/new/path';
168167

169-
// Act.
170-
location.goTo(newPath, { preserveQuery });
168+
// Act.
169+
location.goTo(newPath, { preserveQuery });
171170

172-
// Assert.
173-
expect(window.location.pathname).to.equal(newPath);
174-
expect(window.location.search).to.equal(`${expectedQs}`);
175-
});
171+
// Assert.
172+
expect(window.location.pathname).to.equal(newPath);
173+
expect(window.location.search).to.equal(`${expectedQs}`);
174+
});
175+
test("Should ignore the preserveQuery option when doing shallow routing.", () => {
176+
// Arrange.
177+
const currentPath = "/current/path";
178+
window.location.href = `${currentPath}?some=value&plus=another`;
179+
location.url.href = window.location.href;
180+
console.debug('Path: ', window.location.pathname);
181+
182+
// Act.
183+
location.goTo('', { preserveQuery: true });
184+
// Assert.
185+
expect(window.location.pathname).to.equal(currentPath);
186+
});
176187
});
177188
describe('navigate', () => {
178189
afterEach(() => {
179190
resetRoutingOptions();
180191
});
181-
192+
182193
test.each<{
183194
qs: string;
184195
preserveQuery: PreserveQuery;
@@ -229,17 +240,17 @@ describe("LocationLite", () => {
229240
{
230241
hash: ALL_HASHES.path,
231242
desc: 'path',
232-
options: { disallowPathRouting: true}
243+
options: { disallowPathRouting: true }
233244
},
234245
{
235246
hash: ALL_HASHES.single,
236247
desc: 'hash',
237-
options: { disallowHashRouting: true}
248+
options: { disallowHashRouting: true }
238249
},
239250
{
240251
hash: ALL_HASHES.multi,
241252
desc: 'multi hash',
242-
options: { disallowMultiHashRouting: true}
253+
options: { disallowMultiHashRouting: true }
243254
},
244255
])("Should throw an error when the hash option is $hash and $desc routing is disallowed.", ({ hash, options }) => {
245256
// Arrange.

src/lib/core/LocationLite.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class LocationLite implements Location {
7676
}
7777

7878
goTo(url: string, options?: GoToOptions): void {
79-
if (options?.preserveQuery) {
79+
if (options?.preserveQuery && url !== '') {
8080
url = preserveQueryInUrl(url, options.preserveQuery);
8181
}
8282
this.#goTo(url, options?.replace ?? false, options?.state);

0 commit comments

Comments
 (0)