@blocklet/pages-kit-inner-components 0.1.7 → 0.1.8
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.
- package/lib/cjs/project-html.js +2 -3
- package/lib/es/project-html.js +45 -46
- package/package.json +1 -1
package/lib/cjs/project-html.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
"use strict";const k=require("./_chunks/components-Bu9hOpQX.js"),
|
|
1
|
+
"use strict";const k=require("./_chunks/components-Bu9hOpQX.js"),j=require("@blocklet/crawler/middlewares"),J=require("@blocklet/pages-kit/types"),G=require("@blocklet/pages-kit/utils/preload"),z=require("@blocklet/sdk/lib/component"),U=require("@blocklet/sdk/lib/config"),K=require("cheerio"),Q=require("express"),L=require("fs"),V=require("mustache"),A=require("path"),D=require("ufo"),x=e=>e&&e.__esModule?e:{default:e};function X(e){if(e&&e.__esModule)return e;const a=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const u in e)if(u!=="default"){const t=Object.getOwnPropertyDescriptor(e,u);Object.defineProperty(a,u,t.get?t:{enumerable:!0,get:()=>e[u]})}}return a.default=e,Object.freeze(a)}const i=x(U),q=X(K),Y=x(V),Z=["/assets/"],R=()=>[!0,"true"].includes(i.default.env.preferences.allowCrawler);function H(){R()?j.initCronCrawlBlocklet({time:"30 30 */12 * * *",options:{}}):j.cancelCronCrawlBlocklet()}H();i.default.events.on(i.default.Events.envUpdate,H);function F({app:e,viteDevServer:a,projectId:u,state:t}){let _;if(a){const n=L.readFileSync(A.resolve(process.cwd(),"./index.html"),"utf-8");e.use(async(b,T,P)=>{const v=b.originalUrl;_=await a.transformIndexHtml(v,n),P()})}else _=L.readFileSync(A.resolve(process.env.BLOCKLET_APP_DIR,"dist/index.html"),"utf-8");const y=Q.Router();y.use(j.initSEOMiddleware({autoReturnHtml:!1,allowCrawler:R})),y.get("/:path(*)?",async(n,b,T)=>{var M,O,$;if(Z.some(c=>n.path.startsWith(c))){T();return}const P=performance.now(),v=()=>Math.round(performance.now()-P),N="production";let r,m,C=_;if(n.path.startsWith("/pages")){const c=n.params.lang,h=n.cookies.nf_lang,g=new Set([...i.default.env.languages.map(d=>d.code)]);let p=(c&&g.has(c)?c:void 0)||(g.has(h)?h:void 0)||[...g][0];if((!p||!((M=t.supportedLocales)!=null&&M.find(d=>d.locale===p)))&&(p=(($=(O=t.supportedLocales)==null?void 0:O[0])==null?void 0:$.locale)||""),p){const d=[];Object.values(t.pages).forEach(s=>{const S=Object.values((s==null?void 0:s.sections)??{}).filter(l=>{var w;return l.component==="custom-component"&&((w=l.config)==null?void 0:w.componentId)}).map(l=>{var w,I;return{id:l.id,componentId:l.config.componentId,useCache:l.config.useCache,cacheDuration:l.config.cacheDuration,properties:(I=(w=l.locales)==null?void 0:w[p])==null?void 0:I.properties}});d.push(...S)});const B=new Promise((s,S)=>{setTimeout(()=>{S(new Error("getPreloadComponents timeout in promise race, skip it"))},90*1e3)});try{await Promise.race([B,k.getPreloadComponents({mode:N,req:n,state:t,locale:p,instances:d,module:J.PreloadComponentScriptModule.UMD_FN}).then(s=>{s&&(m=G.injectPreloadComponents(s))})])}catch(s){k.logger.warn("getPreloadComponents timeout in promise race, skip inject html elements"),k.logger.warn(s)}}}try{const c=(r==null?void 0:r.title)||i.default.env.appName,h=(r==null?void 0:r.description)||i.default.env.appDescription,g=D.withQuery(D.joinURL(i.default.env.appUrl,"/.well-known/service/blocklet/og.png"),{title:c,description:h});C=Y.default.render(_,{ogTitle:c,ogDescription:h,ogImage:ee(r==null?void 0:r.image)||g,pagesPublishedAt:(t==null?void 0:t.config.publishedAt)||new Date().getTime()})}catch{}const o=t==null?void 0:t.config.fontFamily,W=n.query.mode==="draft"||n.url.includes("preview"),f=q.load(C);f("head").find("#injected-head-elements").replaceWith(`
|
|
2
2
|
|
|
3
3
|
${o!=null&&o.title?`<link rel="preload" as="style" href="https://fonts.googleapis.com/css?family=${o.title}&display=swap">`:""}
|
|
4
4
|
${o!=null&&o.description&&(o==null?void 0:o.description)!==(o==null?void 0:o.title)?`<link rel="preload" as="style" href="https://fonts.googleapis.com/css?family=${o.description}&display=swap">`:""}
|
|
5
5
|
<script>
|
|
6
|
-
window.__PROJECT_ID__ = "${d}"
|
|
7
6
|
window.__PAGE_STATE__ = ${JSON.stringify(t)}
|
|
8
7
|
|
|
9
8
|
document.querySelectorAll('link[rel="preload"][as="style"]').forEach(function (link) {
|
|
@@ -20,4 +19,4 @@
|
|
|
20
19
|
darkMode: "class",
|
|
21
20
|
};
|
|
22
21
|
<\/script>`:""}
|
|
23
|
-
`);const
|
|
22
|
+
`);const E=U.getBlockletJs();if(E&&f('script[src="__blocklet__.js"]').replaceWith(`<script>${E}<\/script>`),m!=null&&m.html&&f("head").find("#injected-html-elements").replaceWith(m.html),n.cachedHtml){const c=q.load(n.cachedHtml);f("body").html(c("body").html()||"")}C=f.html(),b.send(C),k.logger.info(`[html] render: ${n.path}, ${v()}ms`)}),e.use(y)}function ee(e){return z.getComponentMountPoint("image-bin"),e}module.exports=F;
|
package/lib/es/project-html.js
CHANGED
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import { a as
|
|
2
|
-
import { initCronCrawlBlocklet as
|
|
3
|
-
import { PreloadComponentScriptModule as
|
|
4
|
-
import { injectPreloadComponents as
|
|
5
|
-
import { getComponentMountPoint as
|
|
6
|
-
import l, { getBlockletJs as
|
|
7
|
-
import * as
|
|
8
|
-
import { Router as
|
|
9
|
-
import { readFileSync as
|
|
10
|
-
import
|
|
11
|
-
import { resolve as
|
|
12
|
-
import { withQuery as
|
|
13
|
-
const
|
|
14
|
-
function
|
|
15
|
-
|
|
1
|
+
import { a as H, l as y } from "./_chunks/components-BVBbxiyS.js";
|
|
2
|
+
import { initCronCrawlBlocklet as W, cancelCronCrawlBlocklet as B, initSEOMiddleware as N } from "@blocklet/crawler/middlewares";
|
|
3
|
+
import { PreloadComponentScriptModule as J } from "@blocklet/pages-kit/types";
|
|
4
|
+
import { injectPreloadComponents as G } from "@blocklet/pages-kit/utils/preload";
|
|
5
|
+
import { getComponentMountPoint as K } from "@blocklet/sdk/lib/component";
|
|
6
|
+
import l, { getBlockletJs as Q } from "@blocklet/sdk/lib/config";
|
|
7
|
+
import * as j from "cheerio";
|
|
8
|
+
import { Router as z } from "express";
|
|
9
|
+
import { readFileSync as A } from "fs";
|
|
10
|
+
import V from "mustache";
|
|
11
|
+
import { resolve as M } from "path";
|
|
12
|
+
import { withQuery as X, joinURL as Y } from "ufo";
|
|
13
|
+
const Z = ["/assets/"], U = () => [!0, "true"].includes(l.env.preferences.allowCrawler);
|
|
14
|
+
function D() {
|
|
15
|
+
U() ? W({
|
|
16
16
|
time: "30 30 */12 * * *",
|
|
17
17
|
options: {}
|
|
18
|
-
}) :
|
|
18
|
+
}) : B();
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
l.events.on(l.Events.envUpdate,
|
|
20
|
+
D();
|
|
21
|
+
l.events.on(l.Events.envUpdate, D);
|
|
22
22
|
function de({
|
|
23
23
|
app: p,
|
|
24
24
|
viteDevServer: E,
|
|
25
|
-
projectId:
|
|
25
|
+
projectId: q,
|
|
26
26
|
state: t
|
|
27
27
|
}) {
|
|
28
28
|
let g;
|
|
29
29
|
if (E) {
|
|
30
|
-
const o =
|
|
31
|
-
p.use(async (
|
|
32
|
-
const P =
|
|
33
|
-
g = await E.transformIndexHtml(P, o),
|
|
30
|
+
const o = A(M(process.cwd(), "./index.html"), "utf-8");
|
|
31
|
+
p.use(async (k, T, _) => {
|
|
32
|
+
const P = k.originalUrl;
|
|
33
|
+
g = await E.transformIndexHtml(P, o), _();
|
|
34
34
|
});
|
|
35
35
|
} else
|
|
36
|
-
g =
|
|
37
|
-
const C =
|
|
36
|
+
g = A(M(process.env.BLOCKLET_APP_DIR, "dist/index.html"), "utf-8");
|
|
37
|
+
const C = z();
|
|
38
38
|
C.use(
|
|
39
39
|
N({
|
|
40
40
|
autoReturnHtml: !1,
|
|
41
|
-
allowCrawler:
|
|
41
|
+
allowCrawler: U
|
|
42
42
|
})
|
|
43
|
-
), C.get("/:path(*)?", async (o,
|
|
43
|
+
), C.get("/:path(*)?", async (o, k, T) => {
|
|
44
44
|
var S, b, I;
|
|
45
|
-
if (
|
|
45
|
+
if (Z.some((n) => o.path.startsWith(n))) {
|
|
46
46
|
T();
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
|
-
const
|
|
49
|
+
const _ = performance.now(), P = () => Math.round(performance.now() - _), O = "production";
|
|
50
50
|
let r, m, w = g;
|
|
51
51
|
if (o.path.startsWith("/pages")) {
|
|
52
52
|
const n = o.params.lang, h = o.cookies.nf_lang, u = /* @__PURE__ */ new Set([...l.env.languages.map((a) => a.code)]);
|
|
@@ -71,23 +71,23 @@ function de({
|
|
|
71
71
|
});
|
|
72
72
|
a.push(...v);
|
|
73
73
|
});
|
|
74
|
-
const
|
|
74
|
+
const x = new Promise((i, v) => {
|
|
75
75
|
setTimeout(() => {
|
|
76
76
|
v(new Error("getPreloadComponents timeout in promise race, skip it"));
|
|
77
77
|
}, 90 * 1e3);
|
|
78
78
|
});
|
|
79
79
|
try {
|
|
80
80
|
await Promise.race([
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
mode:
|
|
81
|
+
x,
|
|
82
|
+
H({
|
|
83
|
+
mode: O,
|
|
84
84
|
req: o,
|
|
85
85
|
state: t,
|
|
86
86
|
locale: s,
|
|
87
87
|
instances: a,
|
|
88
|
-
module:
|
|
88
|
+
module: J.UMD_FN
|
|
89
89
|
}).then((i) => {
|
|
90
|
-
i && (m =
|
|
90
|
+
i && (m = G(i));
|
|
91
91
|
})
|
|
92
92
|
]);
|
|
93
93
|
} catch (i) {
|
|
@@ -96,25 +96,24 @@ function de({
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
try {
|
|
99
|
-
const n = (r == null ? void 0 : r.title) || l.env.appName, h = (r == null ? void 0 : r.description) || l.env.appDescription, u = Y(
|
|
99
|
+
const n = (r == null ? void 0 : r.title) || l.env.appName, h = (r == null ? void 0 : r.description) || l.env.appDescription, u = X(Y(l.env.appUrl, "/.well-known/service/blocklet/og.png"), {
|
|
100
100
|
title: n,
|
|
101
101
|
description: h
|
|
102
102
|
});
|
|
103
|
-
w =
|
|
103
|
+
w = V.render(g, {
|
|
104
104
|
ogTitle: n,
|
|
105
105
|
ogDescription: h,
|
|
106
|
-
ogImage:
|
|
106
|
+
ogImage: F(r == null ? void 0 : r.image) || u,
|
|
107
107
|
pagesPublishedAt: (t == null ? void 0 : t.config.publishedAt) || (/* @__PURE__ */ new Date()).getTime()
|
|
108
108
|
});
|
|
109
109
|
} catch {
|
|
110
110
|
}
|
|
111
|
-
const e = t == null ? void 0 : t.config.fontFamily,
|
|
111
|
+
const e = t == null ? void 0 : t.config.fontFamily, R = o.query.mode === "draft" || o.url.includes("preview"), d = j.load(w);
|
|
112
112
|
d("head").find("#injected-head-elements").replaceWith(`
|
|
113
113
|
|
|
114
114
|
${e != null && e.title ? `<link rel="preload" as="style" href="https://fonts.googleapis.com/css?family=${e.title}&display=swap">` : ""}
|
|
115
115
|
${e != null && e.description && (e == null ? void 0 : e.description) !== (e == null ? void 0 : e.title) ? `<link rel="preload" as="style" href="https://fonts.googleapis.com/css?family=${e.description}&display=swap">` : ""}
|
|
116
116
|
<script>
|
|
117
|
-
window.__PROJECT_ID__ = "${R}"
|
|
118
117
|
window.__PAGE_STATE__ = ${JSON.stringify(t)}
|
|
119
118
|
|
|
120
119
|
document.querySelectorAll('link[rel="preload"][as="style"]').forEach(function (link) {
|
|
@@ -125,23 +124,23 @@ function de({
|
|
|
125
124
|
});
|
|
126
125
|
<\/script>
|
|
127
126
|
|
|
128
|
-
${
|
|
127
|
+
${R ? `<script src="https://cdn.tailwindcss.com"><\/script>
|
|
129
128
|
<script>
|
|
130
129
|
window.tailwind.config = {
|
|
131
130
|
darkMode: "class",
|
|
132
131
|
};
|
|
133
132
|
<\/script>` : ""}
|
|
134
133
|
`);
|
|
135
|
-
const $ =
|
|
134
|
+
const $ = Q();
|
|
136
135
|
if ($ && d('script[src="__blocklet__.js"]').replaceWith(`<script>${$}<\/script>`), m != null && m.html && d("head").find("#injected-html-elements").replaceWith(m.html), o.cachedHtml) {
|
|
137
|
-
const n =
|
|
136
|
+
const n = j.load(o.cachedHtml);
|
|
138
137
|
d("body").html(n("body").html() || "");
|
|
139
138
|
}
|
|
140
|
-
w = d.html(),
|
|
139
|
+
w = d.html(), k.send(w), y.info(`[html] render: ${o.path}, ${P()}ms`);
|
|
141
140
|
}), p.use(C);
|
|
142
141
|
}
|
|
143
|
-
function
|
|
144
|
-
return
|
|
142
|
+
function F(p) {
|
|
143
|
+
return K("image-bin"), p;
|
|
145
144
|
}
|
|
146
145
|
export {
|
|
147
146
|
de as default
|