From d0bbc92f265dba8c2ae1648deb9ce8e8fa24f769 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Sun, 15 Sep 2024 11:31:27 +0900 Subject: [PATCH] feat(serve-static): add `onFound` option (#198) --- README.md | 16 ++++++++++++++++ src/serve-static.ts | 8 +++++--- test/serve-static.test.ts | 11 ++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 75e58de..71d6dec 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,22 @@ app.use( ) ``` +#### `onFound` + +You can specify handling when the requested file is found with `onFound`. + +```ts +app.get( + '/static/*', + serveStatic({ + // ... + onFound: (_path, c) => { + c.header('Cache-Control', `public, immutable, max-age=31536000`) + }, + }) +) +``` + #### `onNotFound` The `onNotFound` is useful for debugging. You can write a handle for when a file is not found. diff --git a/src/serve-static.ts b/src/serve-static.ts index 075b75b..8bfd5e5 100644 --- a/src/serve-static.ts +++ b/src/serve-static.ts @@ -1,10 +1,10 @@ -import type { Context, MiddlewareHandler } from 'hono' +import type { Context, Env, MiddlewareHandler } from 'hono' import { getFilePath, getFilePathWithoutDefaultDocument } from 'hono/utils/filepath' import { getMimeType } from 'hono/utils/mime' import { createReadStream, lstatSync } from 'fs' import type { ReadStream, Stats } from 'fs' -export type ServeStaticOptions = { +export type ServeStaticOptions = { /** * Root path, relative to current working directory from which the app was started. Absolute paths are not supported. */ @@ -12,7 +12,8 @@ export type ServeStaticOptions = { path?: string index?: string // default is 'index.html' rewriteRequestPath?: (path: string) => string - onNotFound?: (path: string, c: Context) => void | Promise + onFound?: (path: string, c: Context) => void | Promise + onNotFound?: (path: string, c: Context) => void | Promise } const createStreamBody = (stream: ReadStream) => { @@ -87,6 +88,7 @@ export const serveStatic = (options: ServeStaticOptions = { root: '' }): Middlew await options.onNotFound?.(path, c) return next() } + await options.onFound?.(path, c) const mimeType = getMimeType(path) if (mimeType) { diff --git a/test/serve-static.test.ts b/test/serve-static.test.ts index 511ec77..5d3e507 100644 --- a/test/serve-static.test.ts +++ b/test/serve-static.test.ts @@ -6,7 +6,15 @@ import { createAdaptorServer } from './../src/server' describe('Serve Static Middleware', () => { const app = new Hono() - app.use('/static/*', serveStatic({ root: './test/assets' })) + app.use( + '/static/*', + serveStatic({ + root: './test/assets', + onFound: (path, c) => { + c.header('X-Custom', `Found the file at ${path}`) + }, + }) + ) app.use('/favicon.ico', serveStatic({ path: './test/assets/favicon.ico' })) app.use( '/dot-static/*', @@ -34,6 +42,7 @@ describe('Serve Static Middleware', () => { expect(res.status).toBe(200) expect(res.text).toBe('

Hello Hono

') expect(res.headers['content-type']).toBe('text/html; charset=utf-8') + expect(res.headers['x-custom']).toBe('Found the file at ./test/assets/static/index.html') }) it('Should return hono.html', async () => {