hono icon indicating copy to clipboard operation
hono copied to clipboard

feat: expose serve-static builder

Open cometkim opened this issue 2 years ago • 2 comments

This allows accessing middleware/serve-static from third-party

Use case

I run my own handler based on Cloudflare R2 to avoid the limitations of Cloudflare KV / Pages. And I found that most of the code I needed there was already built into hono.

So what I expect is like:

import { Hono } from 'hono';
import { serveStatic as makeServe } from 'hono/base-serve-static';

const app = new Hono<Env>();

const serveContent = makeServe<Env>({
  getContent: async (path, c) => {
    // retrieve
    let response = await cache.match(c.req.raw);

    // ...get R2 object
    if (!response) {
      let obj = await c.env.CONTENT.get(path);
      if (obj) {
        response = obj.body;
        c.executionCtx.waitUntil(cache.put(c.req.raw, obj.body));
      }
    }

    c.body(response);
  },
});

app.use(serveContent);

export default app;

Author should do the followings, if applicable

  • [ ] Add tests
  • [x] Run tests
  • [x] yarn denoify to generate files for Deno

cometkim avatar Apr 16 '24 09:04 cometkim

Hi @cometkim

This is good! But please wait until I look over the details!

yusukebe avatar Apr 17 '24 22:04 yusukebe

@yusukebe added a type definition rather than any as it would be a public interface.

And made some changes to allow a situation where users needed to create the Response object themselves. (e.g. using with Browser/Cloudflare's Cache API)

cometkim avatar Apr 23 '24 12:04 cometkim

@cometkim Thanks!

yusukebe avatar Apr 30 '24 22:04 yusukebe