hviana / faster

A fast and optimized middleware server with an absurdly small amount of code (300 lines) built on top of Deno's native HTTP APIs with no dependencies. It also has a collection of useful middlewares: log file, serve static, CORS, session, rate limit, token, body parsers, redirect, proxy and handle upload. In "README" there are examples of all the resources. Faster's ideology is: all you need is an optimized middleware manager, all other functionality is middleware.
MIT License
36 stars 8 forks source link

Serve static for both GET and HEAD requests breaks server #3

Closed exside closed 1 year ago

exside commented 2 years ago

Hi, first of all, great work on faster, love it so far!

I'm running into an issue when trying to handle HEAD requests (using them from the frontend to see if i need to fetch the entire file or if i can used the cached version in the browser, e.g. to check if the file was changed) for static files while also serving them statically via GET requests, i have this (played around with the order of the blocks, but no change):

server.get(
    '/www/*',
    serveStatic('./www'),
);

server.head(
    '/www/*',
    serveStatic('./www'),
);

// also tried the following with no change in behaviour
server.get(
    '/www/*',
    serveStatic('./www'),
    async (ctx, next) => {
        await next();
    },
);

server.head(
    '/www/*',
    serveStatic('./www'),
    async (ctx, next) => {
        await next();
    },
);

when the server.head() handler is present, the HEAD requests work fine, but the GET requests stop working and stay as (pending) forever in the network monitor (browser side), if i remove it, HEAD requests return a 404 and the GET requests work as expected. I'm not sure where the issue is, i don't get errors on the server side and neither in the browser, the GET request is just stuck. Any ideas why that could be? maybe using the same middleware twice? but i checked the code, don't see why that would be an issue.

hviana commented 2 years ago

Thanks for reporting the issue. I will be looking for a solution and will fix it as soon as possible.

exside commented 2 years ago

Any ideas where to look? Maybe i can help...

hviana commented 2 years ago

I think it's important to see between lines 218 and 238 of the server.ts file, that's where there might be a problem.

hviana commented 1 year ago

No activity

exside commented 1 year ago

Well, I couldn't figure out what is the problem, but it persists, try it yourself:

['get', 'head'].map((method) => {
    server[method]('/*', serveStatic(`${Deno.cwd()}/src`), async (ctx, next) => {
        // console.log(ctx.req.method, ctx.req.url.match(/\.(?<ext>[^.]*?)(?=\?|#|$)/));
        const ext = ctx.req.url.match(/\.(?<ext>[^.]*?)(?=\?|#|$)/)[1];
        if ( ext ) {
            const stats = await Deno.stat(`${Deno.cwd()}/src${new URL(ctx.req.url).pathname}`);

            ctx.res.headers = new Headers({
                'content-type': mime(ext),
                'content-length': stats.size,
                'last-modified': stats.mtime.toUTCString(),
            });
        }
        // console.log(ctx.res);
        await next();
    });
});