@umijs/renderer-react 4.0.0-canary.20240305.1 → 4.0.0-canary.20240402.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/dist/browser.d.ts CHANGED
@@ -18,11 +18,21 @@ export declare type RenderClientOpts = {
18
18
  * @doc https://umijs.org/docs/api/config#runtimepublicpath
19
19
  */
20
20
  runtimePublicPath?: boolean;
21
+ /**
22
+ * react dom 渲染的的目标节点 id
23
+ * @doc 一般不需要改,微前端的时候会变化
24
+ */
25
+ mountElementId?: string;
21
26
  /**
22
27
  * react dom 渲染的的目标 dom
23
28
  * @doc 一般不需要改,微前端的时候会变化
24
29
  */
25
30
  rootElement?: HTMLElement;
31
+ /**
32
+ * ssr 是否从 app root 根节点开始 render
33
+ * @doc 默认 false, 从 app root 开始 render,为 true 时从 html 开始
34
+ */
35
+ renderFromRoot?: boolean;
26
36
  /**
27
37
  * 当前的路由配置
28
38
  */
package/dist/browser.js CHANGED
@@ -7,6 +7,7 @@ import ReactDOM from 'react-dom/client';
7
7
  import { matchRoutes, Router, useRoutes } from 'react-router-dom';
8
8
  import { AppContext, useAppData } from "./appContext";
9
9
  import { fetchServerLoader } from "./dataFetcher";
10
+ import { Html } from "./html";
10
11
  import { createClientRoutes } from "./routes";
11
12
  var root = null;
12
13
 
@@ -247,7 +248,15 @@ export function renderClient(opts) {
247
248
  // 为了测试,直接返回组件
248
249
  if (opts.components) return Browser;
249
250
  if (opts.hydrate) {
250
- ReactDOM.hydrateRoot(rootElement, /*#__PURE__*/React.createElement(Browser, null));
251
+ // @ts-ignore
252
+ var loaderData = window.__UMI_LOADER_DATA__ || {};
253
+ // @ts-ignore
254
+ var metadata = window.__UMI_METADATA_LOADER_DATA__ || {};
255
+ ReactDOM.hydrateRoot(opts.renderFromRoot ? rootElement : document, /*#__PURE__*/React.createElement(Html, {
256
+ metadata: metadata,
257
+ loaderData: loaderData,
258
+ mountElementId: opts.mountElementId
259
+ }, /*#__PURE__*/React.createElement(Browser, null)));
251
260
  return;
252
261
  }
253
262
  if (ReactDOM.createRoot) {
@@ -7,7 +7,8 @@ export function fetchServerLoader(_ref) {
7
7
  url: window.location.href
8
8
  }).toString();
9
9
  // 在有basename的情况下__serverLoader的请求路径需要加上basename
10
- var url = "".concat(withEndSlash(basename), "__serverLoader?").concat(query);
10
+ // FIXME: 先临时解自定义 serverLoader 请求路径的问题,后续改造 serverLoader 时再提取成类似 runtimeServerLoader 的配置项
11
+ var url = "".concat(withEndSlash(window.umiServerLoaderPath || basename), "__serverLoader?").concat(query);
11
12
  fetch(url, {
12
13
  credentials: 'include'
13
14
  }).then(function (d) {
package/dist/html.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ import { IHtmlProps } from './types';
3
+ export declare function Html({ children, loaderData, manifest, tplOpts, renderFromRoot, mountElementId, }: React.PropsWithChildren<IHtmlProps>): JSX.Element;
package/dist/html.js ADDED
@@ -0,0 +1,139 @@
1
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
2
+ import _extends from "@babel/runtime/helpers/esm/extends";
3
+ import _typeof from "@babel/runtime/helpers/esm/typeof";
4
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
+ var _excluded = ["content"],
6
+ _excluded2 = ["content"];
7
+ import React from 'react';
8
+ var RE_URL = /^(http:|https:)?\/\//;
9
+ function isUrl(str) {
10
+ return RE_URL.test(str) || str.startsWith('/') && !str.startsWith('/*') || str.startsWith('./') || str.startsWith('../');
11
+ }
12
+ function normalizeScripts(script) {
13
+ var extraProps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
14
+ if (typeof script === 'string') {
15
+ return isUrl(script) ? _objectSpread({
16
+ src: script
17
+ }, extraProps) : {
18
+ content: script
19
+ };
20
+ } else if (_typeof(script) === 'object') {
21
+ return _objectSpread(_objectSpread({}, script), extraProps);
22
+ } else {
23
+ throw new Error("Invalid script type: ".concat(_typeof(script)));
24
+ }
25
+ }
26
+ function generatorStyle(style) {
27
+ return isUrl(style) ? {
28
+ type: 'link',
29
+ href: style
30
+ } : {
31
+ type: 'style',
32
+ content: style
33
+ };
34
+ }
35
+ var HydrateMetadata = function HydrateMetadata(props) {
36
+ var _tplOpts$favicons, _tplOpts$keywords, _tplOpts$metas, _tplOpts$links, _tplOpts$styles, _tplOpts$headScripts;
37
+ var tplOpts = props.tplOpts;
38
+ return /*#__PURE__*/React.createElement(React.Fragment, null, (tplOpts === null || tplOpts === void 0 ? void 0 : tplOpts.title) && /*#__PURE__*/React.createElement("title", null, tplOpts.title), tplOpts === null || tplOpts === void 0 || (_tplOpts$favicons = tplOpts.favicons) === null || _tplOpts$favicons === void 0 ? void 0 : _tplOpts$favicons.map(function (favicon, key) {
39
+ return /*#__PURE__*/React.createElement("link", {
40
+ key: key,
41
+ rel: "shortcut icon",
42
+ href: favicon
43
+ });
44
+ }), (tplOpts === null || tplOpts === void 0 ? void 0 : tplOpts.description) && /*#__PURE__*/React.createElement("meta", {
45
+ name: "description",
46
+ content: tplOpts.description
47
+ }), (tplOpts === null || tplOpts === void 0 || (_tplOpts$keywords = tplOpts.keywords) === null || _tplOpts$keywords === void 0 ? void 0 : _tplOpts$keywords.length) && /*#__PURE__*/React.createElement("meta", {
48
+ name: "keywords",
49
+ content: tplOpts.keywords.join(',')
50
+ }), tplOpts === null || tplOpts === void 0 || (_tplOpts$metas = tplOpts.metas) === null || _tplOpts$metas === void 0 ? void 0 : _tplOpts$metas.map(function (em) {
51
+ return /*#__PURE__*/React.createElement("meta", {
52
+ key: em.name,
53
+ name: em.name,
54
+ content: em.content
55
+ });
56
+ }), tplOpts === null || tplOpts === void 0 || (_tplOpts$links = tplOpts.links) === null || _tplOpts$links === void 0 ? void 0 : _tplOpts$links.map(function (link, key) {
57
+ return /*#__PURE__*/React.createElement("link", _extends({
58
+ key: key
59
+ }, link));
60
+ }), tplOpts === null || tplOpts === void 0 || (_tplOpts$styles = tplOpts.styles) === null || _tplOpts$styles === void 0 ? void 0 : _tplOpts$styles.map(function (style, key) {
61
+ var _generatorStyle = generatorStyle(style),
62
+ type = _generatorStyle.type,
63
+ href = _generatorStyle.href,
64
+ content = _generatorStyle.content;
65
+ if (type === 'link') {
66
+ return /*#__PURE__*/React.createElement("link", {
67
+ key: key,
68
+ rel: "stylesheet",
69
+ href: href
70
+ });
71
+ } else if (type === 'style') {
72
+ return /*#__PURE__*/React.createElement("style", {
73
+ key: key
74
+ }, content);
75
+ }
76
+ }), tplOpts === null || tplOpts === void 0 || (_tplOpts$headScripts = tplOpts.headScripts) === null || _tplOpts$headScripts === void 0 ? void 0 : _tplOpts$headScripts.map(function (script, key) {
77
+ var _normalizeScripts = normalizeScripts(script),
78
+ content = _normalizeScripts.content,
79
+ rest = _objectWithoutProperties(_normalizeScripts, _excluded);
80
+ return /*#__PURE__*/React.createElement("script", _extends({
81
+ key: key
82
+ }, rest), content);
83
+ }));
84
+ };
85
+ export function Html(_ref) {
86
+ var _tplOpts$scripts;
87
+ var children = _ref.children,
88
+ loaderData = _ref.loaderData,
89
+ manifest = _ref.manifest,
90
+ tplOpts = _ref.tplOpts,
91
+ renderFromRoot = _ref.renderFromRoot,
92
+ mountElementId = _ref.mountElementId;
93
+ // TODO: 处理 head 标签,比如 favicon.ico 的一致性
94
+ // TODO: root 支持配置
95
+ if (renderFromRoot) {
96
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(HydrateMetadata, {
97
+ tplOpts: tplOpts
98
+ }), /*#__PURE__*/React.createElement("div", {
99
+ id: mountElementId
100
+ }, children));
101
+ }
102
+ // @ts-ignore
103
+ var serverBuildManifest = typeof window === 'undefined' ? manifest :
104
+ // @ts-ignore
105
+ window.__UMI_BUILD_MANIFEST_DATA__;
106
+ return /*#__PURE__*/React.createElement("html", {
107
+ suppressHydrationWarning: true,
108
+ lang: (tplOpts === null || tplOpts === void 0 ? void 0 : tplOpts.lang) || 'en'
109
+ }, /*#__PURE__*/React.createElement("head", null, /*#__PURE__*/React.createElement("meta", {
110
+ charSet: "utf-8"
111
+ }), /*#__PURE__*/React.createElement("meta", {
112
+ name: "viewport",
113
+ content: "width=device-width, initial-scale=1"
114
+ }), (serverBuildManifest === null || serverBuildManifest === void 0 ? void 0 : serverBuildManifest.assets['umi.css']) && /*#__PURE__*/React.createElement("link", {
115
+ suppressHydrationWarning: true,
116
+ rel: "stylesheet",
117
+ href: manifest === null || manifest === void 0 ? void 0 : manifest.assets['umi.css']
118
+ }), /*#__PURE__*/React.createElement(HydrateMetadata, {
119
+ tplOpts: tplOpts
120
+ })), /*#__PURE__*/React.createElement("body", null, /*#__PURE__*/React.createElement("noscript", {
121
+ dangerouslySetInnerHTML: {
122
+ __html: "<b>Enable JavaScript to run this app.</b>"
123
+ }
124
+ }), /*#__PURE__*/React.createElement("div", {
125
+ id: mountElementId
126
+ }, children), /*#__PURE__*/React.createElement("script", {
127
+ suppressHydrationWarning: true,
128
+ dangerouslySetInnerHTML: {
129
+ __html: "window.__UMI_LOADER_DATA__ = ".concat(JSON.stringify(loaderData || {}), "; window.__UMI_METADATA_LOADER_DATA__ = ").concat(JSON.stringify(tplOpts || {}), "; window.__UMI_BUILD_MANIFEST_DATA__ = ").concat(JSON.stringify(manifest) || {})
130
+ }
131
+ }), tplOpts === null || tplOpts === void 0 || (_tplOpts$scripts = tplOpts.scripts) === null || _tplOpts$scripts === void 0 ? void 0 : _tplOpts$scripts.map(function (script, key) {
132
+ var _normalizeScripts2 = normalizeScripts(script),
133
+ content = _normalizeScripts2.content,
134
+ rest = _objectWithoutProperties(_normalizeScripts2, _excluded2);
135
+ return /*#__PURE__*/React.createElement("script", _extends({
136
+ key: key
137
+ }, rest), content);
138
+ })));
139
+ }
package/dist/server.d.ts CHANGED
@@ -1,15 +1,2 @@
1
- import type { IMetadata } from '@umijs/server/dist/types';
2
- import { IRouteComponents, IRoutesById } from './types';
3
- interface IHtmlProps {
4
- routes: IRoutesById;
5
- routeComponents: IRouteComponents;
6
- pluginManager: any;
7
- location: string;
8
- loaderData: {
9
- [routeKey: string]: any;
10
- };
11
- manifest: any;
12
- metadata?: IMetadata;
13
- }
14
- export declare function getClientRootComponent(opts: IHtmlProps): Promise<JSX.Element>;
15
- export {};
1
+ import { IRootComponentOptions } from './types';
2
+ export declare function getClientRootComponent(opts: IRootComponentOptions): Promise<JSX.Element>;
package/dist/server.js CHANGED
@@ -5,6 +5,7 @@ import React from 'react';
5
5
  import { StaticRouter } from 'react-router-dom/server';
6
6
  import { AppContext } from "./appContext";
7
7
  import { Routes } from "./browser";
8
+ import { Html } from "./html";
8
9
  import { createClientRoutes } from "./routes";
9
10
  // Get the root React component for ReactDOMServer.renderToString
10
11
  export function getClientRootComponent(_x) {
@@ -56,47 +57,4 @@ function _getClientRootComponent() {
56
57
  }, _callee);
57
58
  }));
