sonikjs / sonik

348 stars 9 forks source link

Cloudflare Pages plugin registers `serveStatic` middleware prematurely #8

Open bruceharrison1984 opened 12 months ago

bruceharrison1984 commented 12 months ago

In using the @sonikjs/cloudflare-pages, I noticed that static assets would not be served correctly if I created a new instance of Hono in my route, for purposes of adding additional middleware. This would cause all static assets to return a 404.

Non-working:

const base = new Hono<TorchEnv>();
base.use(
  '*',
  secureHeaders(),
  timing(),
  poweredBy(),
  withDatabase(),
  withServerConfig()
);
const app = createApp({ app: base });
app.showRoutes();
export default app;

Manually registering the serveStatic middleware from [Hono] works:

import { serveStatic } from 'hono/cloudflare-pages';

const base = new Hono<TorchEnv>();

base.use('/static/*', serveStatic()); <-- re-register Hono middleware
base.use(
  '*',
  secureHeaders(),
  timing(),
  poweredBy(),
  withDatabase(),
  withServerConfig()
);

const app = createApp({ app: base });

app.showRoutes();

export default app;

So it seems like the root issue is that the middleware is registered immediately, and if the developer creates a new instance of Hono, this original registration is lost. Not sure what the fix would be, perhaps just making the user register it manually is the easiest path forward, or just noting this in the documentation.

yusukebe commented 11 months ago

You are right. I wasn't aware of this issue.

Not sure what the fix would be, perhaps just making the user register it manually is the easiest path forward, or just noting this in the documentation.

Yeah. I'll think about it and leave this issue open until the next action. Thanks.