Files
bunext/dist/functions/server/web-pages/handle-web-pages.js
T
tbenandClaude Opus 5.5 4d4a79a6dd Push HMR updates before the SSR rebuild in dev
Client-only edits are sent to the browser right after the hydration build,
without re-running server functions; SSR rebuilds afterwards and props are
re-fetched only when a .server.ts output changed or after fullRebuild. Page
requests wait for an in-flight SSR rebuild. The SSR build no longer runs
Tailwind, the PostCSS processor is reused across rebuilds, and the watcher
debounce drops from 200ms to 50ms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 07:32:33 +01:00

65 lines
2.5 KiB
JavaScript

import isDevelopment from "../../../utils/is-development";
import { log } from "../../../utils/log";
import { logBuildErrorsOnce } from "../../../utils/build-error-log";
import getCache from "../../cache/get-cache";
import generateWebPageResponseFromComponentReturn from "./generate-web-page-response-from-component-return";
import grabPageComponent from "./grab-page-component";
import grabPageErrorComponent from "./grab-page-error-component";
import { awaitSSRRebuild } from "../../bundler/pages-ssr-context-bundler";
export default async function handleWebPages({ req, }) {
try {
const is_dev = isDevelopment();
if (!is_dev) {
const url = new URL(req.url);
const key = url.pathname + (url.search || "");
const existing_cache = getCache({ key, paradigm: "html" });
if (existing_cache) {
const res_opts = {
headers: {
"Content-Type": "text/html",
"X-Bunext-Cache": "HIT",
},
};
return new Response(existing_cache, res_opts);
}
}
// The dev server port opens before the one-time startup build
// finishes. First page render waits for it so we never serve HTML
// against half-built hydration/SSR modules.
if (is_dev && global.BUNEXT_INITIAL_BUILD_PROMISE) {
try {
await global.BUNEXT_INITIAL_BUILD_PROMISE;
}
catch (error) { }
}
// HMR pushes client updates before the SSR rebuild finishes; a page
// load in that window waits so SSR HTML matches the new bundle.
if (is_dev) {
await awaitSSRRebuild();
}
const componentRes = await grabPageComponent({
req,
});
if (componentRes instanceof Response) {
return componentRes;
}
return await generateWebPageResponseFromComponentReturn({
...componentRes,
});
}
catch (error) {
logBuildErrorsOnce([
{
text: `Error Handling Web Page: ${error?.message || String(error)}`,
},
], (line) => log.error(line));
const componentRes = await grabPageErrorComponent({
error,
});
if (componentRes instanceof Response) {
return componentRes;
}
return await generateWebPageResponseFromComponentReturn(componentRes);
}
}