Skip to content

Only add charset to content-type header if defined in mime db #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require('path');

const mime = require('mime');
const mimeDb = require('mime-db');

const DevMiddlewareError = require('./DevMiddlewareError');
const {
Expand All @@ -12,10 +13,6 @@ const {
ready,
} = require('./util');

// Do not add a charset to the Content-Type header of these file types
// otherwise the client will fail to render them correctly.
const NonCharsetFileTypes = /\.(wasm|usdz)$/;

module.exports = function wrapper(context) {
return function middleware(req, res, next) {
// fixes #282. credit @cexoso. in certain edge situations res.locals is
Expand Down Expand Up @@ -97,14 +94,17 @@ module.exports = function wrapper(context) {

content = handleRangeHeaders(content, req, res);

let contentType = mime.getType(filename) || '';

if (!NonCharsetFileTypes.test(filename)) {
contentType += '; charset=UTF-8';
}

if (!res.getHeader('Content-Type')) {
res.setHeader('Content-Type', contentType);
const contentType = mime.getType(filename);
const mimeInfo = mimeDb[contentType];
const charset = mimeInfo && mimeInfo.charset;

if (contentType) {
res.setHeader(
'Content-Type',
contentType + (charset ? `; charset=${charset}` : '')
);
}
}

res.setHeader('Content-Length', content.length);
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
],
"dependencies": {
"memory-fs": "^0.4.1",
"mime": "^2.3.1",
"mime": "^2.4.0",
"mime-db": "^1.38.0",
"range-parser": "^1.0.3",
"webpack-log": "^2.0.0"
},
Expand Down
18 changes: 10 additions & 8 deletions test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('Server', () => {
it('request to image', (done) => {
request(app)
.get('/public/svg.svg')
.expect('Content-Type', 'image/svg+xml; charset=UTF-8')
.expect('Content-Type', 'image/svg+xml')
.expect('Content-Length', '4778')
.expect(200, done);
});
Expand All @@ -124,15 +124,15 @@ describe('Server', () => {
it('request to directory', (done) => {
request(app)
.get('/public/')
.expect('Content-Type', 'text/html; charset=UTF-8')
.expect('Content-Type', 'text/html')
.expect('Content-Length', '10')
.expect(200, /My Index\./, done);
});

it('request to subdirectory without trailing slash', (done) => {
request(app)
.get('/public/reference/mono-v6.x.x')
.expect('Content-Type', 'text/html; charset=UTF-8')
.expect('Content-Type', 'text/html')
.expect('Content-Length', '9')
.expect(200, /My Index\./, done);
});
Expand All @@ -156,7 +156,6 @@ describe('Server', () => {
it('request to non-public path', (done) => {
request(app)
.get('/nonpublic/')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(404, done);
});
});
Expand Down Expand Up @@ -210,7 +209,6 @@ describe('Server', () => {
it('request to directory', (done) => {
request(app)
.get('/')
.expect('Content-Type', 'text/html; charset=utf-8')
.expect(404, done);
});
});
Expand Down Expand Up @@ -265,6 +263,10 @@ describe('Server', () => {
describe('no extension support', () => {
beforeAll((done) => {
app = express();
app.use((req, res, next) => {
res.set('Content-Type', 'text/plain');
next();
});
const compiler = webpack(webpackConfig);
instance = middleware(compiler, {
stats: 'errors-only',
Expand All @@ -281,7 +283,7 @@ describe('Server', () => {
request(app)
.get('/')
.expect('hello')
.expect('Content-Type', '; charset=UTF-8')
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect(200, done);
});
});
Expand Down Expand Up @@ -333,7 +335,7 @@ describe('Server', () => {
request(app)
.get('/')
.expect('welcome')
.expect('Content-Type', /text\/html/)
.expect('Content-Type', 'text/html')
.expect(200, done);
});
});
Expand Down Expand Up @@ -361,7 +363,7 @@ describe('Server', () => {
request(app)
.get('/')
.expect('welcome')
.expect('Content-Type', /text\/html/)
.expect('Content-Type', 'text/html')
.expect(200, done);
});
});
Expand Down