Skip to content

Commit 408e5a7

Browse files
authored
fix: respect server.headers in static middlewares (#8481)
1 parent 6d0ede7 commit 408e5a7

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

packages/vite/src/node/server/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,9 @@ export async function createServer(
477477
// this applies before the transform middleware so that these files are served
478478
// as-is without transforms.
479479
if (config.publicDir) {
480-
middlewares.use(servePublicMiddleware(config.publicDir))
480+
middlewares.use(
481+
servePublicMiddleware(config.publicDir, config.server.headers)
482+
)
481483
}
482484

483485
// main transform middleware

packages/vite/src/node/server/middlewares/static.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'path'
2-
import type { ServerResponse } from 'http'
2+
import type { OutgoingHttpHeaders, ServerResponse } from 'http'
33
import type { Options } from 'sirv'
44
import sirv from 'sirv'
55
import type { Connect } from 'types/connect'
@@ -20,24 +20,34 @@ import {
2020

2121
const { isMatch } = micromatch
2222

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 (/\.[tj]sx?$/.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 (/\.[tj]sx?$/.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+
}
3542
}
3643
}
3744
}
3845

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))
4151

4252
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
4353
return function viteServePublicMiddleware(req, res, next) {
@@ -53,7 +63,7 @@ export function serveStaticMiddleware(
5363
dir: string,
5464
server: ViteDevServer
5565
): Connect.NextHandleFunction {
56-
const serve = sirv(dir, sirvOptions)
66+
const serve = sirv(dir, sirvOptions(server.config.server.headers))
5767

5868
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
5969
return function viteServeStaticMiddleware(req, res, next) {
@@ -109,7 +119,7 @@ export function serveStaticMiddleware(
109119
export function serveRawFsMiddleware(
110120
server: ViteDevServer
111121
): Connect.NextHandleFunction {
112-
const serveFromRoot = sirv('/', sirvOptions)
122+
const serveFromRoot = sirv('/', sirvOptions(server.config.server.headers))
113123

114124
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
115125
return function viteServeRawFsMiddleware(req, res, next) {

0 commit comments

Comments
 (0)