Skip to content
Open
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
1 change: 1 addition & 0 deletions build/director.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ Router.prototype.destroy = function () {

Router.prototype.getPath = function () {
var path = window.location.pathname;
path = path.replace(/\/$/, '');
if (path.substr(0, 1) !== '/') {
path = '/' + path;
}
Expand Down
5 changes: 5 additions & 0 deletions lib/director/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ Router.prototype.destroy = function () {

Router.prototype.getPath = function () {
var path = window.location.pathname;

// Browsers love to add a trailing / on paths which
// breaks the initial routing in html5
path = path.replace(/\/$/, '');

if (path.substr(0, 1) !== '/') {
path = '/' + path;
}
Expand Down
9 changes: 9 additions & 0 deletions test/browser/backend/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var http = require('http'),
director = require('../../../lib/director'),
index;

var __filename = module.uri,
__dirname = path.dirname(__filename);

fs.readFile(path.join(__dirname, '..', 'html5-routes-harness.html'), function (err, data) {
if (err) {
throw err;
Expand All @@ -20,10 +23,16 @@ var CONTENT_TYPES = {
// Dummy file server
function fileServer(folder, file) {
var root = path.resolve(__dirname, '..');

if (folder === 'build' || folder === 'node_modules') {
root = path.resolve(root, '..', '..');
}

if(file === undefined) {
file = folder;
folder = '.';
}

var filepath = path.resolve(root, folder, file);

var res = this.res;
Expand Down
31 changes: 31 additions & 0 deletions test/browser/html5-routes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,34 @@ createTest('route should accept _ and . within parameters', {
this.finish();
});
});

// This test doesn't use the createTest since createTest runs init on the router before
// running the test, which is what we want to test.
test('fire the correct route when initializing the router', function(){
var fired = [];
if (browser_history_support) {
window.history.pushState({}, 'Initial', '/initial/');
}
else {
fired = ['/initial', 'initial'];
}
var router = new Router();
router.mount({
'/initial': function(){
fired.push('/initial');
},
'initial': function(){
fired.push('initial');
},
'/': function(){
fired.push('/');
}
});
router.configure({
run_handler_in_init: true,
html5history: true
});
router.init();
deepEqual(fired, ['/initial', 'initial']);
router.destroy();
});