Skip to content
Merged
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
11 changes: 7 additions & 4 deletions src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type ServeStaticOptions<E extends Env = Env> = {
path?: string
index?: string // default is 'index.html'
precompressed?: boolean
rewriteRequestPath?: (path: string) => string
rewriteRequestPath?: (path: string, c: Context<E>) => string
onFound?: (path: string, c: Context<E>) => void | Promise<void>
onNotFound?: (path: string, c: Context<E>) => void | Promise<void>
}
Expand Down Expand Up @@ -56,7 +56,10 @@ const getStats = (path: string) => {
return stats
}

export const serveStatic = (options: ServeStaticOptions = { root: '' }): MiddlewareHandler => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const serveStatic = <E extends Env = any>(
options: ServeStaticOptions<E> = { root: '' }
): MiddlewareHandler<E> => {
return async (c, next) => {
// Do nothing if Response is already set
if (c.finalized) {
Expand All @@ -73,7 +76,7 @@ export const serveStatic = (options: ServeStaticOptions = { root: '' }): Middlew
}

let path = getFilePathWithoutDefaultDocument({
filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,
filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename, c) : filename,
root: options.root,
})

Expand All @@ -87,7 +90,7 @@ export const serveStatic = (options: ServeStaticOptions = { root: '' }): Middlew

if (stats && stats.isDirectory()) {
path = getFilePath({
filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename) : filename,
filename: options.rewriteRequestPath ? options.rewriteRequestPath(filename, c) : filename,
root: options.root,
defaultDocument: options.index ?? 'index.html',
})
Expand Down
1 change: 1 addition & 0 deletions test/assets/static-with-context-path/plain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is plain.txt
27 changes: 26 additions & 1 deletion test/serve-static.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Hono } from 'hono'

import request from 'supertest'
import { serveStatic } from './../src/serve-static'
import { createAdaptorServer } from './../src/server'

describe('Serve Static Middleware', () => {
const app = new Hono()
const app = new Hono<{
Variables: {
path: string
}
}>()

app.use(
'/static/*',
Expand All @@ -24,6 +29,19 @@ describe('Serve Static Middleware', () => {
})
)

app.use(
'/static-with-context-path-route/*',
async (c, next) => {
c.set('path', '/static-with-context-path')
await next()
},
serveStatic({
root: './test/assets',
rewriteRequestPath: (path, c) =>
path.replace('static-with-context-path-route', c.get('path')),
})
)

let notFoundMessage = ''
app.use(
'/on-not-found/*',
Expand Down Expand Up @@ -103,6 +121,13 @@ describe('Serve Static Middleware', () => {
expect(res.status).toBe(404)
})

it('Should return 200 with rewriteRequestPath with the context', async () => {
const res = await request(server).get('/static-with-context-path-route/plain.txt')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toBe('text/plain; charset=utf-8')
expect(res.text).toBe('This is plain.txt')
})

it('Should return 200 response to HEAD request', async () => {
const res = await request(server).head('/static/plain.txt')
expect(res.status).toBe(200)
Expand Down