First working version
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import type { FC } from "react";
|
||||
import grabDirNames from "../../../utils/grab-dir-names";
|
||||
import grabPageName from "../../../utils/grab-page-name";
|
||||
import grabRouteParams from "../../../utils/grab-route-params";
|
||||
import grabRouter from "../../../utils/grab-router";
|
||||
import type { BunextPageModule, GrabPageComponentRes } from "../../../types";
|
||||
@@ -20,12 +19,7 @@ export default async function grabPageComponent({
|
||||
const url = req?.url ? new URL(req.url) : undefined;
|
||||
const router = grabRouter();
|
||||
|
||||
const {
|
||||
BUNX_ROOT_500_PRESET_COMPONENT,
|
||||
HYDRATION_DST_DIR,
|
||||
BUNX_ROOT_500_FILE_NAME,
|
||||
PAGES_DIR,
|
||||
} = grabDirNames();
|
||||
const { BUNX_ROOT_500_PRESET_COMPONENT, PAGES_DIR } = grabDirNames();
|
||||
|
||||
const routeParams = req ? await grabRouteParams({ req }) : undefined;
|
||||
|
||||
@@ -34,7 +28,7 @@ export default async function grabPageComponent({
|
||||
|
||||
if (!match?.filePath && url?.pathname) {
|
||||
const errMsg = `Page ${url.pathname} not found`;
|
||||
console.error(errMsg);
|
||||
// console.error(errMsg);
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
|
||||
@@ -42,11 +36,21 @@ export default async function grabPageComponent({
|
||||
|
||||
if (!file_path) {
|
||||
const errMsg = `No File Path (\`file_path\`) or Request Object (\`req\`) provided not found`;
|
||||
// console.error(errMsg);
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
|
||||
const bundledMap = global.BUNDLER_CTX_MAP?.find(
|
||||
(m) => m.local_path == file_path,
|
||||
);
|
||||
|
||||
if (!bundledMap?.path) {
|
||||
const errMsg = `No Bundled File Path for this request path!`;
|
||||
console.error(errMsg);
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
|
||||
const pageName = grabPageName({ path: file_path });
|
||||
// const pageName = grabPageName({ path: file_path });
|
||||
|
||||
const root_pages_component_ts_file = `${path.join(PAGES_DIR, AppNames["RootPagesComponentName"])}.ts`;
|
||||
const root_pages_component_tsx_file = `${path.join(PAGES_DIR, AppNames["RootPagesComponentName"])}.tsx`;
|
||||
@@ -63,17 +67,19 @@ export default async function grabPageComponent({
|
||||
? root_pages_component_js_file
|
||||
: undefined;
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
const root_module = root_file
|
||||
? await import(`${root_file}?t=${global.LAST_BUILD_TIME ?? 0}`)
|
||||
? await import(`${root_file}?t=${now}`)
|
||||
: undefined;
|
||||
|
||||
const RootComponent = root_module?.default as FC<any> | undefined;
|
||||
|
||||
const component_file_path = root_module
|
||||
? `${file_path}`
|
||||
: `${file_path}?t=${global.LAST_BUILD_TIME ?? 0}`;
|
||||
// const component_file_path = root_module
|
||||
// ? `${file_path}`
|
||||
// : `${file_path}?t=${global.LAST_BUILD_TIME ?? 0}`;
|
||||
|
||||
const module: BunextPageModule = await import(component_file_path);
|
||||
const module: BunextPageModule = await import(`${file_path}?t=${now}`);
|
||||
|
||||
const serverRes = await (async () => {
|
||||
try {
|
||||
@@ -87,6 +93,7 @@ export default async function grabPageComponent({
|
||||
})();
|
||||
|
||||
const Component = module.default as FC<any>;
|
||||
|
||||
const component = RootComponent ? (
|
||||
<RootComponent {...serverRes}>
|
||||
<Component {...serverRes} />
|
||||
@@ -95,34 +102,30 @@ export default async function grabPageComponent({
|
||||
<Component {...serverRes} />
|
||||
);
|
||||
|
||||
return { component, serverRes, routeParams, pageName, module };
|
||||
return {
|
||||
component,
|
||||
serverRes,
|
||||
routeParams,
|
||||
module,
|
||||
bundledMap,
|
||||
};
|
||||
} catch (error: any) {
|
||||
// console.log(`Grab page component ERROR =>`, error.message);
|
||||
|
||||
const match = router.match("/500");
|
||||
|
||||
const filePath = match?.filePath || BUNX_ROOT_500_PRESET_COMPONENT;
|
||||
|
||||
// if (!match?.filePath) {
|
||||
// bundle({
|
||||
// out_dir: HYDRATION_DST_DIR,
|
||||
// src: `${BUNX_ROOT_500_PRESET_COMPONENT}`,
|
||||
// debug: true,
|
||||
// });
|
||||
// }
|
||||
|
||||
const module: BunextPageModule = await import(filePath);
|
||||
|
||||
// const module: BunextPageModule = await import(
|
||||
// `${filePath}?t=${global.LAST_BUILD_TIME ?? 0}`
|
||||
// );
|
||||
|
||||
const Component = module.default as FC<any>;
|
||||
const component = <Component />;
|
||||
|
||||
return {
|
||||
component,
|
||||
pageName: BUNX_ROOT_500_FILE_NAME,
|
||||
routeParams,
|
||||
module,
|
||||
bundledMap: {},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user