honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
20.15k stars 575 forks source link

Unable to setup fallback when using parent directory in path #3242

Open staniboy opened 3 months ago

staniboy commented 3 months ago

What version of Hono are you using?

4.5.4

What runtime/platform is your app running on?

Bun

What steps can reproduce the bug?

import { Hono } from "hono";
import { expensesRoute } from "./routes/expenses";
import { serveStatic } from "hono/bun";

const app = new Hono();

app.route("/api/expenses", expensesRoute);

app.get("*", serveStatic({ root: "../client/dist" }));
app.get("*", serveStatic({ path: "../client/dist/index.html" })); // fallback

export default app;

This will serve the root from client/dist and display index.html but will fail to trigger fallback to index.html when non existing path is specified.

I've then moved my client build inside of server project folder and used:

app.get("*", serveStatic({ root: "./public" }));
app.get("*", serveStatic({ path: "./public/index.html" })); // fallback

in this case fallback worked properly.

What is the expected behavior?

Fallback path with parent directory working

What do you see instead?

Fallback path with parent directory not working

Additional information

No response

rjoydip commented 3 months ago

@staniboy You can check https://github.com/honojs/hono/pull/3108#issuecomment-2225636805 and PR https://github.com/honojs/hono/pull/3108, https://github.com/honojs/node-server/pull/78

For workaround, you can do something like below

// for client
app.get("*", serveStatic({ root: `${relative(process.cwd(), __dirname)}/../client/dist/` }));
app.get("*", serveStatic({ path: `${relative(process.cwd(), __dirname)}/client/dist/index.html` })); // fallback 

// for public
app.get("*", serveStatic({ root: `${relative(process.cwd(), __dirname)}/public/` }));
app.get("*", serveStatic({ path: `${relative(process.cwd(), __dirname)}/public/index.html` })); // fallback

If you use ES modules then instead of using __dirname you can use import.meta.dirname

Duplicate #2200

yusukebe commented 2 months ago

Regarding this problem, it's not an actual bug, but we have to consider to solve it.

yusukebe commented 1 month ago

Related to https://github.com/honojs/hono/pull/3420

github-actions[bot] commented 2 days ago

This issue has been marked as stale due to inactivity.