Add retries to page component function error. Reload for all 404 pages

This commit is contained in:
Benjamin Toby 2026-04-11 08:48:01 +01:00
parent 5a0972beb8
commit af8c207ac1
18 changed files with 293 additions and 238 deletions

View File

@ -9,6 +9,7 @@ import virtualFilesPlugin from "./plugins/virtual-files-plugin";
import esbuildCTXArtifactTracker from "./plugins/esbuild-ctx-artifact-tracker";
const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames();
export default async function allPagesESBuildContextBundler(params) {
try {
const pages = grabAllPages({ exclude_api: true });
global.PAGE_FILES = pages;
const dev = isDevelopment();
@ -60,6 +61,10 @@ export default async function allPagesESBuildContextBundler(params) {
],
});
await global.BUNDLER_CTX.rebuild();
}
catch (error) {
console.log(`ESBUILD Error =>`, error);
}
}
function forceExternalReact() {
return {

View File

@ -40,5 +40,7 @@ declare global {
css: string;
}>;
var BUNDLER_CTX_DISPOSED: boolean | undefined;
var REBUILD_RETRIES: number;
var IS_404_PAGE: boolean;
}
export default function bunextInit(): Promise<void>;

View File

@ -15,6 +15,7 @@ export default async function bunextInit() {
global.BUNDLER_CTX_MAP = {};
global.SSR_BUNDLER_CTX_MAP = {};
global.BUNDLER_REBUILDS = 0;
global.REBUILD_RETRIES = 0;
global.PAGE_FILES = [];
global.SKIPPED_BROWSER_MODULES = new Set();
global.DIR_NAMES = dirNames;

View File

@ -0,0 +1,3 @@
export default function fullRebuild(params?: {
msg?: string;
}): Promise<void>;

30
dist/functions/server/full-rebuild.js vendored Normal file
View File

@ -0,0 +1,30 @@
import { log } from "../../utils/log";
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
import serverPostBuildFn from "./server-post-build-fn";
import watcherEsbuildCTX from "./watcher-esbuild-ctx";
export default async function fullRebuild(params) {
try {
const { msg } = params || {};
global.RECOMPILING = true;
if (msg) {
log.watch(msg);
}
global.ROUTER.reload();
await global.BUNDLER_CTX?.dispose();
global.BUNDLER_CTX = undefined;
global.BUNDLER_CTX_MAP = {};
await global.SSR_BUNDLER_CTX?.dispose();
global.SSR_BUNDLER_CTX = undefined;
global.SSR_BUNDLER_CTX_MAP = {};
allPagesESBuildContextBundler({
post_build_fn: serverPostBuildFn,
});
}
catch (error) {
log.error(error);
}
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();
watcherEsbuildCTX();
}
}

View File

@ -9,6 +9,10 @@ export default async function serverPostBuildFn() {
if (!controller?.target_map?.local_path) {
continue;
}
if (global.IS_404_PAGE) {
controller.controller.enqueue(`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`);
continue;
}
const target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path];
const mock_req = new Request(controller.page_url);
const { serverRes } = global.IS_SERVER_COMPONENT

View File

@ -4,6 +4,7 @@ import grabDirNames from "../../utils/grab-dir-names";
import { log } from "../../utils/log";
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
import serverPostBuildFn from "./server-post-build-fn";
import fullRebuild from "./full-rebuild";
const { ROOT_DIR } = grabDirNames();
export default async function watcherEsbuildCTX() {
const pages_src_watcher = watch(ROOT_DIR, {
@ -38,8 +39,10 @@ export default async function watcherEsbuildCTX() {
return;
}
const target_files_match = /\.(tsx?|jsx?|css)$/;
const rebuild_skip_paths = /\/pages\/api\//;
if (event !== "rename") {
if (filename.match(target_files_match)) {
if (filename.match(target_files_match) &&
!filename.match(rebuild_skip_paths)) {
if (global.RECOMPILING)
return;
global.RECOMPILING = true;
@ -47,8 +50,13 @@ export default async function watcherEsbuildCTX() {
global.IS_SERVER_COMPONENT = true;
}
if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) {
try {
await global.BUNDLER_CTX.rebuild();
}
catch (error) {
console.log(`ESBUILD Rebuild Error =>`, error);
}
}
else {
await fullRebuild({ msg: `Restarting Bundler ...` });
global.BUNDLER_CTX_DISPOSED = false;
@ -89,32 +97,6 @@ export default async function watcherEsbuildCTX() {
});
global.PAGES_SRC_WATCHER = pages_src_watcher;
}
async function fullRebuild(params) {
try {
const { msg } = params || {};
global.RECOMPILING = true;
if (msg) {
log.watch(msg);
}
global.ROUTER.reload();
await global.BUNDLER_CTX?.dispose();
global.BUNDLER_CTX = undefined;
global.BUNDLER_CTX_MAP = {};
await global.SSR_BUNDLER_CTX?.dispose();
global.SSR_BUNDLER_CTX = undefined;
global.SSR_BUNDLER_CTX_MAP = {};
allPagesESBuildContextBundler({
post_build_fn: serverPostBuildFn,
});
}
catch (error) {
log.error(error);
}
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();
watcherEsbuildCTX();
}
}
function reloadWatcher() {
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();

View File

@ -3,8 +3,9 @@ type Params = {
req?: Request;
file_path?: string;
debug?: boolean;
retry?: boolean;
return_server_res_only?: boolean;
skip_server_res?: boolean;
};
export default function grabPageComponent({ req, file_path: passed_file_path, debug, return_server_res_only, skip_server_res, }: Params): Promise<GrabPageComponentRes>;
export default function grabPageComponent(params: Params): Promise<GrabPageComponentRes>;
export {};

View File

@ -4,6 +4,7 @@ import _ from "lodash";
import { log } from "../../../utils/log";
import grabPageModules from "./grab-page-modules";
import grabPageCombinedServerRes from "./grab-page-combined-server-res";
import fullRebuild from "../full-rebuild";
class NotFoundError extends Error {
status = 404;
constructor(message) {
@ -11,7 +12,8 @@ class NotFoundError extends Error {
this.name = "NotFoundError";
}
}
export default async function grabPageComponent({ req, file_path: passed_file_path, debug, return_server_res_only, skip_server_res, }) {
export default async function grabPageComponent(params) {
const { req, file_path: passed_file_path, debug, return_server_res_only, skip_server_res, } = params;
const url = req?.url ? new URL(req.url) : undefined;
const router = global.ROUTER;
let routeParams = undefined;
@ -65,6 +67,7 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
url,
skip_server_res,
});
global.IS_404_PAGE = false;
return {
component,
serverRes,
@ -72,13 +75,33 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
module,
bundledMap,
root_module,
success: true,
};
}
catch (error) {
const is404 = error instanceof NotFoundError ||
error?.name === "NotFoundError" ||
error?.status === 404;
if (!is404) {
if (!params.retry) {
while (global.REBUILD_RETRIES < 2) {
global.REBUILD_RETRIES = global.REBUILD_RETRIES + 1;
await fullRebuild();
await Bun.sleep(200);
const component_retried = await grabPageComponent({
...params,
retry: true,
});
if (component_retried.success) {
global.REBUILD_RETRIES = 0;
return component_retried;
}
}
global.REBUILD_RETRIES = 0;
}
if (is404) {
global.IS_404_PAGE = true;
}
else {
log.error(`Error Grabbing Page Component: ${error.message}`);
log.error(`Page: ${passed_file_path || url?.pathname}`);
}
@ -90,28 +113,3 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
});
}
}
// let root_module: any;
// if (root_file) {
// if (isDevelopment()) {
// root_module = await grabFilePathModule({
// file_path: root_file,
// });
// } else {
// root_module = root_file ? await import(root_file) : undefined;
// }
// }
// const RootComponent = root_module?.default as FC<any> | undefined;
// let module: BunextPageModule;
// if (isDevelopment()) {
// module = await grabFilePathModule({ file_path });
// } else {
// module = await import(file_path);
// }
// const Component = main_module.default as FC<any>;
// const component = RootComponent ? (
// <RootComponent {...serverRes}>
// <Component {...serverRes} />
// </RootComponent>
// ) : (
// <Component {...serverRes} />
// );

View File

@ -250,6 +250,7 @@ export type GrabPageComponentRes = {
module?: BunextPageModule;
root_module?: BunextRootModule;
debug?: boolean;
success?: boolean;
};
export type BunextRootModule = BunextPageModule;
export type GrabPageReactBundledComponentRes = {

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/bunext",
"version": "1.0.69",
"version": "1.0.70",
"main": "dist/index.js",
"module": "index.ts",
"dependencies": {

View File

@ -16,6 +16,7 @@ type Params = {
};
export default async function allPagesESBuildContextBundler(params?: Params) {
try {
const pages = grabAllPages({ exclude_api: true });
global.PAGE_FILES = pages;
@ -83,6 +84,9 @@ export default async function allPagesESBuildContextBundler(params?: Params) {
});
await global.BUNDLER_CTX.rebuild();
} catch (error) {
console.log(`ESBUILD Error =>`, error);
}
}
function forceExternalReact(): esbuild.Plugin {

View File

@ -46,6 +46,8 @@ declare global {
var REACT_DOM_SERVER: any;
var REACT_DOM_MODULE_CACHE: Map<string, { main: any; css: string }>;
var BUNDLER_CTX_DISPOSED: boolean | undefined;
var REBUILD_RETRIES: number;
var IS_404_PAGE: boolean;
}
const dirNames = grabDirNames();
@ -56,6 +58,7 @@ export default async function bunextInit() {
global.BUNDLER_CTX_MAP = {};
global.SSR_BUNDLER_CTX_MAP = {};
global.BUNDLER_REBUILDS = 0;
global.REBUILD_RETRIES = 0;
global.PAGE_FILES = [];
global.SKIPPED_BROWSER_MODULES = new Set<string>();
global.DIR_NAMES = dirNames;

View File

@ -0,0 +1,37 @@
import { log } from "../../utils/log";
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
import serverPostBuildFn from "./server-post-build-fn";
import watcherEsbuildCTX from "./watcher-esbuild-ctx";
export default async function fullRebuild(params?: { msg?: string }) {
try {
const { msg } = params || {};
global.RECOMPILING = true;
if (msg) {
log.watch(msg);
}
global.ROUTER.reload();
await global.BUNDLER_CTX?.dispose();
global.BUNDLER_CTX = undefined;
global.BUNDLER_CTX_MAP = {};
await global.SSR_BUNDLER_CTX?.dispose();
global.SSR_BUNDLER_CTX = undefined;
global.SSR_BUNDLER_CTX_MAP = {};
allPagesESBuildContextBundler({
post_build_fn: serverPostBuildFn,
});
} catch (error: any) {
log.error(error);
}
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();
watcherEsbuildCTX();
}
}

View File

@ -14,6 +14,13 @@ export default async function serverPostBuildFn() {
continue;
}
if (global.IS_404_PAGE) {
controller.controller.enqueue(
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
);
continue;
}
const target_artifact =
global.BUNDLER_CTX_MAP[controller.target_map.local_path];

View File

@ -4,6 +4,7 @@ import grabDirNames from "../../utils/grab-dir-names";
import { log } from "../../utils/log";
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
import serverPostBuildFn from "./server-post-build-fn";
import fullRebuild from "./full-rebuild";
const { ROOT_DIR } = grabDirNames();
@ -49,9 +50,13 @@ export default async function watcherEsbuildCTX() {
}
const target_files_match = /\.(tsx?|jsx?|css)$/;
const rebuild_skip_paths = /\/pages\/api\//;
if (event !== "rename") {
if (filename.match(target_files_match)) {
if (
filename.match(target_files_match) &&
!filename.match(rebuild_skip_paths)
) {
if (global.RECOMPILING) return;
global.RECOMPILING = true;
@ -60,7 +65,11 @@ export default async function watcherEsbuildCTX() {
}
if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) {
try {
await global.BUNDLER_CTX.rebuild();
} catch (error) {
console.log(`ESBUILD Rebuild Error =>`, error);
}
} else {
await fullRebuild({ msg: `Restarting Bundler ...` });
global.BUNDLER_CTX_DISPOSED = false;
@ -114,39 +123,6 @@ export default async function watcherEsbuildCTX() {
global.PAGES_SRC_WATCHER = pages_src_watcher;
}
async function fullRebuild(params?: { msg?: string }) {
try {
const { msg } = params || {};
global.RECOMPILING = true;
if (msg) {
log.watch(msg);
}
global.ROUTER.reload();
await global.BUNDLER_CTX?.dispose();
global.BUNDLER_CTX = undefined;
global.BUNDLER_CTX_MAP = {};
await global.SSR_BUNDLER_CTX?.dispose();
global.SSR_BUNDLER_CTX = undefined;
global.SSR_BUNDLER_CTX_MAP = {};
allPagesESBuildContextBundler({
post_build_fn: serverPostBuildFn,
});
} catch (error: any) {
log.error(error);
}
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();
watcherEsbuildCTX();
}
}
function reloadWatcher() {
if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close();

View File

@ -5,6 +5,7 @@ import _ from "lodash";
import { log } from "../../../utils/log";
import grabPageModules from "./grab-page-modules";
import grabPageCombinedServerRes from "./grab-page-combined-server-res";
import fullRebuild from "../full-rebuild";
class NotFoundError extends Error {
status = 404;
@ -19,17 +20,22 @@ type Params = {
req?: Request;
file_path?: string;
debug?: boolean;
retry?: boolean;
return_server_res_only?: boolean;
skip_server_res?: boolean;
};
export default async function grabPageComponent({
export default async function grabPageComponent(
params: Params,
): Promise<GrabPageComponentRes> {
const {
req,
file_path: passed_file_path,
debug,
return_server_res_only,
skip_server_res,
}: Params): Promise<GrabPageComponentRes> {
} = params;
const url = req?.url ? new URL(req.url) : undefined;
const router = global.ROUTER;
@ -101,6 +107,8 @@ export default async function grabPageComponent({
skip_server_res,
});
global.IS_404_PAGE = false;
return {
component,
serverRes,
@ -108,6 +116,7 @@ export default async function grabPageComponent({
module,
bundledMap,
root_module,
success: true,
};
} catch (error: any) {
const is404 =
@ -115,7 +124,29 @@ export default async function grabPageComponent({
error?.name === "NotFoundError" ||
error?.status === 404;
if (!is404) {
if (!params.retry) {
while (global.REBUILD_RETRIES < 2) {
global.REBUILD_RETRIES = global.REBUILD_RETRIES + 1;
await fullRebuild();
await Bun.sleep(200);
const component_retried = await grabPageComponent({
...params,
retry: true,
});
if (component_retried.success) {
global.REBUILD_RETRIES = 0;
return component_retried;
}
}
global.REBUILD_RETRIES = 0;
}
if (is404) {
global.IS_404_PAGE = true;
} else {
log.error(`Error Grabbing Page Component: ${error.message}`);
log.error(`Page: ${passed_file_path || url?.pathname}`);
}
@ -128,34 +159,3 @@ export default async function grabPageComponent({
});
}
}
// let root_module: any;
// if (root_file) {
// if (isDevelopment()) {
// root_module = await grabFilePathModule({
// file_path: root_file,
// });
// } else {
// root_module = root_file ? await import(root_file) : undefined;
// }
// }
// const RootComponent = root_module?.default as FC<any> | undefined;
// let module: BunextPageModule;
// if (isDevelopment()) {
// module = await grabFilePathModule({ file_path });
// } else {
// module = await import(file_path);
// }
// const Component = main_module.default as FC<any>;
// const component = RootComponent ? (
// <RootComponent {...serverRes}>
// <Component {...serverRes} />
// </RootComponent>
// ) : (
// <Component {...serverRes} />
// );

View File

@ -284,6 +284,7 @@ export type GrabPageComponentRes = {
module?: BunextPageModule;
root_module?: BunextRootModule;
debug?: boolean;
success?: boolean;
};
export type BunextRootModule = BunextPageModule;