zova-module-a-ssr 5.0.0 → 5.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.
- package/LICENSE +21 -0
- package/dist/index.js +658 -0
- package/dist/index.js.map +1 -0
- package/package.json +32 -23
- package/src/.metadata/index.ts +70 -0
- package/src/.metadata/this.ts +2 -0
- package/src/bean/sys.ssrState.ts +40 -0
- package/src/config/config.ts +27 -0
- package/src/index.ts +3 -0
- package/src/lib/index.ts +3 -0
- package/src/lib/ssr.ts +264 -0
- package/src/lib/ssrMetaStore.ts +392 -0
- package/src/lib/useMeta.ts +50 -0
- package/src/lib/utils.ts +10 -0
- package/src/monkey.ts +87 -0
- package/src/monkeySys.ts +38 -0
- package/src/types/config.ts +21 -0
- package/src/types/index.ts +2 -0
- package/src/types/ssr.ts +136 -0
- package/README.md +0 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016-present Zova
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,658 @@
|
|
|
1
|
+
import { BeanBase, BeanInfo, BeanScopeBase, BeanSimple, cast } from "zova";
|
|
2
|
+
import { Scope, Sys } from "zova-module-a-bean";
|
|
3
|
+
import { isNavigationFailure } from "@cabloy/vue-router";
|
|
4
|
+
import { computed, normalizeClass, normalizeStyle, onActivated, onDeactivated, onUnmounted, ref, useSSRContext, watch } from "vue";
|
|
5
|
+
import { includeBooleanAttr, isBooleanAttr, isString, stringifyStyle } from "@vue/shared";
|
|
6
|
+
import { defu } from "defu";
|
|
7
|
+
import { extend } from "@cabloy/extend";
|
|
8
|
+
import * as devalue from "devalue";
|
|
9
|
+
//#region src/bean/sys.ssrState.ts
|
|
10
|
+
var _dec$1, _dec2$1, _class$1;
|
|
11
|
+
var SymbolSSRState$1 = Symbol("SymbolSSRState");
|
|
12
|
+
var SymbolSSRStateDefer = Symbol("SymbolSSRStateDefer");
|
|
13
|
+
var SysSsrState = (_dec$1 = Sys(), _dec2$1 = BeanInfo({ module: "a-ssr" }), _dec$1(_class$1 = _dec2$1(_class$1 = class SysSsrState extends BeanBase {
|
|
14
|
+
constructor(...args) {
|
|
15
|
+
super(...args);
|
|
16
|
+
this[SymbolSSRState$1] = void 0;
|
|
17
|
+
this[SymbolSSRStateDefer] = void 0;
|
|
18
|
+
}
|
|
19
|
+
async __init__() {
|
|
20
|
+
if (process.env.CLIENT) {
|
|
21
|
+
if (cast(window).__INITIAL_STATE__) {
|
|
22
|
+
this[SymbolSSRState$1] = cast(window).__INITIAL_STATE__;
|
|
23
|
+
delete cast(window).__INITIAL_STATE__;
|
|
24
|
+
document.getElementById("ssr-state-init")?.remove();
|
|
25
|
+
} else this[SymbolSSRState$1] = {};
|
|
26
|
+
if (cast(window).__INITIAL_STATE_DEFER__) {
|
|
27
|
+
this[SymbolSSRStateDefer] = cast(window).__INITIAL_STATE_DEFER__;
|
|
28
|
+
delete cast(window).__INITIAL_STATE_DEFER__;
|
|
29
|
+
document.getElementById("ssr-state-defer-init")?.remove();
|
|
30
|
+
} else this[SymbolSSRStateDefer] = {};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
get state() {
|
|
34
|
+
return this[SymbolSSRState$1];
|
|
35
|
+
}
|
|
36
|
+
get stateDefer() {
|
|
37
|
+
return this[SymbolSSRStateDefer];
|
|
38
|
+
}
|
|
39
|
+
}) || _class$1) || _class$1);
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/config/config.ts
|
|
42
|
+
var config = (sys) => {
|
|
43
|
+
return {
|
|
44
|
+
cookieTheme: sys.env.SSR_COOKIE_THEME === "true",
|
|
45
|
+
cookieThemeDarkDefault: sys.env.SSR_COOKIE_THEMEDARK_DEFAULT === "true",
|
|
46
|
+
optimization: { bodyReadyObserver: sys.env.SSR_BODYREADYOBSERVER === "true" },
|
|
47
|
+
transferCache: sys.env.SSR_TRANSFERCACHE === "false" ? false : { expires: _normalizeExpires(sys.env.SSR_TRANSFERCACHE_EXPIRES) }
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
function _normalizeExpires(expires) {
|
|
51
|
+
if (!expires) return 0;
|
|
52
|
+
if (!Number.isNaN(Number(expires))) return Number(expires);
|
|
53
|
+
return expires;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/lib/useMeta.ts
|
|
57
|
+
function useMeta(ctx, metaOptions) {
|
|
58
|
+
if (process.env.SERVER) ctx.meta.$ssr.context.__qMetaList.push(typeof metaOptions === "function" ? metaOptions() : metaOptions);
|
|
59
|
+
else {
|
|
60
|
+
const meta = { active: true };
|
|
61
|
+
if (typeof metaOptions === "function") {
|
|
62
|
+
const content = computed(metaOptions);
|
|
63
|
+
meta.val = content.value;
|
|
64
|
+
watch(content, (val) => {
|
|
65
|
+
meta.val = val;
|
|
66
|
+
meta.active === true && ctx.meta.$ssr.metaStore.planClientUpdate();
|
|
67
|
+
});
|
|
68
|
+
} else meta.val = metaOptions;
|
|
69
|
+
ctx.meta.$ssr.metaStore.addMetaOptions(meta);
|
|
70
|
+
ctx.meta.$ssr.metaStore.planClientUpdate();
|
|
71
|
+
onActivated(() => {
|
|
72
|
+
try {
|
|
73
|
+
meta.active = true;
|
|
74
|
+
ctx.meta.$ssr.metaStore.planClientUpdate();
|
|
75
|
+
} catch (_err) {}
|
|
76
|
+
});
|
|
77
|
+
onDeactivated(() => {
|
|
78
|
+
try {
|
|
79
|
+
meta.active = false;
|
|
80
|
+
ctx.meta.$ssr.metaStore.planClientUpdate();
|
|
81
|
+
} catch (_err) {}
|
|
82
|
+
});
|
|
83
|
+
onUnmounted(() => {
|
|
84
|
+
try {
|
|
85
|
+
ctx.meta.$ssr.metaStore.removeMetaOptions(meta);
|
|
86
|
+
ctx.meta.$ssr.metaStore.planClientUpdate();
|
|
87
|
+
} catch (_err) {}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/monkey.ts
|
|
93
|
+
var Monkey = class extends BeanSimple {
|
|
94
|
+
appContextInitialize(ctx) {
|
|
95
|
+
ctx.meta.$ssr = ctx.app.ctx.meta.$ssr;
|
|
96
|
+
}
|
|
97
|
+
async appInitialize() {
|
|
98
|
+
if (process.env.SERVER) this._ssrErrorHandler();
|
|
99
|
+
}
|
|
100
|
+
async moduleLoading(_module) {}
|
|
101
|
+
async moduleLoaded(module) {
|
|
102
|
+
if (process.env.SERVER && process.env.PROD) {
|
|
103
|
+
if (!this.ctx.meta.$ssr.context.modules) this.ctx.meta.$ssr.context.modules = /* @__PURE__ */ new Set();
|
|
104
|
+
this.ctx.meta.$ssr.context.modules.add(`@@${module.info.relativeName}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async beanInit(bean, beanInstance) {
|
|
108
|
+
const self = this;
|
|
109
|
+
bean.defineProperty(beanInstance, "$ssr", {
|
|
110
|
+
enumerable: false,
|
|
111
|
+
configurable: true,
|
|
112
|
+
get() {
|
|
113
|
+
return self.app.ctx.meta.$ssr;
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
bean.defineProperty(beanInstance, "$useMeta", {
|
|
117
|
+
enumerable: false,
|
|
118
|
+
configurable: true,
|
|
119
|
+
get() {
|
|
120
|
+
return function(options) {
|
|
121
|
+
const ctx = cast(this).ctx;
|
|
122
|
+
ctx.util.instanceScope(() => {
|
|
123
|
+
useMeta(ctx, options);
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
_ssrErrorHandler() {
|
|
130
|
+
if (!process.env.SERVER) return;
|
|
131
|
+
const _eventErrorHandler = this.app.meta.event.on("app:errorHandler", (_data, next) => {
|
|
132
|
+
const err = next();
|
|
133
|
+
if (!err || !(err instanceof Error)) return err;
|
|
134
|
+
return this._errorHandlerDefaultServer(err);
|
|
135
|
+
});
|
|
136
|
+
this.ctx.meta.$ssr.context.onRendered((_err) => {
|
|
137
|
+
_eventErrorHandler();
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
_errorHandlerDefaultServer(err) {
|
|
141
|
+
if (!process.env.SERVER) return err;
|
|
142
|
+
if (isNavigationFailure(err)) {
|
|
143
|
+
if (!this.ctx.meta.$ssr.context._meta.renderError) this.ctx.meta.$ssr.context._meta.renderError = err;
|
|
144
|
+
return;
|
|
145
|
+
} else if (err.code === 401) try {
|
|
146
|
+
this.app.$gotoLogin();
|
|
147
|
+
} catch (err) {
|
|
148
|
+
this.ctx.meta.$ssr.context._meta.renderError = err;
|
|
149
|
+
}
|
|
150
|
+
else this.ctx.meta.$ssr.context._meta.renderError = err;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region src/lib/utils.ts
|
|
155
|
+
function unevalPatch(value) {
|
|
156
|
+
return devalue.uneval(value);
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region src/lib/ssrMetaStore.ts
|
|
160
|
+
var CtxSSRMetaStore = class extends BeanSimple {
|
|
161
|
+
constructor(...args) {
|
|
162
|
+
super(...args);
|
|
163
|
+
this._updateId = 0;
|
|
164
|
+
this._currentClientMeta = void 0;
|
|
165
|
+
this._clientList = [];
|
|
166
|
+
}
|
|
167
|
+
__init__() {
|
|
168
|
+
if (process.env.SERVER) {
|
|
169
|
+
const ssrContext = this.ctx.meta.$ssr.context;
|
|
170
|
+
ssrContext.__qMetaList = [];
|
|
171
|
+
if (this.sys.env.SSR_BODYREADYOBSERVER === "true") ssrContext.__qMetaList.push({ bodyStyle: { display: "none" } });
|
|
172
|
+
ssrContext.rendered = () => {
|
|
173
|
+
this._onRenderedLast();
|
|
174
|
+
ssrContext.rendered = () => {};
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
if (process.env.CLIENT && this.ctx.meta.$ssr.isRuntimeSsrPreHydration) {
|
|
178
|
+
this._currentClientMeta = cast(window).__Q_META__;
|
|
179
|
+
document.getElementById("ssr-meta-init")?.remove();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
_onRenderedLast(err) {
|
|
183
|
+
if (!err) {
|
|
184
|
+
const ssrContext = this.ctx.meta.$ssr.context;
|
|
185
|
+
this._injectContextState(ssrContext);
|
|
186
|
+
this._injectContextStateDefer(ssrContext);
|
|
187
|
+
this._injectServerMeta(ssrContext);
|
|
188
|
+
}
|
|
189
|
+
this.app.close();
|
|
190
|
+
}
|
|
191
|
+
addMetaOptions(metaOptionsWrapper) {
|
|
192
|
+
this._clientList.push(metaOptionsWrapper);
|
|
193
|
+
}
|
|
194
|
+
removeMetaOptions(metaOptionsWrapper) {
|
|
195
|
+
this._clientList.splice(this._clientList.indexOf(metaOptionsWrapper), 1);
|
|
196
|
+
}
|
|
197
|
+
planClientUpdate() {
|
|
198
|
+
if (this._updateId !== 0) clearTimeout(this._updateId);
|
|
199
|
+
this._updateId = window.setTimeout(() => {
|
|
200
|
+
this._updateId = 0;
|
|
201
|
+
this._updateClientMeta();
|
|
202
|
+
}, 50);
|
|
203
|
+
}
|
|
204
|
+
_updateClientMeta() {
|
|
205
|
+
const data = {
|
|
206
|
+
title: "",
|
|
207
|
+
titleTemplate: void 0,
|
|
208
|
+
meta: {},
|
|
209
|
+
link: {},
|
|
210
|
+
script: {},
|
|
211
|
+
htmlAttr: {},
|
|
212
|
+
bodyAttr: {}
|
|
213
|
+
};
|
|
214
|
+
for (let i = 0; i < this._clientList.length; i++) {
|
|
215
|
+
const { active, val } = this._clientList[i];
|
|
216
|
+
if (active === true) extend(true, data, val);
|
|
217
|
+
}
|
|
218
|
+
normalize(data);
|
|
219
|
+
apply(diff(this._currentClientMeta, data));
|
|
220
|
+
this._currentClientMeta = data;
|
|
221
|
+
}
|
|
222
|
+
_injectServerMeta(ssrContext) {
|
|
223
|
+
const data = {
|
|
224
|
+
title: "",
|
|
225
|
+
titleTemplate: void 0,
|
|
226
|
+
meta: {},
|
|
227
|
+
link: {},
|
|
228
|
+
htmlAttr: {},
|
|
229
|
+
bodyAttr: {},
|
|
230
|
+
bodyStyle: {},
|
|
231
|
+
bodyClass: {},
|
|
232
|
+
noscript: {}
|
|
233
|
+
};
|
|
234
|
+
const list = ssrContext.__qMetaList;
|
|
235
|
+
for (let i = 0; i < list.length; i++) extend(true, data, list[i]);
|
|
236
|
+
normalize(data);
|
|
237
|
+
const nonce = ssrContext.nonce !== void 0 ? ` nonce="${ssrContext.nonce}"` : "";
|
|
238
|
+
const ctx = ssrContext._meta;
|
|
239
|
+
const htmlAttr = Object.keys(data.htmlAttr).filter(htmlFilter);
|
|
240
|
+
if (htmlAttr.length !== 0) ctx.htmlAttrs += (ctx.htmlAttrs.length !== 0 ? " " : "") + htmlAttr.map(getAttr(data.htmlAttr)).join(" ");
|
|
241
|
+
ctx.headTags += getHead(data);
|
|
242
|
+
const bodyAttr = Object.keys(data.bodyAttr).filter(bodyFilter);
|
|
243
|
+
if (bodyAttr.length !== 0) ctx.bodyAttrs += (ctx.bodyAttrs.length !== 0 ? " " : "") + bodyAttr.map(getAttr(data.bodyAttr)).join(" ");
|
|
244
|
+
const bodyStyle = Object.keys(data.bodyStyle).filter((name) => !!data.bodyStyle[name]).map((name) => `${name}:${data.bodyStyle[name]};`).join("");
|
|
245
|
+
if (bodyStyle) ctx.bodyAttrs += `${ctx.bodyAttrs.length !== 0 ? " " : ""}style="${bodyStyle}"`;
|
|
246
|
+
const _bodyClass = ctx.bodyClasses.split(" ").filter((item) => !!item);
|
|
247
|
+
const bodyClass = {};
|
|
248
|
+
_bodyClass.forEach((item) => bodyClass[item] = true);
|
|
249
|
+
extend(true, bodyClass, data.bodyClass);
|
|
250
|
+
ctx.bodyClasses = Object.keys(bodyClass).filter((name) => bodyClass[name]).map((name) => name).join(" ");
|
|
251
|
+
data.title = "'\"`";
|
|
252
|
+
ctx.endingHeadTags += `${Object.keys(data.noscript).map((name) => `<noscript data-qmeta="${name}">${data.noscript[name]}</noscript>`).join("")}<script${nonce} id="ssr-meta-init">window.__Q_META__=${delete data.bodyStyle && delete data.bodyClass && delete data.noscript && unevalPatch(data)}<\/script>`;
|
|
253
|
+
let ssr_local_themedark = this.sys.env.SSR_COOKIE_THEME === "true" ? `let ssr_cookie_themedark=document.cookie.split('; ')?.find(item=>item.indexOf('themedark=')>-1)?.split('=')[1];
|
|
254
|
+
ssr_cookie_themedark=ssr_cookie_themedark==='true'?true:ssr_cookie_themedark==='false'?false:${this.sys.env.SSR_COOKIE_THEMEDARK_DEFAULT};
|
|
255
|
+
window.ssr_themedark=window.ssr_cookie_themedark=ssr_cookie_themedark;` : `let ssr_local_themedark=window.ssr_load_local('themedark');
|
|
256
|
+
if(ssr_local_themedark===undefined || ssr_local_themedark==='auto'){
|
|
257
|
+
ssr_local_themedark=window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
258
|
+
}
|
|
259
|
+
window.ssr_themedark=window.ssr_local_themedark=ssr_local_themedark;`;
|
|
260
|
+
ssr_local_themedark += `Object.defineProperty(window, 'ssr_themedark_data', {
|
|
261
|
+
get: () => {
|
|
262
|
+
let _data=document.body.getAttribute('data-ssr-theme-dark-'+window.ssr_themedark);
|
|
263
|
+
if(_data===undefined || _data===null){
|
|
264
|
+
_data=window.__INITIAL_STATE__ && window.__INITIAL_STATE__['data-ssr-theme-dark-'+window.ssr_themedark];
|
|
265
|
+
}
|
|
266
|
+
return _data;
|
|
267
|
+
},
|
|
268
|
+
});`;
|
|
269
|
+
const ssr_local_themename = this.sys.env.SSR_COOKIE_THEME === "true" ? "" : "window.ssr_local_themename=window.ssr_load_local('themename');";
|
|
270
|
+
ctx.endingHeadTags += `<script id="ssr-prefers-color-schema-dark">
|
|
271
|
+
window.ssr_load_local=function(key){
|
|
272
|
+
const __ssr_local=localStorage.getItem(key);
|
|
273
|
+
return __ssr_local?JSON.parse(__ssr_local):undefined;
|
|
274
|
+
};
|
|
275
|
+
${ssr_local_themedark}
|
|
276
|
+
${ssr_local_themename}
|
|
277
|
+
document.querySelector('#ssr-prefers-color-schema-dark').remove();
|
|
278
|
+
<\/script>`.replaceAll("\n", "");
|
|
279
|
+
if (this.sys.env.SSR_BODYREADYOBSERVER === "true") ctx.bodyTags += `<script id="ssr-body-ready-observer">
|
|
280
|
+
window.ssr_bodyReadyObserverClear=()=>{
|
|
281
|
+
if(window.ssr_bodyReadyObserver){
|
|
282
|
+
window.ssr_body_ready_condition=undefined;
|
|
283
|
+
window.ssr_body_ready_callback=undefined;
|
|
284
|
+
window.ssr_bodyReadyObserver.disconnect();
|
|
285
|
+
window.ssr_bodyReadyObserver=undefined;
|
|
286
|
+
document.body.style.display='block';
|
|
287
|
+
document.querySelector('#ssr-body-ready-observer').remove();
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
window.ssr_bodyReadyObserver = new MutationObserver(() => {
|
|
291
|
+
if(window.ssr_body_ready_condition && window.ssr_body_ready_condition()){
|
|
292
|
+
window.ssr_body_ready_callback();
|
|
293
|
+
window.ssr_bodyReadyObserverClear();
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
window.ssr_bodyReadyObserver.observe(document.body, {
|
|
297
|
+
subtree: true,
|
|
298
|
+
childList: true,
|
|
299
|
+
});
|
|
300
|
+
document.addEventListener("DOMContentLoaded", () => {
|
|
301
|
+
window.ssr_bodyReadyObserverClear();
|
|
302
|
+
});
|
|
303
|
+
<\/script>`.replaceAll("\n", "");
|
|
304
|
+
}
|
|
305
|
+
_injectContextState(ssrContext) {
|
|
306
|
+
const ctx = ssrContext._meta;
|
|
307
|
+
const nonce = ssrContext.nonce !== void 0 ? ` nonce="${ssrContext.nonce}"` : "";
|
|
308
|
+
ctx.endingHeadTags += `<script${nonce} id="ssr-state-init">window.__INITIAL_STATE__=${unevalPatch(ssrContext.state)}<\/script>`;
|
|
309
|
+
}
|
|
310
|
+
_injectContextStateDefer(ssrContext) {
|
|
311
|
+
const ctx = ssrContext._meta;
|
|
312
|
+
const nonce = ssrContext.nonce !== void 0 ? ` nonce="${ssrContext.nonce}"` : "";
|
|
313
|
+
ctx.endingBodyTags += `<script${nonce} id="ssr-state-defer-init">window.__INITIAL_STATE_DEFER__=${unevalPatch(ssrContext.stateDefer)}<\/script>`;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
function normalize(meta) {
|
|
317
|
+
if (meta.title) {
|
|
318
|
+
meta.title = meta.titleTemplate ? meta.titleTemplate(meta.title) : meta.title;
|
|
319
|
+
delete meta.titleTemplate;
|
|
320
|
+
}
|
|
321
|
+
[["meta", "content"], ["link", "href"]].forEach((type) => {
|
|
322
|
+
const metaType = meta[type[0]];
|
|
323
|
+
const metaProp = type[1];
|
|
324
|
+
for (const name in metaType) {
|
|
325
|
+
const metaLink = metaType[name];
|
|
326
|
+
if (metaLink.template) if (Object.keys(metaLink).length === 1) delete metaType[name];
|
|
327
|
+
else {
|
|
328
|
+
metaLink[metaProp] = metaLink.template(metaLink[metaProp] || "");
|
|
329
|
+
delete metaLink.template;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
function changed(old, def) {
|
|
335
|
+
if (Object.keys(old).length !== Object.keys(def).length) return true;
|
|
336
|
+
for (const key in old) if (old[key] !== def[key]) return true;
|
|
337
|
+
}
|
|
338
|
+
function bodyFilter(name) {
|
|
339
|
+
return ["class", "style"].includes(name) === false;
|
|
340
|
+
}
|
|
341
|
+
function htmlFilter(_name) {
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
function diff(meta, other) {
|
|
345
|
+
const add = {};
|
|
346
|
+
const remove = {};
|
|
347
|
+
if (meta === void 0) return {
|
|
348
|
+
add: other,
|
|
349
|
+
remove
|
|
350
|
+
};
|
|
351
|
+
if (meta.title !== other.title) add.title = other.title;
|
|
352
|
+
[
|
|
353
|
+
"meta",
|
|
354
|
+
"link",
|
|
355
|
+
"script",
|
|
356
|
+
"htmlAttr",
|
|
357
|
+
"bodyAttr"
|
|
358
|
+
].forEach((type) => {
|
|
359
|
+
const old = meta[type];
|
|
360
|
+
const cur = other[type];
|
|
361
|
+
remove[type] = [];
|
|
362
|
+
if (old === void 0 || old === null) {
|
|
363
|
+
add[type] = cur;
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
add[type] = {};
|
|
367
|
+
for (const key in old) if (Object.prototype.hasOwnProperty.call(cur, key) === false) remove[type].push(key);
|
|
368
|
+
for (const key in cur) if (Object.prototype.hasOwnProperty.call(old, key) === false) add[type][key] = cur[key];
|
|
369
|
+
else if (changed(old[key], cur[key]) === true) {
|
|
370
|
+
remove[type].push(key);
|
|
371
|
+
add[type][key] = cur[key];
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
return {
|
|
375
|
+
add,
|
|
376
|
+
remove
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
function apply({ add, remove }) {
|
|
380
|
+
if (add.title) document.title = add.title;
|
|
381
|
+
if (Object.keys(remove).length !== 0) {
|
|
382
|
+
[
|
|
383
|
+
"meta",
|
|
384
|
+
"link",
|
|
385
|
+
"script"
|
|
386
|
+
].forEach((type) => {
|
|
387
|
+
remove[type].forEach((name) => {
|
|
388
|
+
document.head.querySelector(`${type}[data-qmeta="${name}"]`)?.remove();
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
remove.htmlAttr.filter(htmlFilter).forEach((name) => {
|
|
392
|
+
document.documentElement.removeAttribute(name);
|
|
393
|
+
});
|
|
394
|
+
remove.bodyAttr.filter(bodyFilter).forEach((name) => {
|
|
395
|
+
document.body.removeAttribute(name);
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
[
|
|
399
|
+
"meta",
|
|
400
|
+
"link",
|
|
401
|
+
"script"
|
|
402
|
+
].forEach((type) => {
|
|
403
|
+
const metaType = add[type];
|
|
404
|
+
for (const name in metaType) {
|
|
405
|
+
const tag = document.createElement(type);
|
|
406
|
+
for (const att in metaType[name]) if (att !== "innerHTML") tag.setAttribute(att, metaType[name][att]);
|
|
407
|
+
tag.setAttribute("data-qmeta", name);
|
|
408
|
+
if (type === "script") tag.innerHTML = metaType[name].innerHTML || "";
|
|
409
|
+
document.head.appendChild(tag);
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
Object.keys(add.htmlAttr).filter(htmlFilter).forEach((name) => {
|
|
413
|
+
document.documentElement.setAttribute(name, add.htmlAttr[name] || "");
|
|
414
|
+
});
|
|
415
|
+
Object.keys(add.bodyAttr).filter(bodyFilter).forEach((name) => {
|
|
416
|
+
document.body.setAttribute(name, add.bodyAttr[name] || "");
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
function getAttr(seed) {
|
|
420
|
+
return (att) => {
|
|
421
|
+
const val = seed[att];
|
|
422
|
+
return att + (val !== true && val !== void 0 ? `="${val}"` : "");
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
function getHead(meta) {
|
|
426
|
+
let output = "";
|
|
427
|
+
if (meta.title) output += `<title>${meta.title}</title>`;
|
|
428
|
+
[
|
|
429
|
+
"meta",
|
|
430
|
+
"link",
|
|
431
|
+
"script"
|
|
432
|
+
].forEach((type) => {
|
|
433
|
+
const metaType = meta[type];
|
|
434
|
+
for (const att in metaType) {
|
|
435
|
+
const attrs = Object.keys(metaType[att]).filter((att) => att !== "innerHTML").map(getAttr(metaType[att]));
|
|
436
|
+
output += `<${type} ${attrs.join(" ")} data-qmeta="${att}">`;
|
|
437
|
+
if (type === "script") output += `${metaType[att].innerHTML || ""}<\/script>`;
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
return output;
|
|
441
|
+
}
|
|
442
|
+
//#endregion
|
|
443
|
+
//#region src/lib/ssr.ts
|
|
444
|
+
var SymbolIsRuntimeSsrPreHydration = Symbol("SymbolIsRuntimeSsrPreHydration");
|
|
445
|
+
var SymbolSSRContext = Symbol("SymbolSSRContext");
|
|
446
|
+
var SymbolSSRState = Symbol("SymbolSSRState");
|
|
447
|
+
var SymbolOnHydrateds = Symbol("SymbolOnHydrateds");
|
|
448
|
+
var SymbolOnHydratePropHasMismatches = Symbol("SymbolOnHydratePropHasMismatches");
|
|
449
|
+
var SymbolInstanceUpdates = Symbol("SymbolInstanceUpdates");
|
|
450
|
+
var SymbolHydratingCounter = Symbol("SymbolHydratingCounter");
|
|
451
|
+
var CtxSSR = class extends BeanSimple {
|
|
452
|
+
constructor(...args) {
|
|
453
|
+
super(...args);
|
|
454
|
+
this[SymbolIsRuntimeSsrPreHydration] = ref(false);
|
|
455
|
+
this[SymbolSSRContext] = void 0;
|
|
456
|
+
this[SymbolSSRState] = void 0;
|
|
457
|
+
this[SymbolOnHydrateds] = [];
|
|
458
|
+
this[SymbolOnHydratePropHasMismatches] = [];
|
|
459
|
+
this[SymbolInstanceUpdates] = [];
|
|
460
|
+
this[SymbolHydratingCounter] = 0;
|
|
461
|
+
this.metaStore = void 0;
|
|
462
|
+
}
|
|
463
|
+
/** @internal */
|
|
464
|
+
initialize() {
|
|
465
|
+
this[SymbolSSRState] = this.sys.bean._getBeanSyncOnly("a-ssr.sys.ssrState");
|
|
466
|
+
if (process.env.SERVER) this[SymbolIsRuntimeSsrPreHydration].value = true;
|
|
467
|
+
else if (process.env.CLIENT && document.body.getAttribute("data-server-rendered") !== null) this[SymbolIsRuntimeSsrPreHydration].value = true;
|
|
468
|
+
if (process.env.CLIENT && this.isRuntimeSsrPreHydration) this.onHydratePropHasMismatch((el, key, clientValue, vnode, instance) => {
|
|
469
|
+
return this._onHydratePropHasMismatchDefault(el, key, clientValue, vnode, instance);
|
|
470
|
+
});
|
|
471
|
+
this.metaStore = this.bean._newBeanSimple(CtxSSRMetaStore, false);
|
|
472
|
+
if (process.env.CLIENT && this.isRuntimeSsrPreHydration) this.onHydrated(() => {
|
|
473
|
+
document.querySelectorAll("style[vite-css-module-id]").forEach((node) => node.remove());
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
get isRuntimeSsrPreHydration() {
|
|
477
|
+
return this[SymbolIsRuntimeSsrPreHydration].value;
|
|
478
|
+
}
|
|
479
|
+
set isRuntimeSsrPreHydration(value) {
|
|
480
|
+
this[SymbolIsRuntimeSsrPreHydration].value = value;
|
|
481
|
+
}
|
|
482
|
+
get context() {
|
|
483
|
+
if (process.env.CLIENT) throw new Error("cannot called in client");
|
|
484
|
+
if (!this[SymbolSSRContext]) this.ctx.util.instanceScope(() => {
|
|
485
|
+
this[SymbolSSRContext] = useSSRContext();
|
|
486
|
+
this._initContext();
|
|
487
|
+
});
|
|
488
|
+
return this[SymbolSSRContext];
|
|
489
|
+
}
|
|
490
|
+
get state() {
|
|
491
|
+
if (process.env.SERVER) return this.context.state;
|
|
492
|
+
else return this[SymbolSSRState].state;
|
|
493
|
+
}
|
|
494
|
+
get stateDefer() {
|
|
495
|
+
if (process.env.SERVER) return this.context.stateDefer;
|
|
496
|
+
else return this[SymbolSSRState].stateDefer;
|
|
497
|
+
}
|
|
498
|
+
getPerformAction(baseURL) {
|
|
499
|
+
if (process.env.SERVER && baseURL === this.sys.env.SSR_API_BASE_URL) return this.context.performAction;
|
|
500
|
+
}
|
|
501
|
+
_initContext() {
|
|
502
|
+
const ssrContext = this[SymbolSSRContext];
|
|
503
|
+
ssrContext._meta = defu(ssrContext._meta, {
|
|
504
|
+
htmlAttrs: "",
|
|
505
|
+
headTags: "",
|
|
506
|
+
endingHeadTags: "",
|
|
507
|
+
bodyClasses: "",
|
|
508
|
+
bodyAttrs: "data-server-rendered",
|
|
509
|
+
bodyTags: "",
|
|
510
|
+
endingBodyTags: "",
|
|
511
|
+
baseUrl: this.sys.util.getAbsoluteUrlFromPagePath(void 0, false, true)
|
|
512
|
+
});
|
|
513
|
+
ssrContext.state = ssrContext.state || {};
|
|
514
|
+
ssrContext.stateDefer = ssrContext.stateDefer || {};
|
|
515
|
+
}
|
|
516
|
+
onHydrated(fn) {
|
|
517
|
+
this[SymbolOnHydrateds].push(fn);
|
|
518
|
+
}
|
|
519
|
+
onHydratePropHasMismatch(fn) {
|
|
520
|
+
this[SymbolOnHydratePropHasMismatches].push(fn);
|
|
521
|
+
}
|
|
522
|
+
handleDirectOrOnHydrated(fn) {
|
|
523
|
+
if (process.env.CLIENT && this.isRuntimeSsrPreHydration) this.onHydrated(fn);
|
|
524
|
+
else return fn();
|
|
525
|
+
}
|
|
526
|
+
_onHydratePropHasMismatchDefault(el, key, clientValue, _vnode, _instance) {
|
|
527
|
+
let ignore = false;
|
|
528
|
+
let expected;
|
|
529
|
+
if (key === "class") {
|
|
530
|
+
ignore = true;
|
|
531
|
+
if (clientValue !== void 0) {
|
|
532
|
+
expected = normalizeClass(clientValue);
|
|
533
|
+
el.setAttribute(key, expected);
|
|
534
|
+
}
|
|
535
|
+
} else if (key === "style") {
|
|
536
|
+
ignore = true;
|
|
537
|
+
if (clientValue !== void 0) {
|
|
538
|
+
expected = isString(clientValue) ? clientValue : stringifyStyle(normalizeStyle(clientValue));
|
|
539
|
+
el.setAttribute(key, expected);
|
|
540
|
+
}
|
|
541
|
+
} else if (key === "for") {
|
|
542
|
+
ignore = true;
|
|
543
|
+
if (clientValue !== void 0) {
|
|
544
|
+
expected = String(clientValue);
|
|
545
|
+
el.setAttribute(key, expected);
|
|
546
|
+
}
|
|
547
|
+
} else if (key === "id") {
|
|
548
|
+
ignore = true;
|
|
549
|
+
if (clientValue !== void 0) {
|
|
550
|
+
expected = String(clientValue);
|
|
551
|
+
el.setAttribute(key, expected);
|
|
552
|
+
}
|
|
553
|
+
} else if (key === "d") {
|
|
554
|
+
ignore = true;
|
|
555
|
+
if (clientValue !== void 0) {
|
|
556
|
+
expected = String(clientValue);
|
|
557
|
+
el.setAttribute(key, expected);
|
|
558
|
+
}
|
|
559
|
+
} else if (key === "value") {
|
|
560
|
+
ignore = true;
|
|
561
|
+
if (clientValue !== void 0) {
|
|
562
|
+
expected = String(clientValue);
|
|
563
|
+
if (el.tagName === "TEXTAREA") el.value = expected;
|
|
564
|
+
else el.setAttribute(key, expected);
|
|
565
|
+
}
|
|
566
|
+
} else if (isBooleanAttr(key)) {
|
|
567
|
+
ignore = true;
|
|
568
|
+
if (clientValue !== void 0) if (includeBooleanAttr(clientValue)) el.setAttribute(key, "");
|
|
569
|
+
else el.removeAttribute(key);
|
|
570
|
+
} else if (el.getAttribute(`data-hydrate-ignore-${key}`) !== null) {
|
|
571
|
+
ignore = true;
|
|
572
|
+
if (clientValue !== void 0) {
|
|
573
|
+
expected = String(clientValue);
|
|
574
|
+
el.setAttribute(key, expected);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
if (!ignore) return { clientValue };
|
|
578
|
+
return { ignore: true };
|
|
579
|
+
}
|
|
580
|
+
_hydrated() {
|
|
581
|
+
if (!this.isRuntimeSsrPreHydration) return;
|
|
582
|
+
this.isRuntimeSsrPreHydration = false;
|
|
583
|
+
this[SymbolInstanceUpdates].forEach((instance) => {
|
|
584
|
+
if (!instance.isUnmounted && instance.zova) try {
|
|
585
|
+
instance.update();
|
|
586
|
+
} catch (err) {
|
|
587
|
+
if (!err.message.includes("'insertBefore'")) throw err;
|
|
588
|
+
}
|
|
589
|
+
});
|
|
590
|
+
this[SymbolInstanceUpdates] = [];
|
|
591
|
+
this[SymbolOnHydrateds].forEach((fn) => fn());
|
|
592
|
+
this[SymbolOnHydrateds] = [];
|
|
593
|
+
this[SymbolOnHydratePropHasMismatches] = [];
|
|
594
|
+
}
|
|
595
|
+
/** @internal */
|
|
596
|
+
_hydratePropHasMismatch(el, key, clientValue, vnode, instance) {
|
|
597
|
+
for (const fn of this[SymbolOnHydratePropHasMismatches]) {
|
|
598
|
+
const res = fn(el, key, clientValue, vnode, instance);
|
|
599
|
+
if (res.ignore) return res;
|
|
600
|
+
clientValue = res.clientValue;
|
|
601
|
+
}
|
|
602
|
+
return {
|
|
603
|
+
ignore: false,
|
|
604
|
+
clientValue
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
/** @internal */
|
|
608
|
+
_hydratingInc() {
|
|
609
|
+
++this[SymbolHydratingCounter];
|
|
610
|
+
}
|
|
611
|
+
/** @internal */
|
|
612
|
+
_hydratingDec() {
|
|
613
|
+
if (--this[SymbolHydratingCounter] === 0) this._hydrated();
|
|
614
|
+
}
|
|
615
|
+
/** @internal */
|
|
616
|
+
_hydratingInstanceRecord(instance) {
|
|
617
|
+
if (!this[SymbolInstanceUpdates].includes(instance)) {
|
|
618
|
+
this[SymbolInstanceUpdates].push(instance);
|
|
619
|
+
return true;
|
|
620
|
+
}
|
|
621
|
+
return false;
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
//#endregion
|
|
625
|
+
//#region src/monkeySys.ts
|
|
626
|
+
var MonkeySys = class extends BeanSimple {
|
|
627
|
+
constructor(moduleSelf) {
|
|
628
|
+
super();
|
|
629
|
+
this._moduleSelf = void 0;
|
|
630
|
+
this._sysSsrState = void 0;
|
|
631
|
+
this._moduleSelf = moduleSelf;
|
|
632
|
+
}
|
|
633
|
+
async getSysSsrState() {
|
|
634
|
+
if (!this._sysSsrState) this._sysSsrState = await this.bean._getBean("a-ssr.sys.ssrState", false);
|
|
635
|
+
return this._sysSsrState;
|
|
636
|
+
}
|
|
637
|
+
async moduleLoading(module) {
|
|
638
|
+
if (this._moduleSelf === module) return;
|
|
639
|
+
await this.getSysSsrState();
|
|
640
|
+
}
|
|
641
|
+
async moduleLoaded(_module) {}
|
|
642
|
+
async configLoaded(_module, _config) {}
|
|
643
|
+
sysContextInitialize(ctx) {
|
|
644
|
+
ctx.meta.$ssr = ctx.bean._newBeanSimple(CtxSSR, false);
|
|
645
|
+
ctx.meta.$ssr.initialize();
|
|
646
|
+
}
|
|
647
|
+
};
|
|
648
|
+
//#endregion
|
|
649
|
+
//#region src/.metadata/index.ts
|
|
650
|
+
/** monkeySys: end */
|
|
651
|
+
/** scope: begin */
|
|
652
|
+
var _dec, _dec2, _class;
|
|
653
|
+
var ScopeModuleASsr = (_dec = Scope(), _dec2 = BeanInfo({ module: "a-ssr" }), _dec(_class = _dec2(_class = class ScopeModuleASsr extends BeanScopeBase {}) || _class) || _class);
|
|
654
|
+
/** scope: end */
|
|
655
|
+
//#endregion
|
|
656
|
+
export { CtxSSR, CtxSSRMetaStore, Monkey, MonkeySys, ScopeModuleASsr, SysSsrState, config, unevalPatch };
|
|
657
|
+
|
|
658
|
+
//# sourceMappingURL=index.js.map
|