58
59
  return _getClientRootComponent.apply(this, arguments);
59
- }
60
- function Html(_ref) {
61
- var _metadata$keywords, _metadata$metas;
62
- var children = _ref.children,
63
- loaderData = _ref.loaderData,
64
- manifest = _ref.manifest,
65
- metadata = _ref.metadata;
66
- // TODO: 处理 head 标签,比如 favicon.ico 的一致性
67
- // TODO: root 支持配置
68
-
69
- return /*#__PURE__*/React.createElement("html", {
70
- lang: (metadata === null || metadata === void 0 ? void 0 : metadata.lang) || 'en'
71
- }, /*#__PURE__*/React.createElement("head", null, /*#__PURE__*/React.createElement("meta", {
72
- charSet: "utf-8"
73
- }), /*#__PURE__*/React.createElement("meta", {
74
- name: "viewport",
75
- content: "width=device-width, initial-scale=1"
76
- }), (metadata === null || metadata === void 0 ? void 0 : metadata.title) && /*#__PURE__*/React.createElement("title", null, metadata.title), (metadata === null || metadata === void 0 ? void 0 : metadata.description) && /*#__PURE__*/React.createElement("meta", {
77
- name: "description",
78
- content: metadata.description
79
- }), (metadata === null || metadata === void 0 || (_metadata$keywords = metadata.keywords) === null || _metadata$keywords === void 0 ? void 0 : _metadata$keywords.length) && /*#__PURE__*/React.createElement("meta", {
80
- name: "keywords",
81
- content: metadata.keywords.join(',')
82
- }), metadata === null || metadata === void 0 || (_metadata$metas = metadata.metas) === null || _metadata$metas === void 0 ? void 0 : _metadata$metas.map(function (em) {
83
- return /*#__PURE__*/React.createElement("meta", {
84
- key: em.name,
85
- name: em.name,
86
- content: em.content
87
- });
88
- }), manifest.assets['umi.css'] && /*#__PURE__*/React.createElement("link", {
89
- rel: "stylesheet",
90
- href: manifest.assets['umi.css']
91
- })), /*#__PURE__*/React.createElement("body", null, /*#__PURE__*/React.createElement("noscript", {
92
- dangerouslySetInnerHTML: {
93
- __html: "<b>Enable JavaScript to run this app.</b>"
94
- }
95
- }), /*#__PURE__*/React.createElement("div", {
96
- id: "root"
97
- }, children), /*#__PURE__*/React.createElement("script", {
98
- dangerouslySetInnerHTML: {
99
- __html: "window.__UMI_LOADER_DATA__ = ".concat(JSON.stringify(loaderData))
100
- }
101
- })));
102
60
  }
