Imporve HMR Post-build function speed
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { BunextPageModule, BunextPageModuleServerReturn, BunxRouteParams } from "../../../types";
|
||||
type Params = {
|
||||
html: string;
|
||||
module: BunextPageModule;
|
||||
module?: BunextPageModule;
|
||||
root_module?: BunextPageModule;
|
||||
routeParams?: BunxRouteParams;
|
||||
serverRes?: BunextPageModuleServerReturn<any, any>;
|
||||
|
||||
@@ -2,7 +2,7 @@ import _ from "lodash";
|
||||
import { log } from "../../../utils/log";
|
||||
import writeCache from "../../cache/write-cache";
|
||||
export default async function generateWebPageGetCachePage({ module, routeParams, serverRes, root_module, html, }) {
|
||||
const config = _.merge(root_module?.config, module.config);
|
||||
const config = _.merge(root_module?.config, module?.config);
|
||||
const cache_page = config?.cachePage || serverRes?.cachePage || false;
|
||||
const expiry_seconds = config?.cacheExpiry || serverRes?.cacheExpiry;
|
||||
if (cache_page && routeParams?.url) {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { BunextPageModuleServerReturn, BunxRouteParams } from "../../../types";
|
||||
type Params = {
|
||||
file_path: string;
|
||||
debug?: boolean;
|
||||
url?: URL;
|
||||
query?: any;
|
||||
routeParams?: BunxRouteParams;
|
||||
};
|
||||
export default function grabPageCombinedServerRes({ file_path, debug, url, query, routeParams, }: Params): Promise<{
|
||||
serverRes: BunextPageModuleServerReturn;
|
||||
}>;
|
||||
export {};
|
||||
@@ -0,0 +1,42 @@
|
||||
import _ from "lodash";
|
||||
import { log } from "../../../utils/log";
|
||||
import grabRootFilePath from "./grab-root-file-path";
|
||||
import grabPageServerRes from "./grab-page-server-res";
|
||||
import grabPageServerPath from "./grab-page-server-path";
|
||||
export default async function grabPageCombinedServerRes({ file_path, debug, url, query, routeParams, }) {
|
||||
const now = Date.now();
|
||||
const { root_file_path } = grabRootFilePath();
|
||||
const { server_file_path: root_server_file_path } = root_file_path
|
||||
? grabPageServerPath({ file_path: root_file_path })
|
||||
: {};
|
||||
const root_server_module = root_server_file_path
|
||||
? await import(`${root_server_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
const root_server_fn = root_server_module?.default || root_server_module?.server;
|
||||
const rootServerRes = root_server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: root_server_fn,
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
if (debug) {
|
||||
log.info(`rootServerRes:`, rootServerRes);
|
||||
}
|
||||
const { server_file_path } = grabPageServerPath({ file_path });
|
||||
const server_module = server_file_path
|
||||
? await import(`${server_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
const server_fn = server_module?.default || server_module?.server;
|
||||
const serverRes = server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: server_fn,
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
|
||||
return { serverRes: mergedServerRes };
|
||||
}
|
||||
@@ -3,6 +3,7 @@ type Params = {
|
||||
req?: Request;
|
||||
file_path?: string;
|
||||
debug?: boolean;
|
||||
return_server_res_only?: boolean;
|
||||
};
|
||||
export default function grabPageComponent({ req, file_path: passed_file_path, debug, }: Params): Promise<GrabPageComponentRes>;
|
||||
export default function grabPageComponent({ req, file_path: passed_file_path, debug, return_server_res_only, }: Params): Promise<GrabPageComponentRes>;
|
||||
export {};
|
||||
|
||||
+12
-1
@@ -7,9 +7,10 @@ import grabRootFilePath from "./grab-root-file-path";
|
||||
import grabPageServerRes from "./grab-page-server-res";
|
||||
import grabPageServerPath from "./grab-page-server-path";
|
||||
import grabPageModules from "./grab-page-modules";
|
||||
import grabPageCombinedServerRes from "./grab-page-combined-server-res";
|
||||
class NotFoundError extends Error {
|
||||
}
|
||||
export default async function grabPageComponent({ req, file_path: passed_file_path, debug, }) {
|
||||
export default async function grabPageComponent({ req, file_path: passed_file_path, debug, return_server_res_only, }) {
|
||||
const url = req?.url ? new URL(req.url) : undefined;
|
||||
const router = global.ROUTER;
|
||||
let routeParams = undefined;
|
||||
@@ -45,6 +46,16 @@ export default async function grabPageComponent({ req, file_path: passed_file_pa
|
||||
if (debug) {
|
||||
log.info(`bundledMap:`, bundledMap);
|
||||
}
|
||||
if (return_server_res_only) {
|
||||
const { serverRes } = await grabPageCombinedServerRes({
|
||||
file_path,
|
||||
debug,
|
||||
query: match?.query,
|
||||
routeParams,
|
||||
url,
|
||||
});
|
||||
return { serverRes };
|
||||
}
|
||||
const { component, module, serverRes, root_module } = await grabPageModules({
|
||||
file_path,
|
||||
debug,
|
||||
|
||||
+10
-37
@@ -4,55 +4,28 @@ import { log } from "../../../utils/log";
|
||||
import grabRootFilePath from "./grab-root-file-path";
|
||||
import grabPageServerRes from "./grab-page-server-res";
|
||||
import grabPageServerPath from "./grab-page-server-path";
|
||||
import grabPageCombinedServerRes from "./grab-page-combined-server-res";
|
||||
export default async function grabPageModules({ file_path, debug, url, query, routeParams, }) {
|
||||
const now = Date.now();
|
||||
const { root_file_path } = grabRootFilePath();
|
||||
const root_module = root_file_path
|
||||
? await import(`${root_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
const { server_file_path: root_server_file_path } = root_file_path
|
||||
? grabPageServerPath({ file_path: root_file_path })
|
||||
: {};
|
||||
const root_server_module = root_server_file_path
|
||||
? await import(`${root_server_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
const root_server_fn = root_server_module?.default || root_server_module?.server;
|
||||
const rootServerRes = root_server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: root_server_fn,
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
if (debug) {
|
||||
log.info(`rootServerRes:`, rootServerRes);
|
||||
}
|
||||
const module = await import(`${file_path}?t=${now}`);
|
||||
const { server_file_path } = grabPageServerPath({ file_path });
|
||||
const server_module = server_file_path
|
||||
? await import(`${server_file_path}?t=${now}`)
|
||||
: undefined;
|
||||
if (debug) {
|
||||
log.info(`module:`, module);
|
||||
}
|
||||
const server_fn = server_module?.default || server_module?.server;
|
||||
const serverRes = server_fn
|
||||
? await grabPageServerRes({
|
||||
server_function: server_fn,
|
||||
url,
|
||||
query,
|
||||
routeParams,
|
||||
})
|
||||
: undefined;
|
||||
if (debug) {
|
||||
log.info(`serverRes:`, serverRes);
|
||||
}
|
||||
const mergedServerRes = _.merge(rootServerRes || {}, serverRes || {});
|
||||
const { serverRes } = await grabPageCombinedServerRes({
|
||||
file_path,
|
||||
debug,
|
||||
query,
|
||||
routeParams,
|
||||
url,
|
||||
});
|
||||
const { component } = (await grabPageBundledReactComponent({
|
||||
file_path,
|
||||
root_file_path,
|
||||
server_res: mergedServerRes,
|
||||
server_res: serverRes,
|
||||
})) || {};
|
||||
if (!component) {
|
||||
throw new Error(`Couldn't grab page component`);
|
||||
@@ -62,7 +35,7 @@ export default async function grabPageModules({ file_path, debug, url, query, ro
|
||||
}
|
||||
return {
|
||||
component,
|
||||
serverRes: mergedServerRes,
|
||||
serverRes,
|
||||
module,
|
||||
root_module,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user