Skip to content

Commit

Permalink
Handle paths that already have trailing slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
kategengler committed Dec 17, 2024
1 parent 58d951b commit e7477a7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/locations/trailing-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export default HistoryLocation.extend({

export function formatURL(url) {
let modifiedURL = new URL(url, 'http://example.com');
modifiedURL.pathname += '/';
if (!modifiedURL.pathname.endsWith('/')) {
modifiedURL.pathname += '/';
}
return `${modifiedURL.pathname}${modifiedURL.search}${modifiedURL.hash}`;
}
4 changes: 4 additions & 0 deletions tests/unit/locations/trailing-history-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module('Unit | Locations | trailing history', function (hooks) {
assert.deepEqual(formatURL('/foo#placeholder'), '/foo/#placeholder');
});

test('does not add double slashes', function (assert) {
assert.deepEqual(formatURL('/foo/'), '/foo/');
});

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

0 comments on commit e7477a7

Please sign in to comment.