package/dist/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /// <reference types="react" />
2
+ import type { ITplOpts } from '@umijs/server/dist/types';
2
3
  import type { RouteMatch, RouteObject } from 'react-router-dom';
3
4
  export interface IRouteSSRProps {
4
5
  clientLoader?: () => Promise<any>;
@@ -33,3 +34,35 @@ export interface IRouteComponents {
33
34
  export interface ILoaderData {
34
35
  [routeKey: string]: any;
35
36
  }
37
+ export interface IRootComponentOptions {
38
+ routes: IRoutesById;
39
+ routeComponents: IRouteComponents;
40
+ pluginManager: any;
41
+ location: string;
42
+ loaderData: {
43
+ [routeKey: string]: any;
44
+ };
45
+ manifest: any;
46
+ tplOpts?: ITplOpts;
47
+ renderFromRoot: boolean;
48
+ mountElementId: string;
49
+ }
50
+ export interface IHtmlProps {
51
+ children?: React.ReactNode;
52
+ loaderData?: {
53
+ [routeKey: string]: any;
54
+ };
55
+ manifest?: any;
56
+ tplOpts?: ITplOpts;
57
+ mountElementId?: string;
58
+ renderFromRoot?: boolean;
59
+ }
60
+ export declare type IScript = Partial<{
61
+ async: boolean;
62
+ charset: string;
63
+ content: string;
64
+ crossOrigin: string | null;
65
+ defer: boolean;
66
+ src: string;
67
+ type: string;
68
+ }> | string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/renderer-react",
3
- "version": "4.0.0-canary.20240305.1",
3
+ "version": "4.0.0-canary.20240402.1",
4
4
  "description": "@umijs/renderer-react",
5
5
  "homepage": "https://github.com/umijs/umi/tree/master/packages/renderer-react#readme",
6
6
  "bugs": "https://github.com/umijs/umi/issues",