Files
bunext/dist/functions/server/web-pages/grab-page-bundled-react-component.js
T
tbenandClaude Opus 5.5 3d0771f2d0 Stop per-request module re-imports leaking memory in production
Page rendering imported the SSR bundle and .server.ts modules with a
unique ?t= query on every request, in production too. Each one became a
new, never-evicted module record (and re-ran module top-level code), so
RSS grew with request count. Cache-bust only in dev via importFresh.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 19:37:15 +01:00

52 lines
1.8 KiB
JavaScript

import grabPageReactComponentString from "./grab-page-react-component-string";
import grabTsxStringModule from "./grab-tsx-string-module";
import { log } from "../../../utils/log";
import grabRootFilePath from "./grab-root-file-path";
import path from "path";
import grabDirNames from "../../../utils/grab-dir-names";
import grabRelativePath from "../../../utils/grab-relative-path";
import importFresh from "../../../utils/import-fresh";
const { ROOT_DIR } = grabDirNames();
export default async function grabPageBundledReactComponent({ file_path, return_tsx_only, }) {
try {
const relative_path = grabRelativePath(file_path);
if (global.BUNEXT_SSR_BUNDLER_CTX_MAP?.[relative_path]) {
const abs = path.join(ROOT_DIR, global.BUNEXT_SSR_BUNDLER_CTX_MAP[relative_path].path);
// Loader.registry.delete(abs);
const mod = await importFresh(abs);
const Main = mod.default;
return {
component: Main,
page_module: mod.__bunext_page,
root_module: mod.__bunext_root,
};
}
const { root_file_path } = grabRootFilePath();
let tsx = grabPageReactComponentString({
file_path,
root_file_path,
});
if (!tsx) {
return undefined;
}
if (return_tsx_only) {
return { tsx };
}
const mod = await grabTsxStringModule({
tsx,
page_file_path: file_path,
});
const Main = mod.default;
return {
component: Main,
tsx,
page_module: mod.__bunext_page,
root_module: mod.__bunext_root,
};
}
catch (error) {
log.error(`grabPageBundledReactComponent Error: ${error.message}`, error);
return undefined;
}
}