This commit is contained in:
Zack Radisic
2025-08-20 15:07:39 -07:00
parent 33447ef2db
commit 3906407e5d
2 changed files with 15 additions and 7 deletions

View File

@@ -137,18 +137,22 @@ export async function render(
...responseOptions,
});
} else {
// TODO: this seems shitty
// TODO: this is bad and could be done way better
// Buffer the entire response and return it all at once
const htmlStream = renderToHtml(rscPayload, meta.modules, signal);
const chunks: Uint8Array[] = [];
const reader = htmlStream.getReader();
try {
while (true) {
let keepGoing = true;
do {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
}
keepGoing = !done;
if (!done) {
chunks.push(value);
}
} while (keepGoing);
} finally {
reader.releaseLock();
}

View File

@@ -13,7 +13,7 @@ if (typeof IS_BUN_DEVELOPMENT !== "boolean") {
export type RequestContext = {
responseOptions: ResponseInit;
streamingStarted?: boolean;
abortNonStreaming: (path: string, params: Record<string, string> | null) => {};
abortNonStreaming?: (path: string, params: Record<string, string> | null) => {};
};
// Create the AsyncLocalStorage instance for propagating response options
@@ -81,9 +81,13 @@ server_exports = {
requestWithCookies.cookies = req.cookies || new Bun.CookieMap(req.headers.get("Cookie") || "");
}
let storeValue: RequestContext = {
responseOptions: {},
};
// Run the renderer inside the AsyncLocalStorage context
// This allows Response constructors to access the stored options
const response = await responseOptionsALS.run({}, async () => {
const response = await responseOptionsALS.run(storeValue, async () => {
return await serverRenderer(
requestWithCookies,
{