Add error logs for server functions. Add cache dynamic function.
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import type { BunextPageModule, BunextPageModuleServerReturn, BunxRouteParams } from "../../../types";
|
||||
type Params = {
|
||||
html: string;
|
||||
module: BunextPageModule;
|
||||
root_module?: BunextPageModule;
|
||||
routeParams?: BunxRouteParams;
|
||||
serverRes?: BunextPageModuleServerReturn<any, any>;
|
||||
};
|
||||
export default function generateWebPageGetCachePage({ module, routeParams, serverRes, root_module, html, }: Params): Promise<boolean>;
|
||||
export {};
|
||||
@@ -0,0 +1,29 @@
|
||||
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 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) {
|
||||
log.error(`Error writing Cache => ${error.message}\n`, error);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
+10
-12
@@ -1,9 +1,10 @@
|
||||
import _ from "lodash";
|
||||
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, module, bundledMap, routeParams, serverRes, debug, root_module, }) {
|
||||
const is_dev = isDevelopment();
|
||||
const html = await genWebHTML({
|
||||
component,
|
||||
pageProps: serverRes,
|
||||
@@ -28,7 +29,7 @@ export default async function generateWebPageResponseFromComponentReturn({ compo
|
||||
...serverRes?.responseOptions?.headers,
|
||||
},
|
||||
};
|
||||
if (isDevelopment()) {
|
||||
if (is_dev) {
|
||||
res_opts.headers = {
|
||||
...res_opts.headers,
|
||||
"Cache-Control": "no-cache, no-store, must-revalidate",
|
||||
@@ -36,16 +37,13 @@ export default async function generateWebPageResponseFromComponentReturn({ compo
|
||||
Expires: "0",
|
||||
};
|
||||
}
|
||||
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,
|
||||
});
|
||||
}
|
||||
const res = new Response(html, res_opts);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import _ from "lodash";
|
||||
import { log } from "../../../utils/log";
|
||||
export default async function grabPageServerRes({ url, query, routeParams, server_function, }) {
|
||||
const default_props = {
|
||||
url: url
|
||||
@@ -37,8 +38,10 @@ export default async function grabPageServerRes({ url, query, routeParams, serve
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
log.error(`Page ${url?.pathname} Server Error => ${error.message}\n`, error);
|
||||
return {
|
||||
...default_props,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+13
-2
@@ -188,7 +188,10 @@ export type BunextPageServerFn<T extends {
|
||||
[k: string]: any;
|
||||
}> = (ctx: Omit<BunxRouteParams, "body">) => Promise<BunextPageModuleServerReturn<T>>;
|
||||
export type BunextRouteConfig = {
|
||||
cachePage?: boolean;
|
||||
/**
|
||||
* Whether to cache the current page
|
||||
*/
|
||||
cachePage?: BunextCachePageType;
|
||||
/**
|
||||
* Expiry time of the cache in seconds
|
||||
*/
|
||||
@@ -207,13 +210,21 @@ export type BunextPageModuleServerReturn<T extends {
|
||||
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;
|
||||
} = {
|
||||
|
||||
Reference in New Issue
Block a user