Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ServeStaticOptions = {
*/
root?: string
path?: string
mimes?: Record<string, string>
index?: string // default is 'index.html'
rewriteRequestPath?: (path: string) => string
onNotFound?: (path: string, c: Context) => void | Promise<void>
Expand Down Expand Up @@ -60,7 +61,12 @@ export const serveStatic = (options: ServeStaticOptions = { root: '' }): Middlew
return next()
}

const mimeType = getMimeType(path)
let mimeType: string | undefined = undefined
if (options.mimes) {
mimeType = getMimeType(path, options.mimes) ?? getMimeType(path)
} else {
mimeType = getMimeType(path)
}
if (mimeType) {
c.header('Content-Type', mimeType)
}
Expand Down
Empty file.
Empty file.
Empty file.
27 changes: 27 additions & 0 deletions test/serve-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,30 @@ describe('Serve Static Middleware', () => {
expect(res.status).toBe(404)
})
})

describe('With `mimes` options', () => {
const mimes = {
m3u8: 'application/vnd.apple.mpegurl',
ts: 'video/mp2t',
}
const app = new Hono()
app.use('/static/*', serveStatic({ root: './assets', mimes }))

const server = createAdaptorServer(app)

it('Should return content-type of m3u8', async () => {
const res = await request(server).get('/static/video/morning-routine.m3u8')
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('application/vnd.apple.mpegurl')
})
it('Should return content-type of ts', async () => {
const res = await request(server).get('/static/video/morning-routine1.ts1')
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('video/mp2t')
})
it('Should return content-type of default on Hono', async () => {
const res = await request(server).get('/static/video/introduction.mp4')
expect(res.status).toBe(200)
expect(res.headers.get('Content-Type')).toBe('video/mp4')
})
})