diff --git a/build/director.js b/build/director.js index eebbf56..e435103 100644 --- a/build/director.js +++ b/build/director.js @@ -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; } diff --git a/lib/director/browser.js b/lib/director/browser.js index 34519f7..df65d3e 100644 --- a/lib/director/browser.js +++ b/lib/director/browser.js @@ -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; } diff --git a/test/browser/backend/backend.js b/test/browser/backend/backend.js index 980e58b..110cede 100644 --- a/test/browser/backend/backend.js +++ b/test/browser/backend/backend.js @@ -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; @@ -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; diff --git a/test/browser/html5-routes-test.js b/test/browser/html5-routes-test.js index 628d3c9..928dfef 100644 --- a/test/browser/html5-routes-test.js +++ b/test/browser/html5-routes-test.js @@ -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(); +}); \ No newline at end of file