Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

serveStatic's fallthrough option does not function as intended (possible fix included) #904

Open
OmarMAttia7 opened this issue Oct 24, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@OmarMAttia7
Copy link

Environment

  • pnpm 9.10.0
  • NodeJS 20.16.0

Reproduction

import { createApp, defineEventHandler, serveStatic } from "h3";
import { readFile, stat } from "fs/promises";
import { join } from "path";

export const app = createApp();

const publicDir = "public";

app.use(
  "/",
  defineEventHandler(async (event) => {
    return await serveStatic(event, {
      getContents: async (id) => await readFile(join(publicDir, id)),
      getMeta: async (id) => {
        const stats = await stat(join(publicDir, id)).catch(() => undefined);

        if (!stats || !stats.isFile()) {
          return;
        }

        return {
          size: stats.size,
          mtime: stats.mtime,
        };
      },
      fallthrough: true,
    });
  })
);

app.use(
  "/",
  defineEventHandler((event) => {
    return "Hello world";
  })
);

Describe the bug

Adding fallthrough: true option to serveStatic does not pass the request to the next event handler/middleware and returns false which h3 interprets as json.

I think I identified the offending code in src/utils/static.ts, removing it fixes the problem but I don't know if it breaks anything else yet.
on line 24: return false should be return or return undefined
same thing on line 65

Refer to the registering event handlers section in the app instance docs.

I can submit a PR with a test and the fix if needed.

Additional context

No response

Logs

No response

@OmarMAttia7 OmarMAttia7 added the bug Something isn't working label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant