zova-module-a-ssrserver 5.1.15 → 5.1.19
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/dist/index.js +55 -20
- package/dist/index.js.map +1 -1
- package/dist/service/ssrHandler.d.ts +1 -0
- package/dist/service/ssrHandler.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/service/ssrHandler.ts +70 -29
package/dist/index.js
CHANGED
|
@@ -69,26 +69,58 @@ var ServiceSsrHandler = (_dec$1 = Service(), _dec2$1 = BeanInfo({ module: "a-ssr
|
|
|
69
69
|
onRenderedList.push(fn);
|
|
70
70
|
}
|
|
71
71
|
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (
|
|
83
|
-
|
|
72
|
+
let renderFn;
|
|
73
|
+
let runtimePageContent = "";
|
|
74
|
+
let renderedError;
|
|
75
|
+
let onRenderedFlushed = false;
|
|
76
|
+
let renderedFinalized = false;
|
|
77
|
+
try {
|
|
78
|
+
renderFn = await serverEntry(ssrContext);
|
|
79
|
+
const [runtimePageContentInner, err] = await catchError(() => {
|
|
80
|
+
return renderToString(renderFn, ssrContext);
|
|
81
|
+
});
|
|
82
|
+
if (runtimePageContentInner) runtimePageContent = runtimePageContentInner;
|
|
83
|
+
const error = ssrContext._meta.renderError ?? err;
|
|
84
|
+
renderedError = error;
|
|
85
|
+
onRenderedList.forEach((fn) => {
|
|
86
|
+
fn(error);
|
|
87
|
+
});
|
|
88
|
+
onRenderedFlushed = true;
|
|
89
|
+
cast(ssrContext).rendered(error);
|
|
90
|
+
renderedFinalized = true;
|
|
91
|
+
if (error) {
|
|
92
|
+
if (error instanceof Error) throw error;
|
|
93
|
+
return error;
|
|
94
|
+
}
|
|
95
|
+
ssrContext._meta.runtimePageContent = runtimePageContent;
|
|
96
|
+
// @vitejs/plugin-vue injects code into a component's setup() that registers
|
|
97
|
+
const modules = cast(ssrContext).modules;
|
|
98
|
+
if (modules) {
|
|
99
|
+
ssrContext._meta.endingHeadTags += this._renderModulesPreload_zova(modules, { ssrContext });
|
|
100
|
+
ssrContext._meta.endingHeadTags += this._renderModulesPreload(modules, { ssrContext });
|
|
101
|
+
}
|
|
102
|
+
const html = renderTemplate(ssrContext);
|
|
103
|
+
await this._renderTransferCache(options, route);
|
|
104
|
+
return html;
|
|
105
|
+
} finally {
|
|
106
|
+
const context = cast(ssrContext);
|
|
107
|
+
if (!onRenderedFlushed) onRenderedList.forEach((fn) => {
|
|
108
|
+
fn(renderedError);
|
|
109
|
+
});
|
|
110
|
+
if (!renderedFinalized && typeof context.rendered === "function") context.rendered(renderedError);
|
|
111
|
+
onRenderedList.length = 0;
|
|
112
|
+
this._clearSsrContext(context);
|
|
113
|
+
renderFn = void 0;
|
|
84
114
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
115
|
+
}
|
|
116
|
+
_clearSsrContext(context) {
|
|
117
|
+
context.modules?.clear?.();
|
|
118
|
+
if (context.__qMetaList?.length) context.__qMetaList.length = 0;
|
|
119
|
+
context.state = void 0;
|
|
120
|
+
context.stateDefer = void 0;
|
|
121
|
+
context.performAction = void 0;
|
|
122
|
+
context.rendered = void 0;
|
|
123
|
+
context._meta = void 0;
|
|
92
124
|
}
|
|
93
125
|
async ensureReady(handlerNonce) {
|
|
94
126
|
if (!this._handlerInstance) {
|
|
@@ -98,7 +130,10 @@ var ServiceSsrHandler = (_dec$1 = Service(), _dec2$1 = BeanInfo({ module: "a-ssr
|
|
|
98
130
|
return this._handlerInstance;
|
|
99
131
|
}
|
|
100
132
|
async _prepareHandler(handlerNonce) {
|
|
101
|
-
const handlerInstance = await import(
|
|
133
|
+
const handlerInstance = await import(
|
|
134
|
+
/* @vite-ignore */
|
|
135
|
+
`${pathToHref(path.join(this._siteAssetDir, "handler.js"))}?${handlerNonce}`
|
|
136
|
+
);
|
|
102
137
|
const fileManifest = path.join(this._siteAssetDir, "quasar.manifest.json");
|
|
103
138
|
const contentManifest = await fse.readFile(fileManifest, { encoding: "utf-8" });
|
|
104
139
|
this._clientManifest = JSON.parse(contentManifest);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/service/ssrHandler.ts","../src/monkeySys.ts","../src/.metadata/index.ts"],"sourcesContent":["import { catchError } from '@cabloy/utils';\nimport { RouteLocationResolvedGeneric } from '@cabloy/vue-router';\nimport fse from 'fs-extra';\nimport ms from 'ms';\nimport path, { basename } from 'node:path';\nimport { pathToFileURL } from 'node:url';\nimport { BeanBase, cast, Use, UseScope } from 'zova';\nimport { Service } from 'zova-module-a-bean';\nimport { SysRouter } from 'zova-module-a-router';\nimport { ScopeModuleASsr } from 'zova-module-a-ssr';\n\nimport { ISsrHandlerRenderOptionsInner, TypeEventResolvePathResult } from '../types/ssr.js';\n\nconst jsRE = /\\.js$/;\nconst cssRE = /\\.css$/;\nconst woffRE = /\\.woff$/;\nconst woff2RE = /\\.woff2$/;\nconst gifRE = /\\.gif$/;\nconst jpgRE = /\\.jpe?g$/;\nconst pngRE = /\\.png$/;\n\n@Service()\nexport class ServiceSsrHandler extends BeanBase {\n private _siteAssetDir: string;\n private _handlerPromise: Promise<any>;\n private _handlerInstance: any;\n private _clientManifest: any;\n private _ssrModulesZovaCache: any = {};\n\n @Use()\n $$sysRouter: SysRouter;\n\n @UseScope()\n $$scopeSsr: ScopeModuleASsr;\n\n protected __init__(siteAssetDir: string) {\n this._siteAssetDir = siteAssetDir;\n }\n\n protected __dispose__() {\n this.dispose();\n }\n\n public dispose() {\n this.sys.close();\n this._handlerInstance = undefined;\n }\n\n async resolvePath(filename: string): Promise<TypeEventResolvePathResult> {\n if (filename === '') filename = 'index.html';\n // assets\n const fileAsset = path.join(this._siteAssetDir, 'client', filename);\n if (await fse.exists(fileAsset)) return fileAsset;\n // not found\n return undefined;\n }\n\n public async render(options: ISsrHandlerRenderOptionsInner) {\n // resolve route\n const pagePathFull =\n options.state?.pagePathFull ?? this.sys.util.getPagePathFromAbsoluteUrl(options.req.url!);\n const route = await this.$$sysRouter.resolveRoute(pagePathFull, true, false);\n if (!route) return;\n // handler\n const { serverEntry, renderToString, renderTemplate } = this._handlerInstance;\n // ssrContext\n const onRenderedList: Function[] = [];\n const ssrContext = {\n ...options,\n _meta: {} as any,\n onRendered: fn => {\n onRenderedList.push(fn);\n },\n };\n // render\n const renderFn = await serverEntry(ssrContext);\n const [runtimePageContent, err] = await catchError(() => {\n return renderToString(renderFn, ssrContext);\n });\n\n const error = ssrContext._meta.renderError ?? err;\n onRenderedList.forEach(fn => {\n fn(error);\n });\n cast(ssrContext).rendered(error);\n if (error) {\n if (error instanceof Error) throw error;\n return error;\n }\n\n ssrContext._meta.runtimePageContent = runtimePageContent;\n\n // @vitejs/plugin-vue injects code into a component's setup() that registers\n // itself on ctx.modules. After the render, ctx.modules would contain all the\n // components that have been instantiated during this render call.\n ssrContext._meta.endingHeadTags += this._renderModulesPreload_zova(cast(ssrContext).modules, {\n ssrContext,\n });\n ssrContext._meta.endingHeadTags += this._renderModulesPreload(cast(ssrContext).modules, {\n ssrContext,\n });\n\n const html = renderTemplate(ssrContext);\n\n // transferCache\n await this._renderTransferCache(options, route);\n\n // todo: ssg\n\n return html;\n }\n\n public async ensureReady(handlerNonce: number) {\n if (!this._handlerInstance) {\n if (!this._handlerPromise) {\n this._handlerPromise = this._prepareHandler(handlerNonce);\n }\n this._handlerInstance = await this._handlerPromise;\n }\n return this._handlerInstance;\n }\n\n private async _prepareHandler(handlerNonce: number) {\n // handler\n const fileHandler = path.join(this._siteAssetDir, 'handler.js');\n const fileUrl = `${pathToHref(fileHandler)}?${handlerNonce}`;\n const handlerInstance = await import(/* @vite-ignore */ fileUrl);\n // clientManifest\n const fileManifest = path.join(this._siteAssetDir, 'quasar.manifest.json');\n const contentManifest = await fse.readFile(fileManifest, { encoding: 'utf-8' });\n this._clientManifest = JSON.parse(contentManifest);\n // ok\n return handlerInstance;\n }\n\n private _renderModulesPreload(modules, opts) {\n let links = '';\n const seen = new Set();\n\n modules.forEach(id => {\n const files = this._clientManifest[id];\n if (files === void 0) return;\n\n files.forEach(file => {\n if (seen.has(file) === true) return;\n\n seen.add(file);\n const filename = basename(file);\n\n if (this._clientManifest[filename] !== void 0) {\n for (const depFile of this._clientManifest[filename]) {\n if (seen.has(depFile) === false) {\n links += this._renderPreloadTag(depFile, opts);\n seen.add(depFile);\n }\n }\n }\n\n links += this._renderPreloadTag(file, opts);\n });\n });\n\n return links;\n }\n\n private _renderModulesPreload_zova(modules2, opts) {\n let links = '';\n const seen = /* @__PURE__ */ new Set();\n modules2.forEach(id => {\n if (!id.startsWith('@@')) return;\n if (seen.has(id) === true) return;\n let cache = this._ssrModulesZovaCache[id];\n if (!cache) {\n for (const key in this._clientManifest) {\n const prefix = `${id.substring(2)}-`;\n const postfix = '.js';\n if (key.startsWith(prefix) && key.endsWith(postfix)) {\n cache = this._resolveUrlPath(`/assets/${key}`);\n break;\n }\n }\n this._ssrModulesZovaCache[id] = cache;\n }\n links += this._renderPreloadTag(cache, opts);\n seen.add(id);\n });\n return links;\n }\n\n private _resolveUrlPath(url: string) {\n let publicPath = this.sys.env.APP_PUBLIC_PATH;\n if (publicPath) publicPath = `/${publicPath}`;\n return publicPath + url;\n }\n\n private _renderPreloadTag(file: string, _opts) {\n file = this.sys.util.getAbsoluteUrlFromPagePath(file, false, true);\n if (jsRE.test(file) === true) {\n return `<link rel=\"modulepreload\" href=\"${file}\" crossorigin>`;\n }\n\n if (cssRE.test(file) === true) {\n return `<link rel=\"stylesheet\" href=\"${file}\" crossorigin>`;\n }\n\n if (woffRE.test(file) === true) {\n return `<link rel=\"preload\" href=\"${file}\" as=\"font\" type=\"font/woff\" crossorigin>`;\n }\n\n if (woff2RE.test(file) === true) {\n return `<link rel=\"preload\" href=\"${file}\" as=\"font\" type=\"font/woff2\" crossorigin>`;\n }\n\n if (gifRE.test(file) === true) {\n return `<link rel=\"preload\" href=\"${file}\" as=\"image\" type=\"image/gif\" crossorigin>`;\n }\n\n if (jpgRE.test(file) === true) {\n return `<link rel=\"preload\" href=\"${file}\" as=\"image\" type=\"image/jpeg\" crossorigin>`;\n }\n\n if (pngRE.test(file) === true) {\n return `<link rel=\"preload\" href=\"${file}\" as=\"image\" type=\"image/png\" crossorigin>`;\n }\n\n return '';\n }\n\n private async _renderTransferCache(\n options: ISsrHandlerRenderOptionsInner,\n route: RouteLocationResolvedGeneric,\n ) {\n const { res } = options;\n const transferCache = route.meta.transferCache ?? this.$$scopeSsr.config.transferCache;\n if (transferCache === false) return;\n // expires\n const transferCacheExpires = transferCache.expires ?? 0;\n const expires =\n typeof transferCacheExpires === 'string'\n ? ms(transferCacheExpires) / 1000\n : transferCacheExpires;\n if (expires === 0) {\n res.appendHeader('cache-control', 'no-cache, no-store, must-revalidate');\n } else {\n res.appendHeader('cache-control', `public, max-age=${expires}`);\n }\n }\n}\n\nexport function pathToHref(fileName: string): string {\n return pathToFileURL(fileName).href;\n // return Path.sep === '\\\\' ? pathToFileURL(fileName).href : fileName;\n}\n","import type { IMonkeySysInitialize } from 'zova';\n\nimport { BeanSimple } from 'zova';\n\nimport { ServiceSsrHandler } from './service/ssrHandler.js';\n\nexport class MonkeySys extends BeanSimple implements IMonkeySysInitialize {\n async sysInitialize() {\n this.sys.meta.$getSsrHandler = async (siteAssetDir: string): Promise<ServiceSsrHandler> => {\n return await this.sys.bean._getBean(ServiceSsrHandler, false, siteAssetDir);\n };\n }\n}\n","// eslint-disable\n/** service: begin */\nexport * from '../service/ssrHandler.js';\n\nimport 'zova-module-a-bean';\ndeclare module 'zova-module-a-bean' {\n \n export interface IServiceRecord {\n 'a-ssrserver:ssrHandler': never;\n }\n\n \n}\ndeclare module 'zova-module-a-ssrserver' {\n \n export interface ServiceSsrHandler {\n /** @internal */\n get scope(): ScopeModuleASsrserver;\n }\n\n export interface ServiceSsrHandler {\n get $beanFullName(): 'a-ssrserver.service.ssrHandler';\n get $onionName(): 'a-ssrserver:ssrHandler';\n \n } \n}\n/** service: end */\n/** service: begin */\nimport { ServiceSsrHandler } from '../service/ssrHandler.js';\nimport 'zova';\ndeclare module 'zova' {\n export interface IBeanRecordGeneral {\n 'a-ssrserver.service.ssrHandler': ServiceSsrHandler;\n }\n}\n/** service: end */\n/** monkeySys: begin */\nexport * from '../monkeySys.js';\n/** monkeySys: end */\n/** scope: begin */\nimport { BeanScopeBase, type BeanScopeUtil } from 'zova';\nimport { Scope } from 'zova-module-a-bean';\n\n@Scope()\nexport class ScopeModuleASsrserver extends BeanScopeBase {}\n\nexport interface ScopeModuleASsrserver {\n util: BeanScopeUtil;\n}\n\nimport 'zova';\ndeclare module 'zova' {\n export interface IBeanScopeRecord {\n 'a-ssrserver': ScopeModuleASsrserver;\n }\n \n \n\n \n\n \n}\n \n/** scope: end */\n"],"mappings":";;;;;;;;AAAA,IAAA,QAAS,SAAA,OAAa,OAAO,OAAO,OAAM,UAAA,SAAA,aAAA;AAC1C,SAAS,2BAAA,GAA8B,GAAC,GAAK,GAAE;AAAA,MAAO,OAAI,eAAO,GAAA,GAAA;EAAA,YAAA,EAAA;EAAA,cAAA,EAAA;EAAA,UAAA,EAAA;EAAA,OAAA,EAAA,cAAA,EAAA,YAAA,KAAA,EAAA,GAAA,KAAA;EAAA,CAAA;;AACjE,SAAO,0BAAmB,GAAA,GAAA,GAAA,GAAA,GAAA;CAAA,IAAA,IAAA,EAAA;AAAA,QAAA,OAAA,KAAA,EAAA,CAAA,QAAA,SAAA,GAAA;AAAA,IAAA,KAAA,EAAA;GAAA,EAAA,EAAA,aAAA,CAAA,CAAA,EAAA,YAAA,EAAA,eAAA,CAAA,CAAA,EAAA,eAAA,WAAA,KAAA,EAAA,iBAAA,EAAA,WAAA,CAAA,IAAA,IAAA,EAAA,OAAA,CAAA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AA,KAAgB,KAAK,KAC3B,QAAI,6BAA0B,KAAA;AAE9B,MAAA,MAAO,KAAA,KAAa,KAAG,KACzB,QAAA,6BAAA,KAAA;AAEA,MAAA,MAAQ,KAAA,KAAA,KAAkB,KACxB,QAAO,6BAAc,KAAA;AAErB,MAAE,MAAQ,KAAK,KAAK,KAAC,KACrB,QAAA,6BAAA,KAAA;AAEA,SAAI;;CAEN,MAAE,qBAAA,SAAA,OAAA;UAEE,QACA;EACF,MAAA,gBAAA,MAAA,KAAA,iBAAA,KAAA,WAAA,OAAA;;EAGA,MAAE,uBAA2B,cAAc,WAAW;EACtD,MAAA,UAAA,OAAA,yBAAA,WAAA,GAAA,qBAAA,GAAA,MAAA;oBAEE,KAAE,aAAe,iBAAY,sCAAA;MAE/B,KAAA,aAAA,iBAAA,mBAAA,UAAA;;GAGD,cAAY,0BAA2B,QAAS,WAAS,eAAiB,CAAC,OAAA,MAAa,EAAA;CACzF,cAAE;;CAEF,UAAM;CACN,aAAa;CACd,CAAC,EAAE,eAAA,0BAAA,QAAA,WAAA,cAAA,CAAA,OAAA,MAAA,EAAA;;CAEF,YAAW;CACX,UAAA;;CAED,CAAC,EAAA,SAAQ,IAAM,SAAA,IAAA;AAChB,SAAa,WAAA,UAAA;AACX,QAAO,cAAE,SAAA,CAAA;;;;ACpOX,IAAS,YAAT,cAA4B,WAAK;;AAEjC,OAAS,IAAA,KAAA,iBAA0B,OAAE,iBAAmB;;;;;;;;;ACJxD,IAAG,MAAO,OAAA;AAiBV,IAAU,yBAAa,OAAA,OAAqB,EAAA,QAAA,SAAA,EAC1C,QAAM,eAAA,CAAA,EAAA,KAAA,SAAA,MAAA,SAAA,MAAA,8BAAA,cAAA,GAAA,IAAA,OAAA,IAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/service/ssrHandler.ts","../src/monkeySys.ts","../src/.metadata/index.ts"],"sourcesContent":["import { catchError } from '@cabloy/utils';\nimport { RouteLocationResolvedGeneric } from '@cabloy/vue-router';\nimport fse from 'fs-extra';\nimport ms from 'ms';\nimport path, { basename } from 'node:path';\nimport { pathToFileURL } from 'node:url';\nimport { BeanBase, cast, Use, UseScope } from 'zova';\nimport { Service } from 'zova-module-a-bean';\nimport { SysRouter } from 'zova-module-a-router';\nimport { ScopeModuleASsr } from 'zova-module-a-ssr';\n\nimport { ISsrHandlerRenderOptionsInner, TypeEventResolvePathResult } from '../types/ssr.js';\n\nconst jsRE = /\\.js$/;\nconst cssRE = /\\.css$/;\nconst woffRE = /\\.woff$/;\nconst woff2RE = /\\.woff2$/;\nconst gifRE = /\\.gif$/;\nconst jpgRE = /\\.jpe?g$/;\nconst pngRE = /\\.png$/;\n\n@Service()\nexport class ServiceSsrHandler extends BeanBase {\n private _siteAssetDir: string;\n private _handlerPromise: Promise<any>;\n private _handlerInstance: any;\n private _clientManifest: any;\n private _ssrModulesZovaCache: any = {};\n\n @Use()\n $$sysRouter: SysRouter;\n\n @UseScope()\n $$scopeSsr: ScopeModuleASsr;\n\n protected __init__(siteAssetDir: string) {\n this._siteAssetDir = siteAssetDir;\n }\n\n protected __dispose__() {\n this.dispose();\n }\n\n public dispose() {\n this.sys.close();\n this._handlerInstance = undefined;\n }\n\n async resolvePath(filename: string): Promise<TypeEventResolvePathResult> {\n if (filename === '') filename = 'index.html';\n // assets\n const fileAsset = path.join(this._siteAssetDir, 'client', filename);\n if (await fse.exists(fileAsset)) return fileAsset;\n // not found\n return undefined;\n }\n\n public async render(options: ISsrHandlerRenderOptionsInner) {\n // resolve route\n const pagePathFull =\n options.state?.pagePathFull ?? this.sys.util.getPagePathFromAbsoluteUrl(options.req.url!);\n const route = await this.$$sysRouter.resolveRoute(pagePathFull, true, false);\n if (!route) return;\n // handler\n const { serverEntry, renderToString, renderTemplate } = this._handlerInstance;\n // ssrContext\n const onRenderedList: Function[] = [];\n const ssrContext = {\n ...options,\n _meta: {} as any,\n onRendered: fn => {\n onRenderedList.push(fn);\n },\n };\n let renderFn;\n let runtimePageContent = '';\n let renderedError;\n let onRenderedFlushed = false;\n let renderedFinalized = false;\n try {\n // render\n renderFn = await serverEntry(ssrContext);\n const [runtimePageContentInner, err] = await catchError(() => {\n return renderToString(renderFn, ssrContext);\n });\n if (runtimePageContentInner) {\n runtimePageContent = runtimePageContentInner as string;\n }\n\n const error = ssrContext._meta.renderError ?? err;\n renderedError = error;\n onRenderedList.forEach(fn => {\n fn(error);\n });\n onRenderedFlushed = true;\n cast(ssrContext).rendered(error);\n renderedFinalized = true;\n if (error) {\n if (error instanceof Error) throw error;\n return error;\n }\n\n ssrContext._meta.runtimePageContent = runtimePageContent;\n\n // @vitejs/plugin-vue injects code into a component's setup() that registers\n // itself on ctx.modules. After the render, ctx.modules would contain all the\n // components that have been instantiated during this render call.\n const modules = cast(ssrContext).modules;\n if (modules) {\n ssrContext._meta.endingHeadTags += this._renderModulesPreload_zova(modules, {\n ssrContext,\n });\n ssrContext._meta.endingHeadTags += this._renderModulesPreload(modules, {\n ssrContext,\n });\n }\n\n const html = renderTemplate(ssrContext);\n\n // transferCache\n await this._renderTransferCache(options, route);\n\n // todo: ssg\n\n return html;\n } finally {\n const context = cast(ssrContext);\n if (!onRenderedFlushed) {\n onRenderedList.forEach(fn => {\n fn(renderedError);\n });\n }\n if (!renderedFinalized && typeof context.rendered === 'function') {\n context.rendered(renderedError);\n }\n onRenderedList.length = 0;\n this._clearSsrContext(context);\n renderFn = undefined;\n }\n }\n\n private _clearSsrContext(context): void {\n context.modules?.clear?.();\n if (context.__qMetaList?.length) {\n context.__qMetaList.length = 0;\n }\n context.state = undefined;\n context.stateDefer = undefined;\n context.performAction = undefined;\n context.rendered = undefined;\n context._meta = undefined;\n }\n\n public async ensureReady(handlerNonce: number) {\n if (!this._handlerInstance) {\n if (!this._handlerPromise) {\n this._handlerPromise = this._prepareHandler(handlerNonce);\n }\n this._handlerInstance = await this._handlerPromise;\n }\n return this._handlerInstance;\n }\n\n private async _prepareHandler(handlerNonce: number) {\n // handler\n const fileHandler = path.join(this._siteAssetDir, 'handler.js');\n const fileUrl = `${pathToHref(fileHandler)}?${handlerNonce}`;\n const handlerInstance = await import(/* @vite-ignore */ fileUrl);\n // clientManifest\n const fileManifest = path.join(this._siteAssetDir, 'quasar.manifest.json');\n const contentManifest = await fse.readFile(fileManifest, { encoding: 'utf-8' });\n this._clientManifest = JSON.parse(contentManifest);\n // ok\n return handlerInstance;\n }\n\n private _renderModulesPreload(modules, opts) {\n let links = '';\n const seen = new Set();\n\n modules.forEach(id => {\n const files = this._clientManifest[id];\n if (files === void 0) return;\n\n files.forEach(file => {\n if (seen.has(file) === true) return;\n\n seen.add(file);\n const filename = basename(file);\n\n if (this._clientManifest[filename] !== void 0) {\n for (const depFile of this._clientManifest[filename]) {\n if (seen.has(depFile) === false) {\n links += this._renderPreloadTag(depFile, opts);\n seen.add(depFile);\n }\n }\n }\n\n links += this._renderPreloadTag(file, opts);\n });\n });\n\n return links;\n }\n\n private _renderModulesPreload_zova(modules2, opts) {\n let links = '';\n const seen = /* @__PURE__ */ new Set();\n modules2.forEach(id => {\n if (!id.startsWith('@@')) return;\n if (seen.has(id) === true) return;\n let cache = this._ssrModulesZovaCache[id];\n if (!cache) {\n for (const key in this._clientManifest) {\n const prefix = `${id.substring(2)}-`;\n const postfix = '.js';\n if (key.startsWith(prefix) && key.endsWith(postfix)) {\n cache = this._resolveUrlPath(`/assets/${key}`);\n break;\n }\n }\n this._ssrModulesZovaCache[id] = cache;\n }\n links += this._renderPreloadTag(cache, opts);\n seen.add(id);\n });\n return links;\n }\n\n private _resolveUrlPath(url: string) {\n let publicPath = this.sys.env.APP_PUBLIC_PATH;\n if (publicPath) publicPath = `/${publicPath}`;\n return publicPath + url;\n }\n\n private _renderPreloadTag(file: string, _opts) {\n file = this.sys.util.getAbsoluteUrlFromPagePath(file, false, true);\n if (jsRE.test(file) === true) {\n return `<link rel=\"modulepreload\" href=\"${file}\" crossorigin>`;\n }\n\n if (cssRE.test(file) === true) {\n return `<link rel=\"stylesheet\" href=\"${file}\" crossorigin>`;\n }\n\n if (woffRE.test(file) === true) {\n return `<link rel=\"preload\" href=\"${file}\" as=\"font\" type=\"font/woff\" crossorigin>`;\n }\n\n if (woff2RE.test(file) === true) {\n return `<link rel=\"preload\" href=\"${file}\" as=\"font\" type=\"font/woff2\" crossorigin>`;\n }\n\n if (gifRE.test(file) === true) {\n return `<link rel=\"preload\" href=\"${file}\" as=\"image\" type=\"image/gif\" crossorigin>`;\n }\n\n if (jpgRE.test(file) === true) {\n return `<link rel=\"preload\" href=\"${file}\" as=\"image\" type=\"image/jpeg\" crossorigin>`;\n }\n\n if (pngRE.test(file) === true) {\n return `<link rel=\"preload\" href=\"${file}\" as=\"image\" type=\"image/png\" crossorigin>`;\n }\n\n return '';\n }\n\n private async _renderTransferCache(\n options: ISsrHandlerRenderOptionsInner,\n route: RouteLocationResolvedGeneric,\n ) {\n const { res } = options;\n const transferCache = route.meta.transferCache ?? this.$$scopeSsr.config.transferCache;\n if (transferCache === false) return;\n // expires\n const transferCacheExpires = transferCache.expires ?? 0;\n const expires =\n typeof transferCacheExpires === 'string'\n ? ms(transferCacheExpires) / 1000\n : transferCacheExpires;\n if (expires === 0) {\n res.appendHeader('cache-control', 'no-cache, no-store, must-revalidate');\n } else {\n res.appendHeader('cache-control', `public, max-age=${expires}`);\n }\n }\n}\n\nexport function pathToHref(fileName: string): string {\n return pathToFileURL(fileName).href;\n // return Path.sep === '\\\\' ? pathToFileURL(fileName).href : fileName;\n}\n","import type { IMonkeySysInitialize } from 'zova';\n\nimport { BeanSimple } from 'zova';\n\nimport { ServiceSsrHandler } from './service/ssrHandler.js';\n\nexport class MonkeySys extends BeanSimple implements IMonkeySysInitialize {\n async sysInitialize() {\n this.sys.meta.$getSsrHandler = async (siteAssetDir: string): Promise<ServiceSsrHandler> => {\n return await this.sys.bean._getBean(ServiceSsrHandler, false, siteAssetDir);\n };\n }\n}\n","// eslint-disable\n/** service: begin */\nexport * from '../service/ssrHandler.js';\n\nimport 'zova-module-a-bean';\ndeclare module 'zova-module-a-bean' {\n \n export interface IServiceRecord {\n 'a-ssrserver:ssrHandler': never;\n }\n\n \n}\ndeclare module 'zova-module-a-ssrserver' {\n \n export interface ServiceSsrHandler {\n /** @internal */\n get scope(): ScopeModuleASsrserver;\n }\n\n export interface ServiceSsrHandler {\n get $beanFullName(): 'a-ssrserver.service.ssrHandler';\n get $onionName(): 'a-ssrserver:ssrHandler';\n \n } \n}\n/** service: end */\n/** service: begin */\nimport { ServiceSsrHandler } from '../service/ssrHandler.js';\nimport 'zova';\ndeclare module 'zova' {\n export interface IBeanRecordGeneral {\n 'a-ssrserver.service.ssrHandler': ServiceSsrHandler;\n }\n}\n/** service: end */\n/** monkeySys: begin */\nexport * from '../monkeySys.js';\n/** monkeySys: end */\n/** scope: begin */\nimport { BeanScopeBase, type BeanScopeUtil } from 'zova';\nimport { Scope } from 'zova-module-a-bean';\n\n@Scope()\nexport class ScopeModuleASsrserver extends BeanScopeBase {}\n\nexport interface ScopeModuleASsrserver {\n util: BeanScopeUtil;\n}\n\nimport 'zova';\ndeclare module 'zova' {\n export interface IBeanScopeRecord {\n 'a-ssrserver': ScopeModuleASsrserver;\n }\n \n \n\n \n\n \n}\n \n/** scope: end */\n"],"mappings":";;;;;;;;AAAA,IAAA,QAAS,SAAA,OAAa,OAAO,OAAO,OAAM,UAAA,SAAA,aAAA;AAC1C,SAAS,2BAAA,GAA8B,GAAC,GAAK,GAAE;CAAA,KAAO,OAAI,eAAO,GAAA,GAAA;EAAA,YAAA,EAAA;EAAA,cAAA,EAAA;EAAA,UAAA,EAAA;EAAA,OAAA,EAAA,cAAA,EAAA,YAAA,KAAA,CAAA,IAAA,KAAA;CAAA,CAAA;AAAA;AACjE,SAAO,0BAAmB,GAAA,GAAA,GAAA,GAAA,GAAA;CAAA,IAAA,IAAA,CAAA;CAAA,OAAA,OAAA,KAAA,CAAA,EAAA,QAAA,SAAA,GAAA;EAAA,EAAA,KAAA,EAAA;CAAA,CAAA,GAAA,E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AAW,OAAM;EACxE,IAAA,kBAAA,OAAA;EAEA,MAAI,uBAAuB,cAAM,WAAA;EACjC,MAAE,UAAc,OAAK,yBAA2B,WAAW,GAAE,oBAAY,IAAa,MAAA;EACtF,IAAA,YAAA,GAAA,IAAA,aAAA,iBAAA,qCAAA;OAGE,IAAA,aAAiB,iBAAiB,mBAAmB,SAAO;;AAGlE,GAAG,cAAW,0BAAqB,QAAA,WAAA,eAAA,CAAA,OAAA,KAAA,GAAA;CACjC,cAAa;CACb,YAAE;;CAEF,aAAY;AACd,CAAC,GAAG,eAAe,0BAAuB,QAAW,WAAW,cAAa,CAAA,OAAA,KAAa,GAAA;CACxF,cAAE;;CAEF,UAAU;CACV,aAAA;;AAEF,SAAgB,WAAA,UAAoB;CAClC,OAAE,cAAS,QAAA,EAAA;AAEb;;;AC9QA,IAAS,YAAT,cAA4B,WAAK;;EAEjC,KAAS,IAAA,KAAA,iBAA0B,OAAE,iBAAmB;;EAExD;CACE;AACF;;;;;ACRA,IAAG,MAAO,OAAA;AAiBV,IAAU,yBAAa,OAAA,MAAqB,GAAA,QAAA,SAAA,EAC1C,QAAM,cAAA,CAAA,GAAA,KAAA,SAAA,MAAA,SAAA,MAAA,8BAAA,cAAA,CAAA,CAAA,KAAA,MAAA,KAAA"}
|
|
@@ -15,6 +15,7 @@ export declare class ServiceSsrHandler extends BeanBase {
|
|
|
15
15
|
dispose(): void;
|
|
16
16
|
resolvePath(filename: string): Promise<TypeEventResolvePathResult>;
|
|
17
17
|
render(options: ISsrHandlerRenderOptionsInner): Promise<any>;
|
|
18
|
+
private _clearSsrContext;
|
|
18
19
|
ensureReady(handlerNonce: number): Promise<any>;
|
|
19
20
|
private _prepareHandler;
|
|
20
21
|
private _renderModulesPreload;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ssrHandler.d.ts","sourceRoot":"","sources":["../../src/service/ssrHandler.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,QAAQ,EAAuB,MAAM,MA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C,YAAY,EAAE,MAAM;IAIvC,SAAS,CAAC,WAAW;IAId,OAAO;IAKR,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,0BAA0B,CAAC;IAS3D,MAAM,CAAC,OAAO,EAAE,6BAA6B;IAoF1D,OAAO,CAAC,gBAAgB;IAYX,WAAW,CAAC,YAAY,EAAE,MAAM;YAU/B,eAAe;IAa7B,OAAO,CAAC,qBAAqB;IA8B7B,OAAO,CAAC,0BAA0B;IAwBlC,OAAO,CAAC,eAAe;IAMvB,OAAO,CAAC,iBAAiB;YAiCX,oBAAoB;CAmBnC;AAED,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAGnD"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zova-module-a-ssrserver",
|
|
3
|
-
"version": "5.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "5.1.19",
|
|
4
|
+
"gitHead": "3380f8c5d3989242b60dc2a34cbb330f74335cbe",
|
|
5
5
|
"description": "",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"Zova Module"
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"clean": "rimraf dist tsconfig.build.tsbuildinfo",
|
|
27
|
-
"tsc:publish": "npm run clean && zova :bin:buildModule --sourcemap && tsc -p tsconfig.build.json",
|
|
27
|
+
"tsc:publish": "npm run clean && node ../../../../../packages-cli/cli/src/bin/zova.ts :bin:buildModule --sourcemap && tsc -p tsconfig.build.json",
|
|
28
28
|
"prepublishOnly": "npm run tsc:publish",
|
|
29
29
|
"prepack": "clean-package",
|
|
30
30
|
"postpack": "clean-package restore && npm run clean"
|
|
@@ -72,42 +72,83 @@ export class ServiceSsrHandler extends BeanBase {
|
|
|
72
72
|
onRenderedList.push(fn);
|
|
73
73
|
},
|
|
74
74
|
};
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
let renderFn;
|
|
76
|
+
let runtimePageContent = '';
|
|
77
|
+
let renderedError;
|
|
78
|
+
let onRenderedFlushed = false;
|
|
79
|
+
let renderedFinalized = false;
|
|
80
|
+
try {
|
|
81
|
+
// render
|
|
82
|
+
renderFn = await serverEntry(ssrContext);
|
|
83
|
+
const [runtimePageContentInner, err] = await catchError(() => {
|
|
84
|
+
return renderToString(renderFn, ssrContext);
|
|
85
|
+
});
|
|
86
|
+
if (runtimePageContentInner) {
|
|
87
|
+
runtimePageContent = runtimePageContentInner as string;
|
|
88
|
+
}
|
|
80
89
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
fn
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
+
const error = ssrContext._meta.renderError ?? err;
|
|
91
|
+
renderedError = error;
|
|
92
|
+
onRenderedList.forEach(fn => {
|
|
93
|
+
fn(error);
|
|
94
|
+
});
|
|
95
|
+
onRenderedFlushed = true;
|
|
96
|
+
cast(ssrContext).rendered(error);
|
|
97
|
+
renderedFinalized = true;
|
|
98
|
+
if (error) {
|
|
99
|
+
if (error instanceof Error) throw error;
|
|
100
|
+
return error;
|
|
101
|
+
}
|
|
90
102
|
|
|
91
|
-
|
|
103
|
+
ssrContext._meta.runtimePageContent = runtimePageContent;
|
|
104
|
+
|
|
105
|
+
// @vitejs/plugin-vue injects code into a component's setup() that registers
|
|
106
|
+
// itself on ctx.modules. After the render, ctx.modules would contain all the
|
|
107
|
+
// components that have been instantiated during this render call.
|
|
108
|
+
const modules = cast(ssrContext).modules;
|
|
109
|
+
if (modules) {
|
|
110
|
+
ssrContext._meta.endingHeadTags += this._renderModulesPreload_zova(modules, {
|
|
111
|
+
ssrContext,
|
|
112
|
+
});
|
|
113
|
+
ssrContext._meta.endingHeadTags += this._renderModulesPreload(modules, {
|
|
114
|
+
ssrContext,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
92
117
|
|
|
93
|
-
|
|
94
|
-
// itself on ctx.modules. After the render, ctx.modules would contain all the
|
|
95
|
-
// components that have been instantiated during this render call.
|
|
96
|
-
ssrContext._meta.endingHeadTags += this._renderModulesPreload_zova(cast(ssrContext).modules, {
|
|
97
|
-
ssrContext,
|
|
98
|
-
});
|
|
99
|
-
ssrContext._meta.endingHeadTags += this._renderModulesPreload(cast(ssrContext).modules, {
|
|
100
|
-
ssrContext,
|
|
101
|
-
});
|
|
118
|
+
const html = renderTemplate(ssrContext);
|
|
102
119
|
|
|
103
|
-
|
|
120
|
+
// transferCache
|
|
121
|
+
await this._renderTransferCache(options, route);
|
|
104
122
|
|
|
105
|
-
|
|
106
|
-
await this._renderTransferCache(options, route);
|
|
123
|
+
// todo: ssg
|
|
107
124
|
|
|
108
|
-
|
|
125
|
+
return html;
|
|
126
|
+
} finally {
|
|
127
|
+
const context = cast(ssrContext);
|
|
128
|
+
if (!onRenderedFlushed) {
|
|
129
|
+
onRenderedList.forEach(fn => {
|
|
130
|
+
fn(renderedError);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (!renderedFinalized && typeof context.rendered === 'function') {
|
|
134
|
+
context.rendered(renderedError);
|
|
135
|
+
}
|
|
136
|
+
onRenderedList.length = 0;
|
|
137
|
+
this._clearSsrContext(context);
|
|
138
|
+
renderFn = undefined;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
109
141
|
|
|
110
|
-
|
|
142
|
+
private _clearSsrContext(context): void {
|
|
143
|
+
context.modules?.clear?.();
|
|
144
|
+
if (context.__qMetaList?.length) {
|
|
145
|
+
context.__qMetaList.length = 0;
|
|
146
|
+
}
|
|
147
|
+
context.state = undefined;
|
|
148
|
+
context.stateDefer = undefined;
|
|
149
|
+
context.performAction = undefined;
|
|
150
|
+
context.rendered = undefined;
|
|
151
|
+
context._meta = undefined;
|
|
111
152
|
}
|
|
112
153
|
|
|
113
154
|
public async ensureReady(handlerNonce: number) {
|