Bugfix: Refactor SSR bundler. Fix stale cache SSR modules.

This commit is contained in:
2026-04-09 23:50:58 +01:00
parent 40fc7778a8
commit 7fb1784b95
28 changed files with 434 additions and 85 deletions
@@ -2,6 +2,7 @@ import { type Plugin } from "esbuild";
import type { PageFiles } from "../../../types";
import { log } from "../../../utils/log";
import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-result";
import pagesSSRContextBundler from "../pages-ssr-context-bundler";
let buildStart = 0;
let build_starts = 0;
@@ -37,6 +38,9 @@ export default function esbuildCTXArtifactTracker({
global.RECOMPILING = false;
global.IS_SERVER_COMPONENT = false;
await global.SSR_BUNDLER_CTX?.dispose();
global.SSR_BUNDLER_CTX = undefined;
await global.BUNDLER_CTX?.dispose();
global.BUNDLER_CTX = undefined;
}
@@ -87,11 +91,6 @@ export default function esbuildCTXArtifactTracker({
}
post_build_fn?.({ artifacts });
// writeFileSync(
// HYDRATION_DST_DIR_MAP_JSON_FILE,
// JSON.stringify(artifacts, null, 4),
// );
}
const elapsed = (performance.now() - buildStart).toFixed(0);
@@ -101,6 +100,12 @@ export default function esbuildCTXArtifactTracker({
global.IS_SERVER_COMPONENT = false;
build_starts = 0;
if (global.SSR_BUNDLER_CTX) {
global.SSR_BUNDLER_CTX.rebuild();
} else {
pagesSSRContextBundler();
}
});
},
};
@@ -0,0 +1,67 @@
import { type Plugin } from "esbuild";
import type { PageFiles } from "../../../types";
import { log } from "../../../utils/log";
import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-result";
let buildStart = 0;
let build_starts = 0;
const MAX_BUILD_STARTS = 2;
type Params = {
entryToPage: Map<
string,
PageFiles & {
tsx: string;
}
>;
post_build_fn?: (params: { artifacts: any[] }) => Promise<void> | void;
};
export default function ssrCTXArtifactTracker({
entryToPage,
post_build_fn,
}: Params) {
const artifactTracker: Plugin = {
name: "ssr-artifact-tracker",
setup(build) {
build.onStart(async () => {
build_starts++;
buildStart = performance.now();
if (build_starts == MAX_BUILD_STARTS) {
// const error_msg = `SSR Build Failed. Please check all your components and imports.`;
// log.error(error_msg);
}
});
build.onEnd((result) => {
if (result.errors.length > 0) {
return;
}
const artifacts = grabArtifactsFromBundledResults({
result,
entryToPage,
virtual_match: `ssr-virtual`,
});
if (artifacts?.[0] && artifacts.length > 0) {
for (let i = 0; i < artifacts.length; i++) {
const artifact = artifacts[i];
if (
artifact?.local_path &&
global.SSR_BUNDLER_CTX_MAP
) {
global.SSR_BUNDLER_CTX_MAP[artifact.local_path] =
artifact;
}
}
post_build_fn?.({ artifacts });
}
});
},
};
return artifactTracker;
}
@@ -0,0 +1,43 @@
import type { Plugin } from "esbuild";
import path from "path";
import type { PageFiles } from "../../../types";
import { log } from "../../../utils/log";
type Params = {
entryToPage: Map<
string,
PageFiles & {
tsx: string;
}
>;
};
export default function ssrVirtualFilesPlugin({ entryToPage }: Params) {
const virtualPlugin: Plugin = {
name: "ssr-virtual-hydration",
setup(build) {
build.onResolve({ filter: /^ssr-virtual:/ }, (args) => {
const final_path = args.path.replace(/ssr-virtual:/, "");
return {
path: final_path,
namespace: "ssr-virtual",
};
});
build.onLoad({ filter: /.*/, namespace: "ssr-virtual" }, (args) => {
const target = entryToPage.get(args.path);
if (!target?.tsx) return null;
const contents = target.tsx;
return {
contents: contents || "",
loader: "tsx",
resolveDir: path.dirname(target.local_path),
};
});
},
};
return virtualPlugin;
}