Skip to content

Commit

Permalink
Merge 567b805 into 475ad56
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Apr 30, 2015
2 parents 475ad56 + 567b805 commit 03ae2fa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 20 deletions.
51 changes: 31 additions & 20 deletions lib/tasks/server/middleware/history-support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,45 @@ function HistorySupportAddon(project) {
this.name = 'history-support-middleware';
}

HistorySupportAddon.prototype.isEnabled = function(environment) {
var config = this.project.config(environment);
var locationType = config.locationType;
var historySupportMiddlewareEnabled = config.historySupportMiddleware;

return ['auto', 'history'].indexOf(locationType) !== -1 || historySupportMiddlewareEnabled;
};

HistorySupportAddon.prototype.serverMiddleware = function(config) {
if (this.isEnabled(config.options.environment)) {
this.addMiddleware(config);
}
};

HistorySupportAddon.prototype.addMiddleware = function(config) {
var app = config.app;
var options = config.options;
var watcher = options.watcher;

var baseURL = cleanBaseURL(options.baseURL);
var baseURLRegexp = new RegExp('^' + baseURL);
var locationType = this.project.config(options.environment).locationType;

if (['auto', 'history'].indexOf(locationType) !== -1) {
app.use(function(req, res, next) {
watcher.then(function(results) {

var acceptHeaders = req.headers.accept || [];
var hasHTMLHeader = acceptHeaders.indexOf('text/html') !== -1;
var isForBaseURL = baseURLRegexp.test(req.path);

if (hasHTMLHeader && isForBaseURL && req.method === 'GET') {
var assetPath = req.path.slice(baseURL.length);
var isFile = false;
try { isFile = fs.statSync(path.join(results.directory, assetPath)).isFile(); } catch (err) { }
if (!isFile) {
req.serveUrl = baseURL + 'index.html';
}

app.use(function(req, res, next) {
watcher.then(function(results) {

var acceptHeaders = req.headers.accept || [];
var hasHTMLHeader = acceptHeaders.indexOf('text/html') !== -1;
var isForBaseURL = baseURLRegexp.test(req.path);

if (hasHTMLHeader && isForBaseURL && req.method === 'GET') {
var assetPath = req.path.slice(baseURL.length);
var isFile = false;
try { isFile = fs.statSync(path.join(results.directory, assetPath)).isFile(); } catch (err) { }
if (!isFile) {
req.serveUrl = baseURL + 'index.html';
}
}).finally(next);
});
}
}
}).finally(next);
});
};

module.exports = HistorySupportAddon;
23 changes: 23 additions & 0 deletions tests/unit/tasks/server/express-server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,29 @@ describe('express-server', function() {
});
});

it('serves index.html when file not found (with baseURL) with custom history location', function(done) {
project._config = {
baseURL: '/',
locationType: 'blahr',
historySupportMiddleware: true
};

return startServer('/foo')
.then(function() {
request(subject.app)
.get('/foo/someurl')
.set('accept', 'text/html')
.expect(200)
.expect('Content-Type', /html/)
.end(function(err) {
if (err) {
return done(err);
}
done();
});
});
});

it('returns a 404 when file not found with hash location', function(done) {
project._config = {
baseURL: '/',
Expand Down

0 comments on commit 03ae2fa

Please sign in to comment.