1
1
import path from 'path'
2
- import type { ServerResponse } from 'http'
2
+ import type { OutgoingHttpHeaders , ServerResponse } from 'http'
3
3
import type { Options } from 'sirv'
4
4
import sirv from 'sirv'
5
5
import type { Connect } from 'types/connect'
@@ -20,24 +20,34 @@ import {
20
20
21
21
const { isMatch } = micromatch
22
22
23
- const sirvOptions : Options = {
24
- dev : true ,
25
- etag : true ,
26
- extensions : [ ] ,
27
- setHeaders ( res , pathname ) {
28
- // Matches js, jsx, ts, tsx.
29
- // The reason this is done, is that the .ts file extension is reserved
30
- // for the MIME type video/mp2t. In almost all cases, we can expect
31
- // these files to be TypeScript files, and for Vite to serve them with
32
- // this Content-Type.
33
- if ( / \. [ t j ] s x ? $ / . test ( pathname ) ) {
34
- res . setHeader ( 'Content-Type' , 'application/javascript' )
23
+ const sirvOptions = ( headers ?: OutgoingHttpHeaders ) : Options => {
24
+ return {
25
+ dev : true ,
26
+ etag : true ,
27
+ extensions : [ ] ,
28
+ setHeaders ( res , pathname ) {
29
+ // Matches js, jsx, ts, tsx.
30
+ // The reason this is done, is that the .ts file extension is reserved
31
+ // for the MIME type video/mp2t. In almost all cases, we can expect
32
+ // these files to be TypeScript files, and for Vite to serve them with
33
+ // this Content-Type.
34
+ if ( / \. [ t j ] s x ? $ / . test ( pathname ) ) {
35
+ res . setHeader ( 'Content-Type' , 'application/javascript' )
36
+ }
37
+ if ( headers ) {
38
+ for ( const name in headers ) {
39
+ res . setHeader ( name , headers [ name ] ! )
40
+ }
41
+ }
35
42
}
36
43
}
37
44
}
38
45
39
- export function servePublicMiddleware ( dir : string ) : Connect . NextHandleFunction {
40
- const serve = sirv ( dir , sirvOptions )
46
+ export function servePublicMiddleware (
47
+ dir : string ,
48
+ headers ?: OutgoingHttpHeaders
49
+ ) : Connect . NextHandleFunction {
50
+ const serve = sirv ( dir , sirvOptions ( headers ) )
41
51
42
52
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
43
53
return function viteServePublicMiddleware ( req , res , next ) {
@@ -53,7 +63,7 @@ export function serveStaticMiddleware(
53
63
dir : string ,
54
64
server : ViteDevServer
55
65
) : Connect . NextHandleFunction {
56
- const serve = sirv ( dir , sirvOptions )
66
+ const serve = sirv ( dir , sirvOptions ( server . config . server . headers ) )
57
67
58
68
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
59
69
return function viteServeStaticMiddleware ( req , res , next ) {
@@ -109,7 +119,7 @@ export function serveStaticMiddleware(
109
119
export function serveRawFsMiddleware (
110
120
server : ViteDevServer
111
121
) : Connect . NextHandleFunction {
112
- const serveFromRoot = sirv ( '/' , sirvOptions )
122
+ const serveFromRoot = sirv ( '/' , sirvOptions ( server . config . server . headers ) )
113
123
114
124
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
115
125
return function viteServeRawFsMiddleware ( req , res , next ) {
0 commit comments