Skip to content

Commit

Permalink
Add support for query params to the TrailingHistory location
Browse files Browse the repository at this point in the history
  • Loading branch information
kategengler committed Dec 10, 2024
1 parent 39d118d commit 58d951b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
15 changes: 8 additions & 7 deletions app/locations/trailing-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import HistoryLocation from '@ember/routing/history-location';
export default HistoryLocation.extend({
formatURL() {
let url = this._super(...arguments);

if (url.includes('#')) {
return url.replace(/([^/])#(.*)/, '$1/#$2');
} else {
return url.replace(/\/?$/, '/');
}
}
return formatURL(url);
},
});

export function formatURL(url) {
let modifiedURL = new URL(url, 'http://example.com');
modifiedURL.pathname += '/';
return `${modifiedURL.pathname}${modifiedURL.search}${modifiedURL.hash}`;
}
22 changes: 22 additions & 0 deletions tests/unit/locations/trailing-history-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { formatURL } from 'dummy/locations/trailing-history';

module('Unit | Locations | trailing history', function (hooks) {
setupTest(hooks);

test('supports query params', function (assert) {
assert.deepEqual(formatURL('/foo?bar=baz'), '/foo/?bar=baz');
});

test('supports anchors', function (assert) {
assert.deepEqual(formatURL('/foo#placeholder'), '/foo/#placeholder');
});

test('supports query params and anchors', function (assert) {
assert.deepEqual(
formatURL('/foo?bar=baz#placeholder'),
'/foo/?bar=baz#placeholder',
);
});
});

0 comments on commit 58d951b

Please sign in to comment.