@umijs/renderer-react 4.0.0-canary.20240509.4 → 4.0.0-canary.20240511.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,21 +18,11 @@ 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;
26
21
  /**
27
22
  * react dom 渲染的的目标 dom
28
23
  * @doc 一般不需要改,微前端的时候会变化
29
24
  */
30
25
  rootElement?: HTMLElement;
31
- /**
32
- * ssr 是否从 app root 根节点开始 render
33
- * @doc 默认 false, 从 app root 开始 render,为 true 时从 html 开始
34
- */
35
- renderFromRoot?: boolean;
36
26
  /**
37
27
  * 当前的路由配置
38
28
  */
package/dist/browser.js CHANGED
@@ -7,7 +7,6 @@ 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";
11
10
  import { createClientRoutes } from "./routes";
12
11
  var root = null;
13
12
 
@@ -248,16 +247,7 @@ export function renderClient(opts) {
248
247
  // 为了测试,直接返回组件
249
248
  if (opts.components) return Browser;
250
249
  if (opts.hydrate) {
251
- // @ts-ignore
252
- var loaderData = window.__UMI_LOADER_DATA__ || {};
253
- // @ts-ignore
254
- var metadata = window.__UMI_METADATA_LOADER_DATA__ || {};
255
- var hydtateHtmloptions = {
256
- metadata: metadata,
257
- loaderData: loaderData,
258
- mountElementId: opts.mountElementId
259
- };
260
- ReactDOM.hydrateRoot(opts.renderFromRoot ? rootElement : document, /*#__PURE__*/React.createElement(Html, hydtateHtmloptions, /*#__PURE__*/React.createElement(Browser, null)));
250
+ ReactDOM.hydrateRoot(rootElement, /*#__PURE__*/React.createElement(Browser, null));
261
251
  return;
262
252
  }
263
253
  if (ReactDOM.createRoot) {
package/dist/routes.js CHANGED
@@ -4,9 +4,9 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
4
4
  var _excluded = ["redirect"];
5
5
  // @ts-ignore
6
6
  import React from 'react';
7
- import { generatePath, Navigate, useParams, Outlet } from 'react-router-dom';
7
+ import { generatePath, Navigate, Outlet, useLocation, useParams } from 'react-router-dom';
8
+ import { useAppData, useRouteProps } from "./appContext";
8
9
  import { RouteContext, useRouteData } from "./routeContext";
9
- import { useAppData } from "./appContext";
10
10
  export function createClientRoutes(opts) {
11
11
  var routesById = opts.routesById,
12
12
  parentId = opts.parentId,
@@ -43,8 +43,15 @@ export function createClientRoutes(opts) {
43
43
  }
44
44
  function NavigateWithParams(props) {
45
45
  var params = useParams();
46
+ var to = generatePath(props.to, params);
47
+ var routeProps = useRouteProps();
48
+ var location = useLocation();
49
+ if (routeProps !== null && routeProps !== void 0 && routeProps.keepQuery) {
50
+ var queryAndHash = location.search + location.hash;
51
+ to += queryAndHash;
52
+ }
46
53
  var propsWithParams = _objectSpread(_objectSpread({}, props), {}, {
47
- to: generatePath(props.to, params)
54
+ to: to
48
55
  });
49
56
  return /*#__PURE__*/React.createElement(Navigate, _extends({
50
57
  replace: true
package/dist/server.d.ts CHANGED
@@ -1,2 +1,15 @@
1
- import { IRootComponentOptions } from './types';
2
- export declare function getClientRootComponent(opts: IRootComponentOptions): Promise<JSX.Element>;
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 {};
package/dist/server.js CHANGED
@@ -5,7 +5,6 @@ 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";
9
8
  import { createClientRoutes } from "./routes";
10
9
  // Get the root React component for ReactDOMServer.renderToString
11
10
  export function getClientRootComponent(_x) {
@@ -18,18 +17,11 @@ function _getClientRootComponent() {
18
17
  while (1) switch (_context.prev = _context.next) {
19
18
  case 0:
20
19
  basename = '/';
21
- components = _objectSpread({}, opts.routeComponents); // todo 参数对齐
20
+ components = _objectSpread({}, opts.routeComponents);
22
21
  clientRoutes = createClientRoutes({
23
22
  routesById: opts.routes,
24
23
  routeComponents: components
25
24
  });
26
- opts.pluginManager.applyPlugins({
27
- key: 'patchClientRoutes',
28
- type: 'event',
29
- args: {
30
- routes: clientRoutes
31
- }
32
- });
33
25
  rootContainer = /*#__PURE__*/React.createElement(StaticRouter, {
34
26
  basename: basename,
35
27
  location: opts.location
@@ -57,11 +49,54 @@ function _getClientRootComponent() {
57
49
  }
58
50
  }, rootContainer);
59
51
  return _context.abrupt("return", /*#__PURE__*/React.createElement(Html, opts, app));
60
- case 8:
52
+ case 7:
61
53
  case "end":
62
54
  return _context.stop();
63
55
  }
64
56
  }, _callee);
65
57
  }));
66
58
  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
+ })));
67
102
  }
