faster icon indicating copy to clipboard operation
faster copied to clipboard

Serve static for both GET and HEAD requests breaks server

Open exside opened this issue 2 years ago • 3 comments

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.

exside avatar May 16 '22 14:05 exside

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

hviana avatar May 16 '22 15:05 hviana

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

exside avatar May 16 '22 15:05 exside

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 avatar May 16 '22 16:05 hviana

No activity

hviana avatar Dec 19 '22 20:12 hviana

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();
	});
});

exside avatar Mar 22 '23 02:03 exside