Skip to content

Commit

Permalink
fix: backport respect server.headers in static middlewares (#8481)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Jun 7, 2022
1 parent ced0374 commit ab7dc1c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
4 changes: 3 additions & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,9 @@ export async function createServer(
// this applies before the transform middleware so that these files are served
// as-is without transforms.
if (config.publicDir) {
middlewares.use(servePublicMiddleware(config.publicDir))
middlewares.use(
servePublicMiddleware(config.publicDir, config.server.headers)
)
}

// main transform middleware
Expand Down
44 changes: 27 additions & 17 deletions packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import type { ServerResponse } from 'http'
import type { OutgoingHttpHeaders, ServerResponse } from 'http'
import type { Options } from 'sirv'
import sirv from 'sirv'
import type { Connect } from 'types/connect'
Expand All @@ -18,24 +18,34 @@ import {
slash
} from '../../utils'

const sirvOptions: Options = {
dev: true,
etag: true,
extensions: [],
setHeaders(res, pathname) {
// Matches js, jsx, ts, tsx.
// The reason this is done, is that the .ts file extension is reserved
// for the MIME type video/mp2t. In almost all cases, we can expect
// these files to be TypeScript files, and for Vite to serve them with
// this Content-Type.
if (/\.[tj]sx?$/.test(pathname)) {
res.setHeader('Content-Type', 'application/javascript')
const sirvOptions = (headers?: OutgoingHttpHeaders): Options => {
return {
dev: true,
etag: true,
extensions: [],
setHeaders(res, pathname) {
// Matches js, jsx, ts, tsx.
// The reason this is done, is that the .ts file extension is reserved
// for the MIME type video/mp2t. In almost all cases, we can expect
// these files to be TypeScript files, and for Vite to serve them with
// this Content-Type.
if (/\.[tj]sx?$/.test(pathname)) {
res.setHeader('Content-Type', 'application/javascript')
}
if (headers) {
for (const name in headers) {
res.setHeader(name, headers[name]!)
}
}
}
}
}

export function servePublicMiddleware(dir: string): Connect.NextHandleFunction {
const serve = sirv(dir, sirvOptions)
export function servePublicMiddleware(
dir: string,
headers?: OutgoingHttpHeaders
): Connect.NextHandleFunction {
const serve = sirv(dir, sirvOptions(headers))

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

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

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

0 comments on commit ab7dc1c

Please sign in to comment.