@lynx-js/web-core-server 0.13.3 → 0.13.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @lynx-js/web-core-server
2
2
 
3
+ ## 0.13.4
4
+
5
+ ### Patch Changes
6
+
7
+ - feat: support to dump ssrID ([#919](https://github.com/lynx-family/lynx-stack/pull/919))
8
+
3
9
  ## 0.13.3
4
10
 
5
11
  ### Patch Changes
@@ -3,7 +3,7 @@ interface LynxViewConfig extends Pick<StartMainThreadContextConfig, 'browserConf
3
3
  templateName?: string;
4
4
  hydrateUrl: string;
5
5
  injectStyles: string;
6
- overrideElemenTemplates?: Record<string, ((attributes: Record<string, string>) => string) | string>;
6
+ overrideElementTemplates?: Record<string, ((attributes: Record<string, string>) => string) | string>;
7
7
  autoSize?: boolean;
8
8
  lynxViewStyle?: string;
9
9
  }
@@ -1,9 +1,10 @@
1
- import { inShadowRootStyles, } from '@lynx-js/web-constants';
1
+ import { inShadowRootStyles, lynxUniqueIdAttribute, } from '@lynx-js/web-constants';
2
2
  import { Rpc } from '@lynx-js/web-worker-rpc';
3
3
  import { prepareMainThreadAPIs } from '@lynx-js/web-mainthread-apis';
4
4
  import { loadTemplate } from './utils/loadTemplate.js';
5
- import { dumpHTMLString, OffscreenDocument, } from '@lynx-js/offscreen-document/webworker';
5
+ import { _attributes, OffscreenDocument, OffscreenElement, } from '@lynx-js/offscreen-document/webworker';
6
6
  import { templateScrollView, templateXAudioTT, templateXImage, templateFilterImage, templateXInput, templateXList, templateXOverlayNg, templateXRefreshView, templateXSwiper, templateXText, templateInlineImage, templateXTextarea, templateXViewpageNg, } from '@lynx-js/web-elements-template';
7
+ import { dumpHTMLString } from './dumpHTMLString.js';
7
8
  const builtinElementTemplates = {
8
9
  'scroll-view': templateScrollView,
9
10
  'x-audio-tt': templateXAudioTT,
@@ -27,8 +28,14 @@ const builtinTagTransformMap = {
27
28
  'list': 'x-list',
28
29
  'svg': 'x-svg',
29
30
  };
31
+ // @ts-expect-error
32
+ OffscreenElement.prototype.toJSON = function toJSON() {
33
+ return {
34
+ ssrID: this[_attributes].get(lynxUniqueIdAttribute),
35
+ };
36
+ };
30
37
  export async function createLynxView(config) {
31
- const { template: rawTemplate, browserConfig, tagMap, initData, globalProps, overrideElemenTemplates = {}, hydrateUrl, autoSize, injectStyles, lynxViewStyle, } = config;
38
+ const { template: rawTemplate, browserConfig, tagMap, initData, globalProps, overrideElementTemplates = {}, hydrateUrl, autoSize, injectStyles, lynxViewStyle, } = config;
32
39
  const template = await loadTemplate(rawTemplate, config.templateName);
33
40
  const { promise: firstPaintReadyPromise, resolve: firstPaintReady } = Promise
34
41
  .withResolvers();
@@ -59,20 +66,27 @@ export async function createLynxView(config) {
59
66
  });
60
67
  const elementTemplates = {
61
68
  ...builtinElementTemplates,
62
- ...overrideElemenTemplates,
69
+ ...overrideElementTemplates,
63
70
  };
64
71
  async function renderToString() {
65
72
  await firstPaintReadyPromise;
66
- const innerShadowRootHTML = dumpHTMLString(offscreenDocument, elementTemplates);
67
73
  const ssrEncodeData = runtime?.ssrEncode?.();
68
- const encodeDataEncoded = ssrEncodeData ? encodeURI(ssrEncodeData) : ''; // to avoid XSS
69
- return `
70
- <lynx-view url="${hydrateUrl}" ssr ${autoSize ? 'height="auto" width="auto"' : ''} ${lynxViewStyle ? `style="${lynxViewStyle}"` : ''} ${encodeDataEncoded ? `ssr-encode-data="${encodeDataEncoded}"` : ''}>
71
- <template shadowrootmode="open">
72
- <style>${injectStyles}\n${inShadowRootStyles.join('\n')}</style>
73
- ${innerShadowRootHTML}
74
- </template>
75
- </lynx-view>`;
74
+ const buffer = [];
75
+ buffer.push('<lynx-view url="', hydrateUrl, '" ssr');
76
+ if (autoSize) {
77
+ buffer.push(' height="auto" width="auto"');
78
+ }
79
+ if (lynxViewStyle) {
80
+ buffer.push(' style="', lynxViewStyle, '"');
81
+ }
82
+ if (ssrEncodeData) {
83
+ const encodeDataEncoded = ssrEncodeData ? encodeURI(ssrEncodeData) : ''; // to avoid XSS
84
+ buffer.push(' ssr-encode-data="', encodeDataEncoded, '"');
85
+ }
86
+ buffer.push('><template shadowrootmode="open">', '<style>', injectStyles, '\n', inShadowRootStyles.join('\n'), '</style>');
87
+ dumpHTMLString(buffer, offscreenDocument, elementTemplates);
88
+ buffer.push('</template>', '</lynx-view>');
89
+ return buffer.join('');
76
90
  }
77
91
  return {
78
92
  renderToString,
@@ -0,0 +1,4 @@
1
+ import { type OffscreenDocument } from '@lynx-js/offscreen-document/webworker';
2
+ type ShadowrootTemplates = ((attributes: Record<string, string>) => string) | string;
3
+ export declare function dumpHTMLString(buffer: string[], element: OffscreenDocument, shadowrootTemplates: Record<string, ShadowrootTemplates>): void;
4
+ export {};
@@ -0,0 +1,38 @@
1
+ import { _attributes, _children, innerHTML, } from '@lynx-js/offscreen-document/webworker';
2
+ function getInnerHTMLImpl(buffer, element, shadowrootTemplates) {
3
+ const localName = element.localName;
4
+ buffer.push('<');
5
+ buffer.push(localName);
6
+ for (const [key, value] of element[_attributes]) {
7
+ buffer.push(' ');
8
+ buffer.push(key);
9
+ buffer.push('="');
10
+ buffer.push(value);
11
+ buffer.push('"');
12
+ }
13
+ buffer.push('>');
14
+ const templateImpl = shadowrootTemplates[localName];
15
+ if (templateImpl) {
16
+ const template = typeof templateImpl === 'function'
17
+ ? templateImpl(Object.fromEntries(element[_attributes].entries()))
18
+ : templateImpl;
19
+ buffer.push('<template shadowrootmode="open">', template, '</template>');
20
+ }
21
+ if (element[innerHTML]) {
22
+ buffer.push(element[innerHTML]);
23
+ }
24
+ else {
25
+ for (const child of element[_children]) {
26
+ getInnerHTMLImpl(buffer, child, shadowrootTemplates);
27
+ }
28
+ }
29
+ buffer.push('</');
30
+ buffer.push(localName);
31
+ buffer.push('>');
32
+ }
33
+ export function dumpHTMLString(buffer, element, shadowrootTemplates) {
34
+ for (const child of element[_children]) {
35
+ getInnerHTMLImpl(buffer, child, shadowrootTemplates);
36
+ }
37
+ }
38
+ //# sourceMappingURL=dumpHTMLString.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lynx-js/web-core-server",
3
- "version": "0.13.3",
3
+ "version": "0.13.4",
4
4
  "private": false,
5
5
  "description": "",
6
6
  "keywords": [],
@@ -22,10 +22,10 @@
22
22
  "README.md"
23
23
  ],
24
24
  "devDependencies": {
25
- "@lynx-js/offscreen-document": "0.0.4",
26
- "@lynx-js/web-constants": "0.13.3",
27
- "@lynx-js/web-elements-template": "0.7.3",
28
- "@lynx-js/web-mainthread-apis": "0.13.3",
29
- "@lynx-js/web-worker-rpc": "0.13.3"
25
+ "@lynx-js/offscreen-document": "0.1.0",
26
+ "@lynx-js/web-constants": "0.13.4",
27
+ "@lynx-js/web-elements-template": "0.7.4",
28
+ "@lynx-js/web-mainthread-apis": "0.13.4",
29
+ "@lynx-js/web-worker-rpc": "0.13.4"
30
30
  }
31
31
  }