Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Cloudflare Pages plugin registers serveStatic middleware prematurely #8

Open
bruceharrison1984 opened this issue Sep 24, 2023 · 1 comment

Comments

@bruceharrison1984
Copy link

bruceharrison1984 commented Sep 24, 2023

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.

@bruceharrison1984 bruceharrison1984 changed the title Cloudflare Pages plugin registers route pre-maturely Cloudflare Pages plugin registers middleware prematurely Sep 24, 2023
@bruceharrison1984 bruceharrison1984 changed the title Cloudflare Pages plugin registers middleware prematurely Cloudflare Pages plugin registers serveStatic middleware prematurely Sep 24, 2023
@yusukebe
Copy link
Collaborator

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants