Skip to content

Commit 425b931

Browse files
author
Kyle Dickerson
committed
#3391 - Add History trailingSlash option always use trailing slash on root.
1 parent 4abcaa5 commit 425b931

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

backbone.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,7 @@
18511851
// Is pushState desired ... is it available?
18521852
this.options = _.extend({root: '/'}, this.options, options);
18531853
this.root = this.options.root;
1854+
this._trailingSlash = this.options.trailingSlash;
18541855
this._wantsHashChange = this.options.hashChange !== false;
18551856
this._hasHashChange = 'onhashchange' in window && (document.documentMode === void 0 || document.documentMode > 7);
18561857
this._useHashChange = this._wantsHashChange && this._hasHashChange;
@@ -1993,9 +1994,9 @@
19931994
// Normalize the fragment.
19941995
fragment = this.getFragment(fragment || '');
19951996

1996-
// Don't include a trailing slash on the root.
1997+
// Strip trailing slash on the root unless _trailingSlash is true
19971998
var rootPath = this.root;
1998-
if (fragment === '' || fragment.charAt(0) === '?') {
1999+
if (!this._trailingSlash && (fragment === '' || fragment.charAt(0) === '?')) {
19992000
rootPath = rootPath.slice(0, -1) || '/';
20002001
}
20012002
var url = rootPath + fragment;

index.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2706,7 +2706,17 @@ <h2 id="History">Backbone.history</h2>
27062706
<p>
27072707
If your application is not being served from the root url <tt>/</tt> of your
27082708
domain, be sure to tell History where the root really is, as an option:
2709-
<tt>Backbone.history.start({pushState: true, root: "/public/search/"})</tt>
2709+
<tt>Backbone.history.start({pushState: true, root: "/public/search/"})</tt>.
2710+
</p>
2711+
2712+
<p>
2713+
The value provided for <tt>root</tt> will be normalized to include a leading
2714+
and trailing slash. When navigating to a route the default behavior is to
2715+
exclude the trailing slash from the URL (e.g., <tt>/public/search?query=...</tt>).
2716+
If you prefer to include the trailing slash (e.g., <tt>/public/search/?query=...</tt>)
2717+
use <tt>Backbone.history.start({trailingSlash: true})</tt>.
2718+
URLs will always contain a leading slash. When root is <tt>/</tt> URLs will
2719+
look like <tt>/?query=...</tt> regardless of the value of <tt>trailingSlash</tt>.
27102720
</p>
27112721

27122722
<p>

test/router.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,38 @@
807807
Backbone.history.navigate('?x=1');
808808
});
809809

810+
QUnit.test('#3391 - Empty root normalizes to single slash.', function(assert) {
811+
assert.expect(1);
812+
Backbone.history.stop();
813+
Backbone.history = _.extend(new Backbone.History, {
814+
location: location,
815+
history: {
816+
pushState: function(state, title, url) {
817+
assert.strictEqual(url, '/');
818+
}
819+
}
820+
});
821+
location.replace('http://example.com/root/path');
822+
Backbone.history.start({pushState: true, hashChange: false, root: ''});
823+
Backbone.history.navigate('');
824+
});
825+
826+
QUnit.test('#3391 - Use trailing slash on root when trailingSlash is true.', function(assert) {
827+
assert.expect(1);
828+
Backbone.history.stop();
829+
Backbone.history = _.extend(new Backbone.History, {
830+
location: location,
831+
history: {
832+
pushState: function(state, title, url) {
833+
assert.strictEqual(url, '/root/');
834+
}
835+
}
836+
});
837+
location.replace('http://example.com/root/path');
838+
Backbone.history.start({pushState: true, hashChange: false, root: 'root', trailingSlash: true});
839+
Backbone.history.navigate('');
840+
});
841+
810842
QUnit.test('#2765 - Fragment matching sans query/hash.', function(assert) {
811843
assert.expect(2);
812844
Backbone.history.stop();

0 commit comments

Comments
 (0)