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]>
This commit is contained in:
2026-10-15 19:37:15 +01:00
co-authored by Claude Opus 5.5
parent 7694e80c60
commit 3d0771f2d0
10 changed files with 51 additions and 19 deletions
@@ -5,6 +5,7 @@ import grabRootFilePath from "./grab-root-file-path";
import path from "path"; import path from "path";
import grabDirNames from "../../../utils/grab-dir-names"; import grabDirNames from "../../../utils/grab-dir-names";
import grabRelativePath from "../../../utils/grab-relative-path"; import grabRelativePath from "../../../utils/grab-relative-path";
import importFresh from "../../../utils/import-fresh";
const { ROOT_DIR } = grabDirNames(); const { ROOT_DIR } = grabDirNames();
export default async function grabPageBundledReactComponent({ file_path, return_tsx_only, }) { export default async function grabPageBundledReactComponent({ file_path, return_tsx_only, }) {
try { try {
@@ -12,7 +13,7 @@ export default async function grabPageBundledReactComponent({ file_path, return_
if (global.BUNEXT_SSR_BUNDLER_CTX_MAP?.[relative_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); const abs = path.join(ROOT_DIR, global.BUNEXT_SSR_BUNDLER_CTX_MAP[relative_path].path);
// Loader.registry.delete(abs); // Loader.registry.delete(abs);
const mod = await import(`${abs}?t=${Date.now()}`); const mod = await importFresh(abs);
const Main = mod.default; const Main = mod.default;
return { return {
component: Main, component: Main,
@@ -6,9 +6,9 @@ import grabPageServerPath from "./grab-page-server-path";
import path from "path"; import path from "path";
import grabDirNames from "../../../utils/grab-dir-names"; import grabDirNames from "../../../utils/grab-dir-names";
import grabRelativePath from "../../../utils/grab-relative-path"; import grabRelativePath from "../../../utils/grab-relative-path";
import importFresh from "../../../utils/import-fresh";
const { ROOT_DIR } = grabDirNames(); const { ROOT_DIR } = grabDirNames();
export default async function grabPageCombinedServerRes({ file_path, debug, url, query, routeParams, }) { export default async function grabPageCombinedServerRes({ file_path, debug, url, query, routeParams, }) {
const now = Date.now();
const { root_file_path } = grabRootFilePath(); const { root_file_path } = grabRootFilePath();
const { server_file_path: root_server_file_path } = root_file_path const { server_file_path: root_server_file_path } = root_file_path
? grabPageServerPath({ file_path: root_file_path }) ? grabPageServerPath({ file_path: root_file_path })
@@ -23,7 +23,7 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url,
// Loader.registry.delete(final_root_server_path); // Loader.registry.delete(final_root_server_path);
} }
const root_server_module = final_root_server_path const root_server_module = final_root_server_path
? await import(`${final_root_server_path}?t=${now}`) ? await importFresh(final_root_server_path)
: undefined; : undefined;
const root_server_fn = root_server_module?.default || root_server_module?.server; const root_server_fn = root_server_module?.default || root_server_module?.server;
const rootServerRes = await grabPageServerRes({ const rootServerRes = await grabPageServerRes({
@@ -46,7 +46,7 @@ export default async function grabPageCombinedServerRes({ file_path, debug, url,
// Loader.registry.delete(final_page_server_path); // Loader.registry.delete(final_page_server_path);
} }
const server_module = final_page_server_path const server_module = final_page_server_path
? await import(`${final_page_server_path}?t=${now}`) ? await importFresh(final_page_server_path)
: undefined; : undefined;
const server_fn = server_module?.default || server_module?.server; const server_fn = server_module?.default || server_module?.server;
const serverRes = await grabPageServerRes({ const serverRes = await grabPageServerRes({
+3 -3
View File
@@ -3,8 +3,8 @@ import _ from "lodash";
import { log } from "../../../utils/log"; import { log } from "../../../utils/log";
import grabRootFilePath from "./grab-root-file-path"; import grabRootFilePath from "./grab-root-file-path";
import grabPageCombinedServerRes from "./grab-page-combined-server-res"; import grabPageCombinedServerRes from "./grab-page-combined-server-res";
import importFresh from "../../../utils/import-fresh";
export default async function grabPageModules({ file_path, debug, url, query, routeParams, skip_server_res, }) { export default async function grabPageModules({ file_path, debug, url, query, routeParams, skip_server_res, }) {
const now = Date.now();
const { serverRes } = skip_server_res const { serverRes } = skip_server_res
? {} ? {}
: await grabPageCombinedServerRes({ : await grabPageCombinedServerRes({
@@ -33,8 +33,8 @@ export default async function grabPageModules({ file_path, debug, url, query, ro
const { root_file_path } = grabRootFilePath(); const { root_file_path } = grabRootFilePath();
const root_module = !root_file_path const root_module = !root_file_path
? undefined ? undefined
: bundled_root_module || (await import(`${root_file_path}?t=${now}`)); : bundled_root_module || (await importFresh(root_file_path));
const module = page_module || (await import(`${file_path}?t=${now}`)); const module = page_module || (await importFresh(file_path));
if (debug) { if (debug) {
log.info(`module:`, module); log.info(`module:`, module);
} }
+7
View File
@@ -0,0 +1,7 @@
/**
* Dev: bypass Bun's module registry so rebuilt files are picked up.
* Prod: import by stable path so each module is loaded (and kept) once.
* A unique `?t=` per call creates a new, never-evicted module record, so
* using it in production leaks memory on every request.
*/
export default function importFresh<T = any>(file_path: string): Promise<T>;
+10
View File
@@ -0,0 +1,10 @@
import isDevelopment from "./is-development";
/**
* Dev: bypass Bun's module registry so rebuilt files are picked up.
* Prod: import by stable path so each module is loaded (and kept) once.
* A unique `?t=` per call creates a new, never-evicted module record, so
* using it in production leaks memory on every request.
*/
export default function importFresh(file_path) {
return import(isDevelopment() ? `${file_path}?t=${Date.now()}` : file_path);
}
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/bunext", "name": "@moduletrace/bunext",
"version": "1.2.3", "version": "1.2.4",
"main": "dist/index.js", "main": "dist/index.js",
"module": "index.ts", "module": "index.ts",
"dependencies": { "dependencies": {
@@ -7,6 +7,7 @@ import grabRootFilePath from "./grab-root-file-path";
import path from "path"; import path from "path";
import grabDirNames from "../../../utils/grab-dir-names"; import grabDirNames from "../../../utils/grab-dir-names";
import grabRelativePath from "../../../utils/grab-relative-path"; import grabRelativePath from "../../../utils/grab-relative-path";
import importFresh from "../../../utils/import-fresh";
const { ROOT_DIR } = grabDirNames(); const { ROOT_DIR } = grabDirNames();
@@ -28,7 +29,7 @@ export default async function grabPageBundledReactComponent({
global.BUNEXT_SSR_BUNDLER_CTX_MAP[relative_path].path, global.BUNEXT_SSR_BUNDLER_CTX_MAP[relative_path].path,
); );
// Loader.registry.delete(abs); // Loader.registry.delete(abs);
const mod = await import(`${abs}?t=${Date.now()}`); const mod = await importFresh(abs);
const Main = mod.default as FC; const Main = mod.default as FC;
@@ -11,6 +11,7 @@ import grabPageServerPath from "./grab-page-server-path";
import path from "path"; import path from "path";
import grabDirNames from "../../../utils/grab-dir-names"; import grabDirNames from "../../../utils/grab-dir-names";
import grabRelativePath from "../../../utils/grab-relative-path"; import grabRelativePath from "../../../utils/grab-relative-path";
import importFresh from "../../../utils/import-fresh";
const { ROOT_DIR } = grabDirNames(); const { ROOT_DIR } = grabDirNames();
@@ -29,8 +30,6 @@ export default async function grabPageCombinedServerRes({
query, query,
routeParams, routeParams,
}: Params) { }: Params) {
const now = Date.now();
const { root_file_path } = grabRootFilePath(); const { root_file_path } = grabRootFilePath();
const { server_file_path: root_server_file_path } = root_file_path const { server_file_path: root_server_file_path } = root_file_path
? grabPageServerPath({ file_path: root_file_path }) ? grabPageServerPath({ file_path: root_file_path })
@@ -48,8 +47,9 @@ export default async function grabPageCombinedServerRes({
if (final_root_server_path) { if (final_root_server_path) {
// Loader.registry.delete(final_root_server_path); // Loader.registry.delete(final_root_server_path);
} }
const root_server_module: BunextPageServerModule = final_root_server_path const root_server_module: BunextPageServerModule | undefined =
? await import(`${final_root_server_path}?t=${now}`) final_root_server_path
? await importFresh(final_root_server_path)
: undefined; : undefined;
const root_server_fn = const root_server_fn =
@@ -78,8 +78,9 @@ export default async function grabPageCombinedServerRes({
if (final_page_server_path) { if (final_page_server_path) {
// Loader.registry.delete(final_page_server_path); // Loader.registry.delete(final_page_server_path);
} }
const server_module: BunextPageServerModule = final_page_server_path const server_module: BunextPageServerModule | undefined =
? await import(`${final_page_server_path}?t=${now}`) final_page_server_path
? await importFresh(final_page_server_path)
: undefined; : undefined;
const server_fn = server_module?.default || server_module?.server; const server_fn = server_module?.default || server_module?.server;
@@ -10,6 +10,7 @@ import { log } from "../../../utils/log";
import grabRootFilePath from "./grab-root-file-path"; import grabRootFilePath from "./grab-root-file-path";
import grabPageCombinedServerRes from "./grab-page-combined-server-res"; import grabPageCombinedServerRes from "./grab-page-combined-server-res";
import type { FC } from "react"; import type { FC } from "react";
import importFresh from "../../../utils/import-fresh";
type Params = { type Params = {
file_path: string; file_path: string;
@@ -35,8 +36,6 @@ export default async function grabPageModules({
routeParams, routeParams,
skip_server_res, skip_server_res,
}: Params): Promise<Return | Response> { }: Params): Promise<Return | Response> {
const now = Date.now();
const { serverRes } = skip_server_res const { serverRes } = skip_server_res
? {} ? {}
: await grabPageCombinedServerRes({ : await grabPageCombinedServerRes({
@@ -77,10 +76,10 @@ export default async function grabPageModules({
const { root_file_path } = grabRootFilePath(); const { root_file_path } = grabRootFilePath();
const root_module: BunextRootModule | undefined = !root_file_path const root_module: BunextRootModule | undefined = !root_file_path
? undefined ? undefined
: bundled_root_module || (await import(`${root_file_path}?t=${now}`)); : bundled_root_module || (await importFresh(root_file_path));
const module: BunextPageModule = const module: BunextPageModule =
page_module || (await import(`${file_path}?t=${now}`)); page_module || (await importFresh(file_path));
if (debug) { if (debug) {
log.info(`module:`, module); log.info(`module:`, module);
+13
View File
@@ -0,0 +1,13 @@
import isDevelopment from "./is-development";
/**
* Dev: bypass Bun's module registry so rebuilt files are picked up.
* Prod: import by stable path so each module is loaded (and kept) once.
* A unique `?t=` per call creates a new, never-evicted module record, so
* using it in production leaks memory on every request.
*/
export default function importFresh<T = any>(file_path: string): Promise<T> {
return import(
isDevelopment() ? `${file_path}?t=${Date.now()}` : file_path
);
}