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