Skip to content

Commit

Permalink
Merge pull request #27 from jakemmarsh/fix-server
Browse files Browse the repository at this point in the history
Fix browser-sync asset serving
  • Loading branch information
jakemmarsh committed Dec 9, 2015
2 parents d1334f1 + 3c348a0 commit 55b98d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 14 additions & 0 deletions gulp/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ const config = {

testFiles: './__tests__/**/*.{js,jsx}',

assetExtensions: [
'js',
'css',
'png',
'jpe?g',
'gif',
'svg',
'eot',
'otf',
'ttc',
'ttf',
'woff2?'
]

};

export default config;
7 changes: 3 additions & 4 deletions gulp/tasks/browserSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import config from '../config';
gulp.task('browserSync', function() {

const DEFAULT_FILE = 'index.html';
const ASSET_EXTENSIONS = ['js', 'css', 'png', 'jpg', 'jpeg', 'gif'];
const ASSET_EXTENSION_REGEX = new RegExp(`\\b(?!\\?)\\.(${config.assetExtensions.join('|')})\\b(?!\\.)`, 'i');

browserSync.init({
server: {
baseDir: config.buildDir,
middleware: function(req, res, next) {
let fileHrefArray = url.parse(req.url).href.split('.');
let fileExtension = fileHrefArray[fileHrefArray.length - 1];
let fileHref = url.parse(req.url).href;

if ( ASSET_EXTENSIONS.indexOf(fileExtension) === -1 ) {
if ( !ASSET_EXTENSION_REGEX.test(fileHref)) {
req.url = '/' + DEFAULT_FILE;
}

Expand Down

0 comments on commit 55b98d6

Please sign in to comment.