package/dist/types.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  /// <reference types="react" />
2
- import type { IhtmlPageOpts } from '@umijs/server/dist/types';
3
2
  import type { RouteMatch, RouteObject } from 'react-router-dom';
4
3
  export interface IRouteSSRProps {
5
4
  clientLoader?: () => Promise<any>;
@@ -34,35 +33,3 @@ export interface IRouteComponents {
34
33
  export interface ILoaderData {
35
34
  [routeKey: string]: any;
36
35
  }
37
- interface IHtmlHydrateOptions {
38
- htmlPageOpts?: IhtmlPageOpts;
39
- renderFromRoot?: boolean;
40
- mountElementId?: string;
41
- }
42
- export interface IRootComponentOptions extends IHtmlHydrateOptions {
43
- routes: IRoutesById;
44
- routeComponents: IRouteComponents;
45
- pluginManager: any;
46
- location: string;
47
- loaderData: {
48
- [routeKey: string]: any;
49
- };
50
- manifest: any;
51
- }
52
- export interface IHtmlProps extends IHtmlHydrateOptions {
53
- children?: React.ReactNode;
54
- loaderData?: {
55
- [routeKey: string]: any;
56
- };
57
- manifest?: any;
58
- }
59
- export declare type IScript = Partial<{
60
- async: boolean;
61
- charset: string;
62
- content: string;
63
- crossOrigin: string | null;
64
- defer: boolean;
65
- src: string;
66
- type: string;
67
- }> | string;
68
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/renderer-react",
3
- "version": "4.0.0-canary.20240509.4",
3
+ "version": "4.0.0-canary.20240511.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",
package/dist/html.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import React from 'react';
2
- import { IHtmlProps } from './types';
3
- export declare function Html({ children, loaderData, manifest, htmlPageOpts, renderFromRoot, mountElementId, }: React.PropsWithChildren<IHtmlProps>): JSX.Element;
package/dist/html.js DELETED
@@ -1,148 +0,0 @@
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 _htmlPageOpts$favicon, _htmlPageOpts$keyword, _htmlPageOpts$metas, _htmlPageOpts$links, _htmlPageOpts$styles, _htmlPageOpts$headScr;
37
- var htmlPageOpts = props.htmlPageOpts;
38
- return /*#__PURE__*/React.createElement(React.Fragment, null, (htmlPageOpts === null || htmlPageOpts === void 0 ? void 0 : htmlPageOpts.title) && /*#__PURE__*/React.createElement("title", null, htmlPageOpts.title), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$favicon = htmlPageOpts.favicons) === null || _htmlPageOpts$favicon === void 0 ? void 0 : _htmlPageOpts$favicon.map(function (favicon, key) {
39
- return /*#__PURE__*/React.createElement("link", {
40
- key: key,
41
- rel: "shortcut icon",
42
- href: favicon
43
- });
44
- }), (htmlPageOpts === null || htmlPageOpts === void 0 ? void 0 : htmlPageOpts.description) && /*#__PURE__*/React.createElement("meta", {
45
- name: "description",
46
- content: htmlPageOpts.description
47
- }), (htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$keyword = htmlPageOpts.keywords) === null || _htmlPageOpts$keyword === void 0 ? void 0 : _htmlPageOpts$keyword.length) && /*#__PURE__*/React.createElement("meta", {
48
- name: "keywords",
49
- content: htmlPageOpts.keywords.join(',')
50
- }), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$metas = htmlPageOpts.metas) === null || _htmlPageOpts$metas === void 0 ? void 0 : _htmlPageOpts$metas.map(function (em) {
51
- return /*#__PURE__*/React.createElement("meta", {
52
- key: em.name,
53
- name: em.name,
54
- content: em.content
55
- });
56
- }), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$links = htmlPageOpts.links) === null || _htmlPageOpts$links === void 0 ? void 0 : _htmlPageOpts$links.map(function (link, key) {
57
- return /*#__PURE__*/React.createElement("link", _extends({
58
- key: key
59
- }, link));
60
- }), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$styles = htmlPageOpts.styles) === null || _htmlPageOpts$styles === void 0 ? void 0 : _htmlPageOpts$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
- }), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$headScr = htmlPageOpts.headScripts) === null || _htmlPageOpts$headScr === void 0 ? void 0 : _htmlPageOpts$headScr.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
- dangerouslySetInnerHTML: {
82
- __html: content
83
- },
84
- key: key
85
- }, rest));
86
- }));
87
- };
88
- export function Html(_ref) {
89
- var _htmlPageOpts$scripts;
90
- var children = _ref.children,
91
- loaderData = _ref.loaderData,
92
- manifest = _ref.manifest,
93
- htmlPageOpts = _ref.htmlPageOpts,
94
- renderFromRoot = _ref.renderFromRoot,
95
- mountElementId = _ref.mountElementId;
96
- // TODO: 处理 head 标签,比如 favicon.ico 的一致性
97
- // TODO: root 支持配置
98
- if (renderFromRoot) {
99
- return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(HydrateMetadata, {
100
- htmlPageOpts: htmlPageOpts
101
- }), /*#__PURE__*/React.createElement("div", {
102
- id: mountElementId
103
- }, children));
104
- }
105
- var serverBuildManifest = typeof window === 'undefined' ? manifest :
106
- // @ts-ignore
107
- window.__UMI_BUILD_MANIFEST_DATA__;
108
- return (
109
- /*#__PURE__*/
110
- // FIXME: Resolve the hydrate warning for suppressHydrationWarning(3)
111
- React.createElement("html", {
112
- suppressHydrationWarning: true,
113
- lang: (htmlPageOpts === null || htmlPageOpts === void 0 ? void 0 : htmlPageOpts.lang) || 'en'
114
- }, /*#__PURE__*/React.createElement("head", null, /*#__PURE__*/React.createElement("meta", {
115
- charSet: "utf-8"
116
- }), /*#__PURE__*/React.createElement("meta", {
117
- name: "viewport",
118
- content: "width=device-width, initial-scale=1"
119
- }), (serverBuildManifest === null || serverBuildManifest === void 0 ? void 0 : serverBuildManifest.assets['umi.css']) && /*#__PURE__*/React.createElement("link", {
120
- suppressHydrationWarning: true,
121
- rel: "stylesheet",
122
- href: manifest === null || manifest === void 0 ? void 0 : manifest.assets['umi.css']
123
- }), /*#__PURE__*/React.createElement(HydrateMetadata, {
124
- htmlPageOpts: htmlPageOpts
125
- })), /*#__PURE__*/React.createElement("body", null, /*#__PURE__*/React.createElement("noscript", {
126
- dangerouslySetInnerHTML: {
127
- __html: "<b>Enable JavaScript to run this app.</b>"
128
- }
129
- }), /*#__PURE__*/React.createElement("div", {
130
- id: mountElementId
131
- }, children), /*#__PURE__*/React.createElement("script", {
132
- suppressHydrationWarning: true,
133
- dangerouslySetInnerHTML: {
134
- __html: "window.__UMI_LOADER_DATA__ = ".concat(JSON.stringify(loaderData || {}), "; window.__UMI_METADATA_LOADER_DATA__ = ").concat(JSON.stringify(htmlPageOpts || {}), "; window.__UMI_BUILD_MANIFEST_DATA__ = ").concat(JSON.stringify(manifest) || {})
135
- }
136
- }), htmlPageOpts === null || htmlPageOpts === void 0 || (_htmlPageOpts$scripts = htmlPageOpts.scripts) === null || _htmlPageOpts$scripts === void 0 ? void 0 : _htmlPageOpts$scripts.map(function (script, key) {
137
- var _normalizeScripts2 = normalizeScripts(script),
138
- content = _normalizeScripts2.content,
139
- rest = _objectWithoutProperties(_normalizeScripts2, _excluded2);
140
- return /*#__PURE__*/React.createElement("script", _extends({
141
- dangerouslySetInnerHTML: {
142
- __html: content
143
- },
144
- key: key
145
- }, rest));
146
- })))
147
- );
148
- }