diff --git a/src/middleware/serve-static/index.test.ts b/src/middleware/serve-static/index.test.ts index 249e1301..b0bd1e3e 100644 --- a/src/middleware/serve-static/index.test.ts +++ b/src/middleware/serve-static/index.test.ts @@ -18,6 +18,11 @@ describe('Serve Static Middleware', () => { isDir: (path) => { return path === 'static/hello.world' }, + onFound: (path, c) => { + if (path.endsWith('hello.html')) { + c.header('X-Custom', `Found the file at ${path}`) + } + }, }) app.get('/static/*', serveStatic) @@ -32,6 +37,7 @@ describe('Serve Static Middleware', () => { expect(res.headers.get('Content-Encoding')).toBeNull() expect(res.headers.get('Content-Type')).toMatch(/^text\/html/) expect(await res.text()).toBe('Hello in ./static/hello.html') + expect(res.headers.get('X-Custom')).toBe('Found the file at ./static/hello.html') }) it('Should return 200 response - /static/sub', async () => { diff --git a/src/middleware/serve-static/index.ts b/src/middleware/serve-static/index.ts index 5e7d3af6..56c601da 100644 --- a/src/middleware/serve-static/index.ts +++ b/src/middleware/serve-static/index.ts @@ -14,6 +14,7 @@ export type ServeStaticOptions = { precompressed?: boolean mimes?: Record rewriteRequestPath?: (path: string) => string + onFound?: (path: string, c: Context) => void | Promise onNotFound?: (path: string, c: Context) => void | Promise } @@ -128,7 +129,7 @@ export const serveStatic = ( } } } - + await options.onFound?.(path, c) return c.body(content) }