Skip to content

Commit ab7dc1c

Browse files
committed
fix: backport respect server.headers in static middlewares (#8481)
1 parent ced0374 commit ab7dc1c

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
@@ -539,7 +539,9 @@ export async function createServer(
539539
// this applies before the transform middleware so that these files are served
540540
// as-is without transforms.
541541
if (config.publicDir) {
542-
middlewares.use(servePublicMiddleware(config.publicDir))
542+
middlewares.use(
543+
servePublicMiddleware(config.publicDir, config.server.headers)
544+
)
543545
}
544546

545547
// 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'
@@ -18,24 +18,34 @@ import {
1818
slash
1919
} from '../../utils'
2020

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

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

4050
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
4151
return function viteServePublicMiddleware(req, res, next) {
@@ -51,7 +61,7 @@ export function serveStaticMiddleware(
5161
dir: string,
5262
server: ViteDevServer
5363
): Connect.NextHandleFunction {
54-
const serve = sirv(dir, sirvOptions)
64+
const serve = sirv(dir, sirvOptions(server.config.server.headers))
5565

5666
// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
5767
return function viteServeStaticMiddleware(req, res, next) {
@@ -107,7 +117,7 @@ export function serveStaticMiddleware(
107117
export function serveRawFsMiddleware(
108118
server: ViteDevServer
109119
): Connect.NextHandleFunction {
110-
const serveFromRoot = sirv('/', sirvOptions)
120+
const serveFromRoot = sirv('/', sirvOptions(server.config.server.headers))
111121

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

0 commit comments

Comments
 (0)