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,57 +9,62 @@ import virtualFilesPlugin from "./plugins/virtual-files-plugin";
import esbuildCTXArtifactTracker from "./plugins/esbuild-ctx-artifact-tracker"; import esbuildCTXArtifactTracker from "./plugins/esbuild-ctx-artifact-tracker";
const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames(); const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames();
export default async function allPagesESBuildContextBundler(params) { export default async function allPagesESBuildContextBundler(params) {
const pages = grabAllPages({ exclude_api: true }); try {
global.PAGE_FILES = pages; const pages = grabAllPages({ exclude_api: true });
const dev = isDevelopment(); global.PAGE_FILES = pages;
const entryToPage = new Map(); const dev = isDevelopment();
for (const page of pages) { const entryToPage = new Map();
const tsx = await grabClientHydrationScript({ for (const page of pages) {
page_local_path: page.local_path, const tsx = await grabClientHydrationScript({
page_local_path: page.local_path,
});
if (!tsx)
continue;
const entryFile = path.join(BUNX_HYDRATION_SRC_DIR, `${page.url_path}.tsx`);
// await Bun.write(entryFile, txt, { createPath: true });
entryToPage.set(entryFile, { ...page, tsx });
}
const entryPoints = [...entryToPage.keys()].map((e) => `hydration-virtual:${e}`);
global.BUNDLER_CTX = await esbuild.context({
entryPoints,
outdir: HYDRATION_DST_DIR,
bundle: true,
minify: !dev,
format: "esm",
target: "es2020",
platform: "browser",
define: {
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
},
entryNames: "[dir]/[hash]",
metafile: true,
plugins: [
forceExternalReact(),
tailwindEsbuildPlugin,
virtualFilesPlugin({
entryToPage,
}),
esbuildCTXArtifactTracker({
entryToPage,
post_build_fn: params?.post_build_fn,
}),
],
jsx: "automatic",
splitting: true,
treeShaking: true,
external: [
"react",
"react-dom",
"react-dom/client",
"react/jsx-runtime",
"react/jsx-dev-runtime",
],
}); });
if (!tsx) await global.BUNDLER_CTX.rebuild();
continue; }
const entryFile = path.join(BUNX_HYDRATION_SRC_DIR, `${page.url_path}.tsx`); catch (error) {
// await Bun.write(entryFile, txt, { createPath: true }); console.log(`ESBUILD Error =>`, error);
entryToPage.set(entryFile, { ...page, tsx });
} }
const entryPoints = [...entryToPage.keys()].map((e) => `hydration-virtual:${e}`);
global.BUNDLER_CTX = await esbuild.context({
entryPoints,
outdir: HYDRATION_DST_DIR,
bundle: true,
minify: !dev,
format: "esm",
target: "es2020",
platform: "browser",
define: {
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
},
entryNames: "[dir]/[hash]",
metafile: true,
plugins: [
forceExternalReact(),
tailwindEsbuildPlugin,
virtualFilesPlugin({
entryToPage,
}),
esbuildCTXArtifactTracker({
entryToPage,
post_build_fn: params?.post_build_fn,
}),
],
jsx: "automatic",
splitting: true,
treeShaking: true,
external: [
"react",
"react-dom",
"react-dom/client",
"react/jsx-runtime",
"react/jsx-dev-runtime",
],
});
await global.BUNDLER_CTX.rebuild();
} }
function forceExternalReact() { function forceExternalReact() {
return { return {

View File

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

View File

@ -15,6 +15,7 @@ export default async function bunextInit() {
global.BUNDLER_CTX_MAP = {}; global.BUNDLER_CTX_MAP = {};
global.SSR_BUNDLER_CTX_MAP = {}; global.SSR_BUNDLER_CTX_MAP = {};
global.BUNDLER_REBUILDS = 0; global.BUNDLER_REBUILDS = 0;
global.REBUILD_RETRIES = 0;
global.PAGE_FILES = []; global.PAGE_FILES = [];
global.SKIPPED_BROWSER_MODULES = new Set(); global.SKIPPED_BROWSER_MODULES = new Set();
global.DIR_NAMES = dirNames; 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) { if (!controller?.target_map?.local_path) {
continue; 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 target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path];
const mock_req = new Request(controller.page_url); const mock_req = new Request(controller.page_url);
const { serverRes } = global.IS_SERVER_COMPONENT 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 { log } from "../../utils/log";
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler"; import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
import serverPostBuildFn from "./server-post-build-fn"; import serverPostBuildFn from "./server-post-build-fn";
import fullRebuild from "./full-rebuild";
const { ROOT_DIR } = grabDirNames(); const { ROOT_DIR } = grabDirNames();
export default async function watcherEsbuildCTX() { export default async function watcherEsbuildCTX() {
const pages_src_watcher = watch(ROOT_DIR, { const pages_src_watcher = watch(ROOT_DIR, {
@ -38,8 +39,10 @@ export default async function watcherEsbuildCTX() {
return; return;
} }
const target_files_match = /\.(tsx?|jsx?|css)$/; const target_files_match = /\.(tsx?|jsx?|css)$/;
const rebuild_skip_paths = /\/pages\/api\//;
if (event !== "rename") { if (event !== "rename") {
if (filename.match(target_files_match)) { if (filename.match(target_files_match) &&
!filename.match(rebuild_skip_paths)) {
if (global.RECOMPILING) if (global.RECOMPILING)
return; return;
global.RECOMPILING = true; global.RECOMPILING = true;
@ -47,7 +50,12 @@ export default async function watcherEsbuildCTX() {
global.IS_SERVER_COMPONENT = true; global.IS_SERVER_COMPONENT = true;
} }
if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) { if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) {
await global.BUNDLER_CTX.rebuild(); try {
await global.BUNDLER_CTX.rebuild();
}
catch (error) {
console.log(`ESBUILD Rebuild Error =>`, error);
}
} }
else { else {
await fullRebuild({ msg: `Restarting Bundler ...` }); await fullRebuild({ msg: `Restarting Bundler ...` });
@ -89,32 +97,6 @@ export default async function watcherEsbuildCTX() {
}); });
global.PAGES_SRC_WATCHER = pages_src_watcher; 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() { function reloadWatcher() {
if (global.PAGES_SRC_WATCHER) { if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close(); global.PAGES_SRC_WATCHER.close();

View File

@ -3,8 +3,9 @@ type Params = {
req?: Request; req?: Request;
file_path?: string; file_path?: string;
debug?: boolean; debug?: boolean;
retry?: boolean;
return_server_res_only?: boolean; return_server_res_only?: boolean;
skip_server_res?: 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 {}; export {};

View File

@ -4,6 +4,7 @@ import _ from "lodash";
import { log } from "../../../utils/log"; import { log } from "../../../utils/log";
import grabPageModules from "./grab-page-modules"; import grabPageModules from "./grab-page-modules";
import grabPageCombinedServerRes from "./grab-page-combined-server-res"; import grabPageCombinedServerRes from "./grab-page-combined-server-res";
import fullRebuild from "../full-rebuild";
class NotFoundError extends Error { class NotFoundError extends Error {
status = 404; status = 404;
constructor(message) { constructor(message) {
@ -11,7 +12,8 @@ class NotFoundError extends Error {
this.name = "NotFoundError"; 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 url = req?.url ? new URL(req.url) : undefined;
const router = global.ROUTER; const router = global.ROUTER;
let routeParams = undefined; let routeParams = undefined;
@ -65,6 +67,7 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
url, url,
skip_server_res, skip_server_res,
}); });
global.IS_404_PAGE = false;
return { return {
component, component,
serverRes, serverRes,
@ -72,13 +75,33 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
module, module,
bundledMap, bundledMap,
root_module, root_module,
success: true,
}; };
} }
catch (error) { catch (error) {
const is404 = error instanceof NotFoundError || const is404 = error instanceof NotFoundError ||
error?.name === "NotFoundError" || error?.name === "NotFoundError" ||
error?.status === 404; 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(`Error Grabbing Page Component: ${error.message}`);
log.error(`Page: ${passed_file_path || url?.pathname}`); 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; module?: BunextPageModule;
root_module?: BunextRootModule; root_module?: BunextRootModule;
debug?: boolean; debug?: boolean;
success?: boolean;
}; };
export type BunextRootModule = BunextPageModule; export type BunextRootModule = BunextPageModule;
export type GrabPageReactBundledComponentRes = { export type GrabPageReactBundledComponentRes = {

View File

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

View File

@ -16,73 +16,77 @@ type Params = {
}; };
export default async function allPagesESBuildContextBundler(params?: Params) { export default async function allPagesESBuildContextBundler(params?: Params) {
const pages = grabAllPages({ exclude_api: true }); try {
const pages = grabAllPages({ exclude_api: true });
global.PAGE_FILES = pages; global.PAGE_FILES = pages;
const dev = isDevelopment(); const dev = isDevelopment();
const entryToPage = new Map<string, PageFiles & { tsx: string }>(); const entryToPage = new Map<string, PageFiles & { tsx: string }>();
for (const page of pages) { for (const page of pages) {
const tsx = await grabClientHydrationScript({ const tsx = await grabClientHydrationScript({
page_local_path: page.local_path, page_local_path: page.local_path,
}); });
if (!tsx) continue; if (!tsx) continue;
const entryFile = path.join( const entryFile = path.join(
BUNX_HYDRATION_SRC_DIR, BUNX_HYDRATION_SRC_DIR,
`${page.url_path}.tsx`, `${page.url_path}.tsx`,
);
// await Bun.write(entryFile, txt, { createPath: true });
entryToPage.set(entryFile, { ...page, tsx });
}
const entryPoints = [...entryToPage.keys()].map(
(e) => `hydration-virtual:${e}`,
); );
// await Bun.write(entryFile, txt, { createPath: true }); global.BUNDLER_CTX = await esbuild.context({
entryToPage.set(entryFile, { ...page, tsx }); entryPoints,
outdir: HYDRATION_DST_DIR,
bundle: true,
minify: !dev,
format: "esm",
target: "es2020",
platform: "browser",
define: {
"process.env.NODE_ENV": JSON.stringify(
dev ? "development" : "production",
),
},
entryNames: "[dir]/[hash]",
metafile: true,
plugins: [
forceExternalReact(),
tailwindEsbuildPlugin,
virtualFilesPlugin({
entryToPage,
}),
esbuildCTXArtifactTracker({
entryToPage,
post_build_fn: params?.post_build_fn,
}),
],
jsx: "automatic",
splitting: true,
treeShaking: true,
external: [
"react",
"react-dom",
"react-dom/client",
"react/jsx-runtime",
"react/jsx-dev-runtime",
],
});
await global.BUNDLER_CTX.rebuild();
} catch (error) {
console.log(`ESBUILD Error =>`, error);
} }
const entryPoints = [...entryToPage.keys()].map(
(e) => `hydration-virtual:${e}`,
);
global.BUNDLER_CTX = await esbuild.context({
entryPoints,
outdir: HYDRATION_DST_DIR,
bundle: true,
minify: !dev,
format: "esm",
target: "es2020",
platform: "browser",
define: {
"process.env.NODE_ENV": JSON.stringify(
dev ? "development" : "production",
),
},
entryNames: "[dir]/[hash]",
metafile: true,
plugins: [
forceExternalReact(),
tailwindEsbuildPlugin,
virtualFilesPlugin({
entryToPage,
}),
esbuildCTXArtifactTracker({
entryToPage,
post_build_fn: params?.post_build_fn,
}),
],
jsx: "automatic",
splitting: true,
treeShaking: true,
external: [
"react",
"react-dom",
"react-dom/client",
"react/jsx-runtime",
"react/jsx-dev-runtime",
],
});
await global.BUNDLER_CTX.rebuild();
} }
function forceExternalReact(): esbuild.Plugin { function forceExternalReact(): esbuild.Plugin {

View File

@ -46,6 +46,8 @@ declare global {
var REACT_DOM_SERVER: any; var REACT_DOM_SERVER: any;
var REACT_DOM_MODULE_CACHE: Map<string, { main: any; css: string }>; var REACT_DOM_MODULE_CACHE: Map<string, { main: any; css: string }>;
var BUNDLER_CTX_DISPOSED: boolean | undefined; var BUNDLER_CTX_DISPOSED: boolean | undefined;
var REBUILD_RETRIES: number;
var IS_404_PAGE: boolean;
} }
const dirNames = grabDirNames(); const dirNames = grabDirNames();
@ -56,6 +58,7 @@ export default async function bunextInit() {
global.BUNDLER_CTX_MAP = {}; global.BUNDLER_CTX_MAP = {};
global.SSR_BUNDLER_CTX_MAP = {}; global.SSR_BUNDLER_CTX_MAP = {};
global.BUNDLER_REBUILDS = 0; global.BUNDLER_REBUILDS = 0;
global.REBUILD_RETRIES = 0;
global.PAGE_FILES = []; global.PAGE_FILES = [];
global.SKIPPED_BROWSER_MODULES = new Set<string>(); global.SKIPPED_BROWSER_MODULES = new Set<string>();
global.DIR_NAMES = dirNames; 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; continue;
} }
if (global.IS_404_PAGE) {
controller.controller.enqueue(
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
);
continue;
}
const target_artifact = const target_artifact =
global.BUNDLER_CTX_MAP[controller.target_map.local_path]; 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 { log } from "../../utils/log";
import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler"; import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler";
import serverPostBuildFn from "./server-post-build-fn"; import serverPostBuildFn from "./server-post-build-fn";
import fullRebuild from "./full-rebuild";
const { ROOT_DIR } = grabDirNames(); const { ROOT_DIR } = grabDirNames();
@ -49,9 +50,13 @@ export default async function watcherEsbuildCTX() {
} }
const target_files_match = /\.(tsx?|jsx?|css)$/; const target_files_match = /\.(tsx?|jsx?|css)$/;
const rebuild_skip_paths = /\/pages\/api\//;
if (event !== "rename") { if (event !== "rename") {
if (filename.match(target_files_match)) { if (
filename.match(target_files_match) &&
!filename.match(rebuild_skip_paths)
) {
if (global.RECOMPILING) return; if (global.RECOMPILING) return;
global.RECOMPILING = true; global.RECOMPILING = true;
@ -60,7 +65,11 @@ export default async function watcherEsbuildCTX() {
} }
if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) { if (global.BUNDLER_CTX && !global.BUNDLER_CTX_DISPOSED) {
await global.BUNDLER_CTX.rebuild(); try {
await global.BUNDLER_CTX.rebuild();
} catch (error) {
console.log(`ESBUILD Rebuild Error =>`, error);
}
} else { } else {
await fullRebuild({ msg: `Restarting Bundler ...` }); await fullRebuild({ msg: `Restarting Bundler ...` });
global.BUNDLER_CTX_DISPOSED = false; global.BUNDLER_CTX_DISPOSED = false;
@ -114,39 +123,6 @@ export default async function watcherEsbuildCTX() {
global.PAGES_SRC_WATCHER = pages_src_watcher; 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() { function reloadWatcher() {
if (global.PAGES_SRC_WATCHER) { if (global.PAGES_SRC_WATCHER) {
global.PAGES_SRC_WATCHER.close(); global.PAGES_SRC_WATCHER.close();

View File

@ -5,6 +5,7 @@ import _ from "lodash";
import { log } from "../../../utils/log"; import { log } from "../../../utils/log";
import grabPageModules from "./grab-page-modules"; import grabPageModules from "./grab-page-modules";
import grabPageCombinedServerRes from "./grab-page-combined-server-res"; import grabPageCombinedServerRes from "./grab-page-combined-server-res";
import fullRebuild from "../full-rebuild";
class NotFoundError extends Error { class NotFoundError extends Error {
status = 404; status = 404;
@ -19,17 +20,22 @@ type Params = {
req?: Request; req?: Request;
file_path?: string; file_path?: string;
debug?: boolean; debug?: boolean;
retry?: boolean;
return_server_res_only?: boolean; return_server_res_only?: boolean;
skip_server_res?: boolean; skip_server_res?: boolean;
}; };
export default async function grabPageComponent({ export default async function grabPageComponent(
req, params: Params,
file_path: passed_file_path, ): Promise<GrabPageComponentRes> {
debug, const {
return_server_res_only, req,
skip_server_res, file_path: passed_file_path,
}: Params): Promise<GrabPageComponentRes> { debug,
return_server_res_only,
skip_server_res,
} = params;
const url = req?.url ? new URL(req.url) : undefined; const url = req?.url ? new URL(req.url) : undefined;
const router = global.ROUTER; const router = global.ROUTER;
@ -101,6 +107,8 @@ export default async function grabPageComponent({
skip_server_res, skip_server_res,
}); });
global.IS_404_PAGE = false;
return { return {
component, component,
serverRes, serverRes,
@ -108,6 +116,7 @@ export default async function grabPageComponent({
module, module,
bundledMap, bundledMap,
root_module, root_module,
success: true,
}; };
} catch (error: any) { } catch (error: any) {
const is404 = const is404 =
@ -115,7 +124,29 @@ export default async function grabPageComponent({
error?.name === "NotFoundError" || error?.name === "NotFoundError" ||
error?.status === 404; 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(`Error Grabbing Page Component: ${error.message}`);
log.error(`Page: ${passed_file_path || url?.pathname}`); 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; module?: BunextPageModule;
root_module?: BunextRootModule; root_module?: BunextRootModule;
debug?: boolean; debug?: boolean;
success?: boolean;
}; };
export type BunextRootModule = BunextPageModule; export type BunextRootModule = BunextPageModule;