From 3435c4c10c7234f292ac6809c5fbd680c6a915c8 Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Wed, 11 Apr 2018 06:45:16 +0200 Subject: [PATCH] Fix serving files with query params (#1169) --- src/Server.js | 8 +++++--- test/server.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Server.js b/src/Server.js index 331843b94fb..cf5f1d6fc1e 100644 --- a/src/Server.js +++ b/src/Server.js @@ -7,6 +7,7 @@ const generateCertificate = require('./utils/generateCertificate'); const getCertificate = require('./utils/getCertificate'); const logger = require('./Logger'); const path = require('path'); +const url = require('url'); serveStatic.mime.define({ 'application/wasm': ['wasm'] @@ -43,18 +44,19 @@ function middleware(bundler) { } function respond() { + let {pathname} = url.parse(req.url); if (bundler.errored) { return send500(); } else if ( - !req.url.startsWith(bundler.options.publicURL) || - path.extname(req.url) === '' + !pathname.startsWith(bundler.options.publicURL) || + path.extname(pathname) === '' ) { // 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 - req.url = req.url.slice(bundler.options.publicURL.length); + req.url = pathname.slice(bundler.options.publicURL.length); return serve(req, res, send404); } } diff --git a/test/server.js b/test/server.js index 8bb5ce60de9..3a1fbb38576 100644 --- a/test/server.js +++ b/test/server.js @@ -130,4 +130,14 @@ describe('server', function() { data = await get('/hello.txt'); assert.equal(data, 'hello'); }); + + it('should work with query parameters that contain a dot', async function() { + let b = bundler(__dirname + '/integration/html/index.html', { + publicUrl: '/' + }); + server = await b.serve(0); + + let data = await get('/?foo=bar.baz'); + assert.equal(data, fs.readFileSync(__dirname + '/dist/index.html', 'utf8')); + }); });