From 7281dfd0ed19c49361fb9634751e76dcba18f751 Mon Sep 17 00:00:00 2001 From: RiskyMH Date: Wed, 6 Aug 2025 20:12:35 +1000 Subject: [PATCH] cache less here it does add some inefficiency, but that can be dealt with later --- src/bun.js/api/server.zig | 26 ++++---- .../bun/http/bun-serve-html-response.test.ts | 62 +++++++++++++++++++ 2 files changed, 73 insertions(+), 15 deletions(-) diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 794bee1a14..95c8a3d561 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -203,22 +203,9 @@ pub const AnyRoute = union(enum) { const bytes = response.body.value.InternalBlob.bytes.items; if (bytes.len == @sizeOf(*HTMLBundle)) { const html_bundle_ptr = @as(**HTMLBundle, @ptrCast(@alignCast(bytes.ptr))).*; - const entry = init_ctx.dedupe_html_bundle_map.getOrPut(html_bundle_ptr) catch bun.outOfMemory(); - if (!entry.found_existing) { - entry.value_ptr.* = HTMLBundle.Route.init(html_bundle_ptr); - const route = entry.value_ptr.*; + const needs_custom = response.init.headers != null or response.statusCode() != 200; - if (response.init.headers) |headers| { - route.data.custom_headers = bun.http.Headers.from(headers, bun.default_allocator, .{}) catch bun.outOfMemory(); - } - - const status = response.statusCode(); - if (status != 200) { - route.data.custom_status = status; - } - - return .{ .html = route }; - } else { + if (needs_custom) { var route = HTMLBundle.Route.init(html_bundle_ptr); if (response.init.headers) |headers| { @@ -231,6 +218,15 @@ pub const AnyRoute = union(enum) { } return .{ .html = route }; + } else { + const entry = init_ctx.dedupe_html_bundle_map.getOrPut(html_bundle_ptr) catch bun.outOfMemory(); + + if (!entry.found_existing) { + entry.value_ptr.* = HTMLBundle.Route.init(html_bundle_ptr); + return .{ .html = entry.value_ptr.* }; + } else { + return .{ .html = entry.value_ptr.dupeRef() }; + } } } } diff --git a/test/js/bun/http/bun-serve-html-response.test.ts b/test/js/bun/http/bun-serve-html-response.test.ts index 6f150c5231..28a4e11d59 100644 --- a/test/js/bun/http/bun-serve-html-response.test.ts +++ b/test/js/bun/http/bun-serve-html-response.test.ts @@ -99,6 +99,12 @@ const server = Bun.serve({ } }), "/home": new Response(html), + "/haha": new Response(html, {status: 400}), + "/index.html": html, + "/tea": { + GET: new Response(html, {status: 418}), + POST: () => new Response("Teapot!!!"), + }, "/hello": new Response(hello), "/*": new Response(html, { status: 404, @@ -149,6 +155,62 @@ console.log(server.port); expect(text).toMatch(/src="[^"]+\.js"/); } + { + const response = await fetch(`http://localhost:${port}/index.html`); + + expect(response.status).toBe(200); + expect(response.headers.get("X-Custom")).not.toBe("custom-value"); + expect(response.headers.get("X-Test")).not.toBe("test-value"); + expect(response.headers.get("Content-Type")).toBe("text/html;charset=utf-8"); + + const text = await response.text(); + expect(text).toContain("Test Page"); + expect(text).toContain("Hello from HTMLBundle"); + expect(text).toMatch(/src="[^"]+\.js"/); + } + + { + const response = await fetch(`http://localhost:${port}/haha`); + expect(response.status).toBe(400); + expect(response.headers.get("X-Custom")).not.toBe("custom-value"); + expect(response.headers.get("X-Test")).not.toBe("test-value"); + expect(response.headers.get("Content-Type")).toBe("text/html;charset=utf-8"); + + const text = await response.text(); + expect(text).toContain("Test Page"); + expect(text).toContain("Hello from HTMLBundle"); + expect(text).toMatch(/src="[^"]+\.js"/); + } + + { + const response = await fetch(`http://localhost:${port}/tea`, { + method: "GET", + }); + + expect(response.status).toBe(418); + expect(response.headers.get("X-Custom")).not.toBe("custom-value"); + expect(response.headers.get("X-Test")).not.toBe("test-value"); + expect(response.headers.get("Content-Type")).toBe("text/html;charset=utf-8"); + + const text = await response.text(); + expect(text).toContain("Test Page"); + expect(text).toContain("Hello from HTMLBundle"); + expect(text).toMatch(/src="[^"]+\.js"/); + } + + { + const response = await fetch(`http://localhost:${port}/tea`, { + method: "POST", + }); + expect(response.status).toBe(200); + expect(response.headers.get("X-Custom")).not.toBe("custom-value"); + expect(response.headers.get("X-Test")).not.toBe("test-value"); + expect(response.headers.get("Content-Type")).toBe("text/plain;charset=utf-8"); + + const text = await response.text(); + expect(text).toBe("Teapot!!!"); + } + { const response = await fetch(`http://localhost:${port}/hello`); expect(response.status).toBe(200);