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?.cache_page || false; const expiry_seconds = config?.cacheExpiry || serverRes?.cache_expiry; if (cache_page && routeParams?.url) { try { const status = serverRes?.response_options?.status; if (status && (status < 200 || status >= 300)) { return false; } const res_headers = new Headers(serverRes?.response_options?.headers); if (res_headers.has("set-cookie")) { return false; } const req_headers = routeParams.req.headers; if (!global.BUNEXT_CONFIG?.cache_credentialed_requests && (req_headers.has("cookie") || req_headers.has("authorization"))) { return false; } 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; }