@umijs/renderer-react 4.0.88 → 4.0.89
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/appContext.d.ts +3 -3
- package/dist/appContext.js +48 -3
- package/dist/browser.js +13 -22
- package/dist/dataFetcher.d.ts +5 -0
- package/dist/dataFetcher.js +20 -0
- package/dist/types.d.ts +6 -0
- package/package.json +1 -1
package/dist/appContext.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { IClientRoute, ILoaderData, IRouteComponents, IRoutesById } from './types';
|
|
2
|
+
import { IClientRoute, ILoaderData, IRouteComponents, IRoutesById, ISelectedRoutes } from './types';
|
|
3
3
|
interface IAppContextType {
|
|
4
4
|
routes: IRoutesById;
|
|
5
5
|
routeComponents: IRouteComponents;
|
|
@@ -14,11 +14,11 @@ interface IAppContextType {
|
|
|
14
14
|
}
|
|
15
15
|
export declare const AppContext: React.Context<IAppContextType>;
|
|
16
16
|
export declare function useAppData(): IAppContextType;
|
|
17
|
-
export declare function useSelectedRoutes():
|
|
17
|
+
export declare function useSelectedRoutes(): ISelectedRoutes[];
|
|
18
18
|
export declare function useRouteProps<T extends Record<string, any> = any>(): T;
|
|
19
19
|
declare type ServerLoaderFunc = (...args: any[]) => Promise<any> | any;
|
|
20
20
|
export declare function useServerLoaderData<T extends ServerLoaderFunc = any>(): {
|
|
21
|
-
data: Awaited<ReturnType<T
|
|
21
|
+
data: Awaited<ReturnType<T>> | undefined;
|
|
22
22
|
};
|
|
23
23
|
export declare function useClientLoaderData(): {
|
|
24
24
|
data: any;
|
package/dist/appContext.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
1
2
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
2
3
|
var _excluded = ["element"];
|
|
3
4
|
import React from 'react';
|
|
4
5
|
import { matchRoutes, useLocation } from 'react-router-dom';
|
|
6
|
+
import { fetchServerLoader } from "./dataFetcher";
|
|
5
7
|
import { useRouteData } from "./routeContext";
|
|
6
8
|
export var AppContext = /*#__PURE__*/React.createContext({});
|
|
7
9
|
export function useAppData() {
|
|
@@ -24,10 +26,53 @@ export function useRouteProps() {
|
|
|
24
26
|
return props;
|
|
25
27
|
}
|
|
26
28
|
export function useServerLoaderData() {
|
|
27
|
-
var
|
|
28
|
-
var
|
|
29
|
+
var routes = useSelectedRoutes();
|
|
30
|
+
var _useAppData2 = useAppData(),
|
|
31
|
+
serverLoaderData = _useAppData2.serverLoaderData,
|
|
32
|
+
basename = _useAppData2.basename;
|
|
33
|
+
var _React$useState = React.useState(function () {
|
|
34
|
+
var ret = {};
|
|
35
|
+
var has = false;
|
|
36
|
+
routes.forEach(function (route) {
|
|
37
|
+
// 多级路由嵌套时,需要合并多级路由 serverLoader 的数据
|
|
38
|
+
var routeData = serverLoaderData[route.route.id];
|
|
39
|
+
if (routeData) {
|
|
40
|
+
Object.assign(ret, routeData);
|
|
41
|
+
has = true;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return has ? ret : undefined;
|
|
45
|
+
}),
|
|
46
|
+
_React$useState2 = _slicedToArray(_React$useState, 2),
|
|
47
|
+
data = _React$useState2[0],
|
|
48
|
+
setData = _React$useState2[1];
|
|
49
|
+
React.useEffect(function () {
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
if (!window.__UMI_LOADER_DATA__) {
|
|
52
|
+
// 支持 ssr 降级,客户端兜底加载 serverLoader 数据
|
|
53
|
+
Promise.all(routes.filter(function (route) {
|
|
54
|
+
return route.route.hasServerLoader;
|
|
55
|
+
}).map(function (route) {
|
|
56
|
+
return new Promise(function (resolve) {
|
|
57
|
+
fetchServerLoader({
|
|
58
|
+
id: route.route.id,
|
|
59
|
+
basename: basename,
|
|
60
|
+
cb: resolve
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
})).then(function (datas) {
|
|
64
|
+
if (datas.length) {
|
|
65
|
+
var res = {};
|
|
66
|
+
datas.forEach(function (data) {
|
|
67
|
+
Object.assign(res, data);
|
|
68
|
+
});
|
|
69
|
+
setData(res);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}, []);
|
|
29
74
|
return {
|
|
30
|
-
data:
|
|
75
|
+
data: data
|
|
31
76
|
};
|
|
32
77
|
}
|
|
33
78
|
export function useClientLoaderData() {
|
package/dist/browser.js
CHANGED
|
@@ -6,6 +6,7 @@ import React, { useCallback, useEffect, useLayoutEffect, useState } from 'react'
|
|
|
6
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
|
+
import { fetchServerLoader } from "./dataFetcher";
|
|
9
10
|
import { createClientRoutes } from "./routes";
|
|
10
11
|
var root = null;
|
|
11
12
|
|
|
@@ -182,26 +183,19 @@ var getBrowser = function getBrowser(opts, routesElement) {
|
|
|
182
183
|
// server loader
|
|
183
184
|
// use ?. since routes patched with patchClientRoutes is not exists in opts.routes
|
|
184
185
|
if (!isFirst && (_opts$routes$id = opts.routes[id]) !== null && _opts$routes$id !== void 0 && _opts$routes$id.hasServerLoader) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
}).then(function (data) {
|
|
196
|
-
// setServerLoaderData when startTransition because if ssr is enabled,
|
|
197
|
-
// the component may being hydrated and setLoaderData will break the hydration
|
|
198
|
-
// @ts-ignore
|
|
199
|
-
React.startTransition(function () {
|
|
200
|
-
setServerLoaderData(function (d) {
|
|
201
|
-
return _objectSpread(_objectSpread({}, d), {}, _defineProperty({}, id, data));
|
|
186
|
+
fetchServerLoader({
|
|
187
|
+
id: id,
|
|
188
|
+
basename: basename,
|
|
189
|
+
cb: function cb(data) {
|
|
190
|
+
// setServerLoaderData when startTransition because if ssr is enabled,
|
|
191
|
+
// the component may being hydrated and setLoaderData will break the hydration
|
|
192
|
+
React.startTransition(function () {
|
|
193
|
+
setServerLoaderData(function (d) {
|
|
194
|
+
return _objectSpread(_objectSpread({}, d), {}, _defineProperty({}, id, data));
|
|
195
|
+
});
|
|
202
196
|
});
|
|
203
|
-
}
|
|
204
|
-
})
|
|
197
|
+
}
|
|
198
|
+
});
|
|
205
199
|
}
|
|
206
200
|
// client loader
|
|
207
201
|
// onPatchClientRoutes 添加的 route 在 opts.routes 里是不存在的
|
|
@@ -263,7 +257,4 @@ export function renderClient(opts) {
|
|
|
263
257
|
}
|
|
264
258
|
// @ts-ignore
|
|
265
259
|
ReactDOM.render( /*#__PURE__*/React.createElement(Browser, null), rootElement);
|
|
266
|
-
}
|
|
267
|
-
function withEndSlash(str) {
|
|
268
|
-
return str.endsWith('/') ? str : "".concat(str, "/");
|
|
269
260
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function fetchServerLoader(_ref) {
|
|
2
|
+
var id = _ref.id,
|
|
3
|
+
basename = _ref.basename,
|
|
4
|
+
cb = _ref.cb;
|
|
5
|
+
var query = new URLSearchParams({
|
|
6
|
+
route: id,
|
|
7
|
+
url: window.location.href
|
|
8
|
+
}).toString();
|
|
9
|
+
// 在有basename的情况下__serverLoader的请求路径需要加上basename
|
|
10
|
+
var url = "".concat(withEndSlash(basename), "__serverLoader?").concat(query);
|
|
11
|
+
fetch(url, {
|
|
12
|
+
credentials: 'include'
|
|
13
|
+
}).then(function (d) {
|
|
14
|
+
return d.json();
|
|
15
|
+
}).then(cb).catch(console.error);
|
|
16
|
+
}
|
|
17
|
+
function withEndSlash() {
|
|
18
|
+
var str = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
19
|
+
return str.endsWith('/') ? str : "".concat(str, "/");
|
|
20
|
+
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
+
import type { RouteMatch, RouteObject } from 'react-router-dom';
|
|
2
3
|
export interface IRouteSSRProps {
|
|
3
4
|
clientLoader?: () => Promise<any>;
|
|
4
5
|
hasServerLoader?: boolean;
|
|
@@ -18,6 +19,11 @@ export interface IClientRoute extends IRoute {
|
|
|
18
19
|
children?: IClientRoute[];
|
|
19
20
|
routes?: IClientRoute[];
|
|
20
21
|
}
|
|
22
|
+
export interface ISelectedRoute extends IRoute, RouteObject {
|
|
23
|
+
}
|
|
24
|
+
export interface ISelectedRoutes extends RouteMatch {
|
|
25
|
+
route: ISelectedRoute;
|
|
26
|
+
}
|
|
21
27
|
export interface IRoutesById {
|
|
22
28
|
[id: string]: IRoute;
|
|
23
29
|
}
|
package/package.json
CHANGED