Add error logs for server functions. Add cache dynamic function.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import _ from "lodash";
|
||||
import type {
|
||||
BunextPageModule,
|
||||
BunextPageModuleServerReturn,
|
||||
BunxRouteParams,
|
||||
} from "../../../types";
|
||||
import { log } from "../../../utils/log";
|
||||
import writeCache from "../../cache/write-cache";
|
||||
|
||||
type Params = {
|
||||
html: string;
|
||||
module: BunextPageModule;
|
||||
root_module?: BunextPageModule;
|
||||
routeParams?: BunxRouteParams;
|
||||
serverRes?: BunextPageModuleServerReturn<any, any>;
|
||||
};
|
||||
|
||||
export default async function generateWebPageGetCachePage({
|
||||
module,
|
||||
routeParams,
|
||||
serverRes,
|
||||
root_module,
|
||||
html,
|
||||
}: Params) {
|
||||
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) {
|
||||
try {
|
||||
const is_cache =
|
||||
typeof cache_page == "boolean"
|
||||
? cache_page
|
||||
: await cache_page({ ctx: routeParams, serverRes });
|
||||
|
||||
if (!is_cache) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const key =
|
||||
routeParams.url.pathname + (routeParams.url.search || "");
|
||||
|
||||
writeCache({
|
||||
key,
|
||||
value: html,
|
||||
paradigm: "html",
|
||||
expiry_seconds,
|
||||
});
|
||||
} catch (error: any) {
|
||||
log.error(`Error writing Cache => ${error.message}\n`, error);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
+11
-14
@@ -2,8 +2,8 @@ import _ from "lodash";
|
||||
import type { GrabPageComponentRes } from "../../../types";
|
||||
import isDevelopment from "../../../utils/is-development";
|
||||
import { log } from "../../../utils/log";
|
||||
import writeCache from "../../cache/write-cache";
|
||||
import genWebHTML from "./generate-web-html";
|
||||
import generateWebPageGetCachePage from "./generate-web-page-get-cache-page";
|
||||
|
||||
export default async function generateWebPageResponseFromComponentReturn({
|
||||
component,
|
||||
@@ -14,6 +14,8 @@ export default async function generateWebPageResponseFromComponentReturn({
|
||||
debug,
|
||||
root_module,
|
||||
}: GrabPageComponentRes) {
|
||||
const is_dev = isDevelopment();
|
||||
|
||||
const html = await genWebHTML({
|
||||
component,
|
||||
pageProps: serverRes,
|
||||
@@ -45,7 +47,7 @@ export default async function generateWebPageResponseFromComponentReturn({
|
||||
},
|
||||
};
|
||||
|
||||
if (isDevelopment()) {
|
||||
if (is_dev) {
|
||||
res_opts.headers = {
|
||||
...res_opts.headers,
|
||||
"Cache-Control": "no-cache, no-store, must-revalidate",
|
||||
@@ -54,18 +56,13 @@ export default async function generateWebPageResponseFromComponentReturn({
|
||||
};
|
||||
}
|
||||
|
||||
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) {
|
||||
const key = routeParams.url.pathname + (routeParams.url.search || "");
|
||||
writeCache({
|
||||
key,
|
||||
value: html,
|
||||
paradigm: "html",
|
||||
expiry_seconds,
|
||||
if (!is_dev) {
|
||||
await generateWebPageGetCachePage({
|
||||
html,
|
||||
module,
|
||||
root_module,
|
||||
routeParams,
|
||||
serverRes,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import type {
|
||||
GrabPageComponentRes,
|
||||
} from "../../../types";
|
||||
import _ from "lodash";
|
||||
import { log } from "../../../utils/log";
|
||||
|
||||
type Params = {
|
||||
url?: URL;
|
||||
@@ -56,9 +57,15 @@ export default async function grabPageServerRes({
|
||||
return {
|
||||
...default_props,
|
||||
};
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
log.error(
|
||||
`Page ${url?.pathname} Server Error => ${error.message}\n`,
|
||||
error,
|
||||
);
|
||||
|
||||
return {
|
||||
...default_props,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+16
-2
@@ -222,7 +222,10 @@ export type BunextPageServerFn<
|
||||
) => Promise<BunextPageModuleServerReturn<T>>;
|
||||
|
||||
export type BunextRouteConfig = {
|
||||
cachePage?: boolean;
|
||||
/**
|
||||
* Whether to cache the current page
|
||||
*/
|
||||
cachePage?: BunextCachePageType;
|
||||
/**
|
||||
* Expiry time of the cache in seconds
|
||||
*/
|
||||
@@ -237,14 +240,25 @@ export type BunextPageModuleServerReturn<
|
||||
query?: Q;
|
||||
redirect?: BunextPageModuleServerRedirect;
|
||||
responseOptions?: ResponseInit;
|
||||
cachePage?: boolean;
|
||||
/**
|
||||
* Whether to cache the current page
|
||||
*/
|
||||
cachePage?: BunextCachePageType;
|
||||
/**
|
||||
* Expiry time of the cache in seconds
|
||||
*/
|
||||
cacheExpiry?: number;
|
||||
url?: BunextPageModuleServerReturnURLObject;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
export type BunextCachePageType =
|
||||
| boolean
|
||||
| ((params: {
|
||||
ctx: Omit<BunxRouteParams, "body">;
|
||||
serverRes?: BunextPageModuleServerReturn;
|
||||
}) => Promise<boolean> | boolean);
|
||||
|
||||
export type BunextPageProps<
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
Q extends { [k: string]: any } = { [k: string]: any },
|
||||
|
||||
Reference in New Issue
Block a user