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

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

Closed
exside opened this issue May 16, 2022 · 5 comments
Closed

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

exside opened this issue May 16, 2022 · 5 comments

Comments

@exside
Copy link
Contributor

exside commented May 16, 2022

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
Copy link
Owner

hviana commented May 16, 2022

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

@exside
Copy link
Contributor Author

exside commented May 16, 2022

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

@hviana
Copy link
Owner

hviana commented May 16, 2022

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 hviana closed this as completed Dec 19, 2022
@hviana
Copy link
Owner

hviana commented Dec 19, 2022

No activity

@exside
Copy link
Contributor Author

exside commented Mar 22, 2023

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

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

No branches or pull requests

2 participants