This commit is contained in:
Rizky 2024-07-04 14:53:05 +07:00
parent de7acde842
commit 503baa6a64
7 changed files with 75 additions and 25 deletions

View File

@ -100,7 +100,8 @@ export const _ = {
g.code_index_cache[site_id][build_path] && g.code_index_cache[site_id][build_path] &&
g.code_index_cache[site_id][build_path].content && g.code_index_cache[site_id][build_path].content &&
g.code_index_cache[site_id][build_path] && g.code_index_cache[site_id][build_path] &&
g.code_index_cache[site_id][build_path].ts === ts g.code_index_cache[site_id][build_path].ts === ts &&
req.headers.get("accept-encoding")?.includes("br")
) { ) {
return new Response( return new Response(
g.code_index_cache[site_id][build_path].content, g.code_index_cache[site_id][build_path].content,
@ -174,12 +175,29 @@ export const _ = {
case "route": { case "route": {
if (!g.route_cache) g.route_cache = {}; if (!g.route_cache) g.route_cache = {};
if (g.route_cache[site_id]) { if (g.route_cache[site_id]) {
return new Response(g.route_cache[site_id], { if (
headers: { req.headers.get("accept-encoding")?.includes("br") &&
"content-type": "application/json", g.route_cache[site_id].br
"content-encoding": "br", ) {
}, return new Response(g.route_cache[site_id].br, {
}); headers: {
"content-type": "application/json",
"content-encoding": "br",
},
});
}
if (
req.headers.get("accept-encoding")?.includes("gzip") &&
g.route_cache[site_id].gzip
) {
return new Response(g.route_cache[site_id].gzip, {
headers: {
"content-type": "application/json",
"content-encoding": "gzip",
},
});
}
} }
const site = await _db.site.findFirst({ const site = await _db.site.findFirst({
@ -227,6 +245,10 @@ export const _ = {
select: { url: true, id: true }, select: { url: true, id: true },
}); });
if (!g.route_cache[site_id]) {
g.route_cache[site_id] = {};
}
const res = JSON.stringify({ const res = JSON.stringify({
site: { ...site, api_url }, site: { ...site, api_url },
urls, urls,
@ -243,11 +265,13 @@ export const _ = {
g.route_cache_compressing = new Set(); g.route_cache_compressing = new Set();
if (g.route_cache_compressing.has(site_id)) return; if (g.route_cache_compressing.has(site_id)) return;
g.route_cache_compressing.add(site_id); g.route_cache_compressing.add(site_id);
g.route_cache[site_id] = g.br.compress(encoder.encode(res)); g.route_cache[site_id].br = g.br.compress(encoder.encode(res));
g.route_cache_compressing.delete(site_id); g.route_cache_compressing.delete(site_id);
}, 100); }, 100);
return new Response(await gzipAsync(res), { g.route_cache[site_id].gzip = await gzipAsync(res);
return new Response(g.route_cache[site_id].gzip, {
headers: { headers: {
"content-type": "application/json", "content-type": "application/json",
"content-encoding": "gzip", "content-encoding": "gzip",
@ -333,7 +357,11 @@ export const _ = {
}; };
} }
} }
if (g.main_cache[src_path]) {
if (
req.headers.get("accept-encoding")?.includes("br") &&
g.main_cache[src_path]
) {
return new Response(g.main_cache[src_path].content, { return new Response(g.main_cache[src_path].content, {
headers: { headers: {
"content-encoding": "br", "content-encoding": "br",
@ -341,6 +369,20 @@ export const _ = {
}, },
}); });
} }
if (req.headers.get("accept-encoding")?.includes("gzip")) {
const file = Bun.file(src_path);
return new Response(
await gzipAsync(new Uint8Array(await file.arrayBuffer())),
{
headers: {
"content-encoding": "gzip",
"content-type": mime.getType(src_path) || "",
},
}
);
}
return index_html; return index_html;
} }
}, },

File diff suppressed because one or more lines are too long

View File

@ -5,11 +5,10 @@ import { g } from "utils/global";
import { WSData } from "../../../../../../pkgs/core/server/create"; import { WSData } from "../../../../../../pkgs/core/server/create";
import { prodIndex } from "../../../../util/prod-index"; import { prodIndex } from "../../../../util/prod-index";
import { realpathSync } from 'fs' import { realpathSync } from 'fs';
import { code } from "../../code/code"; import { code } from "../../code/code";
import "./server-runtime";
import { initServer } from "../../code/parts/init/server"; import { initServer } from "../../code/parts/init/server";
import { waitUntil } from "web-utils"; import "./server-runtime";
if (!g.server_main_handler) { if (!g.server_main_handler) {
g.server_main_handler = {}; g.server_main_handler = {};

View File

@ -62,12 +62,12 @@ export const loadApiProxyDef = async (_url: string, with_types: boolean) => {
const baseUrl = (url: string) => { const baseUrl = (url: string) => {
const base = new URL(url); const base = new URL(url);
if (!["prasi.avolut.com", "localhost"].includes(base.hostname)) { // if (!["prasi.avolut.com", "localhost"].includes(base.hostname)) {
const cur = new URL(location.href); // const cur = new URL(location.href);
if (!["prasi.avolut.com", "localhost"].includes(cur.hostname)) { // if (!["prasi.avolut.com", "localhost"].includes(cur.hostname)) {
base.hostname = cur.hostname; // base.hostname = cur.hostname;
} // }
} // }
return `${base.protocol}//${base.host}`; return `${base.protocol}//${base.host}`;
}; };

View File

@ -179,7 +179,7 @@ export const fetchSendDb = async (params: any, dburl: string) => {
cachedQueryResult[hsum].result = result; cachedQueryResult[hsum].result = result;
return result; return result;
} catch (e) { } catch (e) {
console.error("DBQuery failed:" + result); console.error("DBQuery failed:", result);
} }
} }
@ -190,7 +190,7 @@ export const fetchSendDb = async (params: any, dburl: string) => {
try { try {
return JSON.parse(result); return JSON.parse(result);
} catch (e) { } catch (e) {
console.error("DBQuery failed:" + result); console.error("DBQuery failed:", result);
} }
} }
return null; return null;

View File

@ -93,7 +93,16 @@ const injectSiteScript = () => {
const ts = localStorage.getItem("api-ts-" + base_url); const ts = localStorage.getItem("api-ts-" + base_url);
script.src = `${base_url}/_prasi/load.js?url=${base_url}&v3&ts=${ts}`; const cur = new URL(location.href);
cur.pathname = "";
if (!["prasi.avolut.com", "localhost"].includes(cur.hostname)) {
const cur_url = cur.toString();
script.src = `${
cur_url.endsWith("/") ? cur_url : `${cur_url}/`
}_prasi/load.js?url=${cur_url}&v3&ts=${ts}`;
} else {
script.src = `${base_url}/_prasi/load.js?url=${base_url}&v3&ts=${ts}`;
}
if (!document.querySelector(`script[src="${script.src}"]`)) { if (!document.querySelector(`script[src="${script.src}"]`)) {
d.body.appendChild(script); d.body.appendChild(script);

View File

@ -61,7 +61,7 @@ export const g = global as unknown as {
syncronize: typeof syncronize; syncronize: typeof syncronize;
static_cache: any; static_cache: any;
route_cache_compressing: Set<string>; route_cache_compressing: Set<string>;
route_cache: Record<string, any>; route_cache: Record<string, {br?: any, gzip?: any}>;
code_index_compressing: Set<string>; code_index_compressing: Set<string>;
code_index_cache: Record< code_index_cache: Record<
string, string,