From 43cab32ece825e9d38b3abe55fa0fde06052864d Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 22 Mar 2018 02:54:50 -0400 Subject: [PATCH] HTML bundle serving bug. (#974) --- src/Server.js | 8 ++++++-- test/server.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Server.js b/src/Server.js index bfa03ad50ab..331843b94fb 100644 --- a/src/Server.js +++ b/src/Server.js @@ -45,8 +45,12 @@ function middleware(bundler) { function respond() { if (bundler.errored) { return send500(); - } else if (!req.url.startsWith(bundler.options.publicURL)) { - // If the URL doesn't start with the public path, send the main HTML bundle + } else if ( + !req.url.startsWith(bundler.options.publicURL) || + path.extname(req.url) === '' + ) { + // If the URL doesn't start with the public path, or the URL doesn't + // have a file extension, send the main HTML bundle. return sendIndex(); } else { // Otherwise, serve the file from the dist folder diff --git a/test/server.js b/test/server.js index 2ba1ba4c12e..f085414ccb4 100644 --- a/test/server.js +++ b/test/server.js @@ -106,4 +106,18 @@ describe('server', function() { let data = await get('/dist/index.js', https); assert.equal(data, fs.readFileSync(__dirname + '/dist/index.js', 'utf8')); }); + + it('should serve static assets as well as html', async function() { + let b = bundler(__dirname + '/integration/html/index.html', { + publicUrl: '/' + }); + server = await b.serve(0); + // When accessing / we should get the index page. + let data = await get('/'); + assert.equal(data, fs.readFileSync(__dirname + '/dist/index.html', 'utf8')); + // When accessing /hello.txt we should get txt document. + fs.writeFileSync(__dirname + '/dist/hello.txt', 'hello'); + data = await get('/hello.txt'); + assert.equal(data, 'hello'); + }); });