Skip to content

Commit

Permalink
fix(static): serve .js, .jsx, .ts, .tsx as application/javascript
Browse files Browse the repository at this point in the history
This fixes vitejs#2642.
Thanks a lot to @lukeed, maintainer of Sirv, for the suggestion!
lukeed/sirv#103 (comment)
  • Loading branch information
eirslett committed Mar 29, 2021
1 parent 6743e07 commit 9858e03
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import os from 'os'
import path from 'path'
import sirv from 'sirv'
import sirv, { Options } from 'sirv'
import { Connect } from 'types/connect'
import { ResolvedConfig } from '../..'
import { FS_PREFIX } from '../../constants'
import { cleanUrl, isImportRequest } from '../../utils'

const sirvOptions = { dev: true, etag: true, extensions: [] }
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')
}
}
}

export function servePublicMiddleware(dir: string): Connect.NextHandleFunction {
const serve = sirv(dir, sirvOptions)
Expand Down

0 comments on commit 9858e03

Please sign in to comment.