Skip to content

Commit dd6d59e

Browse files
authored
fix(middleware): switch headers and mime types handling back after file check (#672)
1 parent 7fa2c15 commit dd6d59e

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/middleware.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@ export default function wrapper(context) {
4949
return;
5050
}
5151

52-
// Content-Type and headers need to be set before checking if
53-
// the file is in the outputFileSystem, as these things should be
54-
// applied to all files that are being served
52+
try {
53+
content = context.outputFileSystem.readFileSync(filename);
54+
} catch (_ignoreError) {
55+
await goNext();
56+
return;
57+
}
5558

5659
if (!res.get('Content-Type')) {
5760
// content-type name(like application/javascript; charset=utf-8) or false
@@ -68,13 +71,6 @@ export default function wrapper(context) {
6871
}
6972
}
7073

71-
try {
72-
content = context.outputFileSystem.readFileSync(filename);
73-
} catch (_ignoreError) {
74-
await goNext();
75-
return;
76-
}
77-
7874
// Buffer
7975
content = handleRangeHeaders(content, req, res);
8076

test/middleware.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,7 +2080,7 @@ describe('middleware', () => {
20802080
});
20812081
});
20822082

2083-
describe('should set "Content-Type" header for route not from outputFileSystem', () => {
2083+
describe('should not set "Content-Type" header for route not from outputFileSystem', () => {
20842084
beforeAll((done) => {
20852085
const outputPath = path.resolve(__dirname, './outputs/basic');
20862086
const compiler = getCompiler({
@@ -2109,10 +2109,10 @@ describe('middleware', () => {
21092109

21102110
afterAll(close);
21112111

2112-
it('should return the "200" code for the "GET" request "file.jpg"', (done) => {
2112+
it('should return the "200" code for the "GET" request "file.jpg" with default content type', (done) => {
21132113
request(app)
21142114
.get('/file.jpg')
2115-
.expect('Content-Type', /application\/octet-stream/)
2115+
.expect('Content-Type', /text\/html/)
21162116
.expect(200, done);
21172117
});
21182118
});
@@ -2741,15 +2741,15 @@ describe('middleware', () => {
27412741
.expect(200, done);
27422742
});
27432743

2744-
it('should return the "200" code for the "GET" request to path not in outputFileSystem and return headers', (done) => {
2744+
it('should return the "200" code for the "GET" request to path not in outputFileSystem but not return headers', async () => {
27452745
app.get('/file.jpg', (req, res) => {
27462746
res.send('welcome');
27472747
});
2748-
request(app)
2749-
.get('/file.jpg')
2750-
.expect('X-nonsense-1', 'yes')
2751-
.expect('X-nonsense-2', 'no')
2752-
.expect(200, done);
2748+
2749+
const res = await request(app).get('/file.jpg');
2750+
expect(res.statusCode).toEqual(200);
2751+
expect(res.headers['X-nonsense-1']).toBeUndefined();
2752+
expect(res.headers['X-nonsense-2']).toBeUndefined();
27532753
});
27542754
});
27552755

0 commit comments

Comments
 (0)