fix: better accept handle for SPAs

This commit is contained in:
Andy Burke 2025-11-20 16:50:36 -08:00
parent 9fc91f4cf4
commit 474d455986
2 changed files with 3 additions and 18 deletions

View file

@ -1,7 +1,7 @@
{
"name": "@andyburke/serverus",
"description": "A flexible HTTP server for mixed content. Throw static files, markdown, Typescript and (hopefully, eventually) more into a directory and serverus can serve it up a bit more like old-school CGI.",
"version": "0.14.0",
"version": "0.14.1",
"license": "MIT",
"exports": {
".": "./serverus.ts",

View file

@ -87,17 +87,9 @@ export const HANDLERS: Partial<Record<HTTP_METHOD, HANDLER_METHOD>> = {
if (index_file_stat.isFile) {
const processed: string = await load_html_with_ssi(index_file_path);
const accepts = request.headers.get('accept') ?? 'text/html';
if (!['*/*', 'text/html', 'text/plain'].includes(accepts)) {
return new Response('unsupported accepts header for SPA: ' + accepts, {
status: 400
});
}
return new Response(processed, {
headers: {
'Content-Type': accepts === '*/*' ? 'text/html' : accepts
'Content-Type': request.headers.get('accept')?.indexOf('text/plain') !== -1 ? 'text/plain' : 'text/html'
}
});
}
@ -111,7 +103,7 @@ export const HANDLERS: Partial<Record<HTTP_METHOD, HANDLER_METHOD>> = {
}
},
OPTIONS: async (request: Request, normalized_path: string): Promise<Response | undefined> => {
OPTIONS: async (_request: Request, normalized_path: string): Promise<Response | undefined> => {
const spa_root = await find_spa_file_root(normalized_path);
if (!spa_root) {
return;
@ -123,13 +115,6 @@ export const HANDLERS: Partial<Record<HTTP_METHOD, HANDLER_METHOD>> = {
const index_file_stat = await Deno.stat(index_file_path);
if (index_file_stat.isFile) {
const accepts = request.headers.get('accept') ?? 'text/html';
if (!['*/*', 'text/html', 'text/plain'].includes(accepts)) {
return new Response('unsupported accepts header for SPA: ' + accepts, {
status: 400
});
}
const allowed = ['GET', 'HEAD', 'OPTIONS'];
return new Response('', {