wildpig 1.0.13 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,8 @@ import devIndexHtml from "#/public/devIndex.html"
7
7
  const env = process.env;
8
8
  const isDev = env.NODE_ENV === "development";
9
9
 
10
+ console.log(metaRoutes);
11
+
10
12
 
11
13
  export const startServer = () => {
12
14
  Bun.serve({
@@ -27,17 +29,25 @@ export const startServer = () => {
27
29
  "Cache-Control": isDev ? "no-cache" : "public, max-age=31536000, immutable"
28
30
  }
29
31
  }),
30
- "/*": isDev ? devIndexHtml : async (request) => {
31
- const url = "/" + (request.url.split("/")[3] || "");
32
- // console.log("url:", url);
33
- const pathname = "/_WILDPIG_META_API" + url.split("?")[0];
34
- let meta: {title: string} | null = null;
32
+ "/*":
33
+ isDev ? devIndexHtml :
34
+ async (request: Request) => {
35
+ const pathname = "/_WILDPIG_META_API/" + (request.url.split("/")[3].split("?")[0] || "");
36
+ let resHtml = htmlString;
35
37
 
36
38
  if(pathname in metaRoutes){
37
- const metaRes = await metaRoutes[pathname]();
38
- meta = await metaRes.json();
39
+ try{
40
+ const metaResponse = await metaRoutes[pathname](request);
41
+ const meta = await metaResponse.json();
42
+ if(meta.title)resHtml = resHtml.replace("{{TITLE}}", meta.title);
43
+ if(meta.description)resHtml = resHtml.replace("{{DESCRIPTION}}", meta.description);
44
+ if(meta.keywords)resHtml = resHtml.replace("{{KEYWORDS}}", meta.keywords.join(", "));
45
+ }catch(e){
46
+ console.error("获取meta信息失败,请检查是否设置了meta信息", e);
47
+ }
39
48
  }
40
- return new Response(htmlString.replace("{{TITLE}}", meta?.title || "WildPig"), {
49
+
50
+ return new Response(resHtml, {
41
51
  headers: {
42
52
  "content-type": "text/html; charset=utf-8",
43
53
  "Access-Control-Allow-Origin": "*",
@@ -22,7 +22,7 @@ export const packageStatic = async () => {
22
22
  }
23
23
  }
24
24
  ));
25
-
25
+
26
26
  /** 打包ico */
27
27
  fs.copyFileSync("./public/favicon.ico", "./dist/public/favicon.ico");
28
28
  }
@@ -6,7 +6,7 @@ const isDev = process.env.NODE_ENV === "development";
6
6
 
7
7
  // 准备服务端路由表
8
8
  let routes = {};
9
- let metaRoutes: Record<string, () => Promise<Response>> = {};
9
+ let metaRoutes: Record<string, (req: Request) => Promise<Response>> = {};
10
10
  if(isDev){
11
11
  // 引入,用于监听
12
12
  setTimeout(() => import("@/App").catch(() => {}), 1000);
@@ -16,11 +16,11 @@ if(isDev){
16
16
  await Bun.build({
17
17
  entrypoints: ["./public/render.tsx"],
18
18
  outdir: "./public",
19
- format: "esm",
20
- sourcemap: true,
19
+ format: "esm"
21
20
  });
22
21
  // 将 ./public/render.js 转为 ./public/render.br
23
22
  writeFileSync("./public/render.js.br", brotliCompressSync(Buffer.from(readFileSync("./public/render.js"))));
23
+
24
24
  }else{
25
25
  routes = require("#/build/built-routes.ts").default;
26
26
  metaRoutes = require("#/build/built-meta-routes.ts").default;