@umijs/renderer-react 4.0.0-rc.15 → 4.0.0-rc.18

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.
@@ -1,3 +1,4 @@
1
1
  import React from 'react';
2
2
  export declare const AppContext: React.Context<any>;
3
3
  export declare function useAppData(): any;
4
+ export declare function useClientLoaderData(): any;
@@ -1,5 +1,11 @@
1
1
  import React from 'react';
2
+ import { useRouteData } from './routeContext';
2
3
  export const AppContext = React.createContext(null);
3
4
  export function useAppData() {
4
5
  return React.useContext(AppContext);
5
6
  }
7
+ export function useClientLoaderData() {
8
+ const route = useRouteData();
9
+ const appData = useAppData();
10
+ return appData.clientLoaderData[route.route.id];
11
+ }
package/dist/browser.d.ts CHANGED
@@ -2,6 +2,8 @@ import { History } from 'history';
2
2
  import React from 'react';
3
3
  import { IRouteComponents, IRoutesById } from './types';
4
4
  export declare function renderClient(opts: {
5
+ publicPath?: string;
6
+ runtimePublicPath?: boolean;
5
7
  rootElement?: HTMLElement;
6
8
  routes: IRoutesById;
7
9
  routeComponents: IRouteComponents;
package/dist/browser.js CHANGED
@@ -1,7 +1,7 @@
1
- import React from 'react';
1
+ import React, { useCallback, useEffect, useState } from 'react';
2
2
  // compatible with < react@18 in @umijs/preset-umi/src/features/react
3
3
  import ReactDOM from 'react-dom/client';
4
- import { Router, useRoutes } from 'react-router-dom';
4
+ import { matchRoutes, Router, useRoutes } from 'react-router-dom';
5
5
  import { AppContext, useAppData } from './appContext';
6
6
  import { createClientRoutes } from './routes';
7
7
  function BrowserRoutes(props) {
@@ -66,16 +66,68 @@ export function renderClient(opts) {
66
66
  args: {},
67
67
  });
68
68
  }
69
- const browser = (React.createElement(AppContext.Provider, { value: {
70
- routes: opts.routes,
71
- routeComponents: opts.routeComponents,
72
- clientRoutes,
73
- pluginManager: opts.pluginManager,
74
- rootElement: opts.rootElement,
75
- basename,
76
- } }, rootContainer));
69
+ const Browser = () => {
70
+ const [clientLoaderData, setClientLoaderData] = useState({});
71
+ const handleRouteChange = useCallback((p) => {
72
+ var _a;
73
+ const matchedRouteIds = ((_a = matchRoutes(clientRoutes, p)) === null || _a === void 0 ? void 0 : _a.map(
74
+ // @ts-ignore
75
+ (route) => route.route.id)) || [];
76
+ matchedRouteIds.forEach((id) => {
77
+ // preload
78
+ // @ts-ignore
79
+ const manifest = window.__umi_manifest__;
80
+ if (manifest) {
81
+ const routeIdReplaced = id.replace(/[\/\-]/g, '_');
82
+ const preloadId = 'preload-' + routeIdReplaced;
83
+ if (!document.getElementById(preloadId)) {
84
+ const key = Object.keys(manifest).find((k) => k.startsWith(routeIdReplaced + '.'));
85
+ if (!key)
86
+ return;
87
+ let file = manifest[key];
88
+ const link = document.createElement('link');
89
+ link.id = preloadId;
90
+ link.rel = 'preload';
91
+ link.as = 'script';
92
+ // publicPath already in the manifest,
93
+ // but if runtimePublicPath is true, we need to replace it
94
+ if (opts.runtimePublicPath) {
95
+ file = file.replace(new RegExp(`^${opts.publicPath}`),
96
+ // @ts-ignore
97
+ window.publicPath);
98
+ }
99
+ link.href = file;
100
+ document.head.appendChild(link);
101
+ }
102
+ }
103
+ // client loader
104
+ const clientLoader = opts.routes[id].clientLoader;
105
+ if (clientLoader && !clientLoaderData[id]) {
106
+ clientLoader().then((data) => {
107
+ setClientLoaderData((d) => ({ ...d, [id]: data }));
108
+ });
109
+ }
110
+ });
111
+ }, [clientLoaderData]);
112
+ useEffect(() => {
113
+ handleRouteChange(window.location.pathname);
114
+ return opts.history.listen((e) => {
115
+ handleRouteChange(e.location.pathname);
116
+ });
117
+ }, []);
118
+ return (React.createElement(AppContext.Provider, { value: {
119
+ routes: opts.routes,
120
+ routeComponents: opts.routeComponents,
121
+ clientRoutes,
122
+ pluginManager: opts.pluginManager,
123
+ rootElement: opts.rootElement,
124
+ basename,
125
+ clientLoaderData,
126
+ preloadRoute: handleRouteChange,
127
+ } }, rootContainer));
128
+ };
77
129
  if (ReactDOM.createRoot) {
78
- ReactDOM.createRoot(rootElement).render(browser);
130
+ ReactDOM.createRoot(rootElement).render(React.createElement(Browser, null));
79
131
  }
80
132
  else {
81
133
  // @ts-ignore
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { createBrowserHistory, createHashHistory, createMemoryHistory, History, } from 'history';
2
- export { createSearchParams, Link, matchPath, matchRoutes, Navigate, NavLink, Outlet, useLocation, useMatch, useNavigate, useOutlet, useParams, useResolvedPath, useRoutes, useSearchParams, } from 'react-router-dom';
3
- export { useAppData } from './appContext';
2
+ export { createSearchParams, matchPath, matchRoutes, Navigate, NavLink, Outlet, useLocation, useMatch, useNavigate, useOutlet, useParams, useResolvedPath, useRoutes, useSearchParams, } from 'react-router-dom';
3
+ export { useAppData, useClientLoaderData } from './appContext';
4
4
  export { renderClient } from './browser';
5
+ export { LinkWithPrefetch as Link } from './link';
5
6
  export { useRouteData } from './routeContext';
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export { createBrowserHistory, createHashHistory, createMemoryHistory, } from 'history';
2
- export { createSearchParams, Link, matchPath, matchRoutes, Navigate, NavLink, Outlet, useLocation, useMatch, useNavigate, useOutlet, useParams, useResolvedPath, useRoutes, useSearchParams, } from 'react-router-dom';
3
- export { useAppData } from './appContext';
2
+ export { createSearchParams, matchPath, matchRoutes, Navigate, NavLink, Outlet, useLocation, useMatch, useNavigate, useOutlet, useParams, useResolvedPath, useRoutes, useSearchParams, } from 'react-router-dom';
3
+ export { useAppData, useClientLoaderData } from './appContext';
4
4
  export { renderClient } from './browser';
5
+ export { LinkWithPrefetch as Link } from './link';
5
6
  export { useRouteData } from './routeContext';
package/dist/link.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { PropsWithChildren } from 'react';
2
+ export declare function LinkWithPrefetch(props: PropsWithChildren<{
3
+ prefetch?: boolean;
4
+ to: string | Partial<{
5
+ pathname: string;
6
+ search: string;
7
+ hash: string;
8
+ }>;
9
+ replace?: boolean;
10
+ state?: boolean;
11
+ reloadDocument?: boolean;
12
+ }>): JSX.Element;
package/dist/link.js ADDED
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ import { Link } from 'react-router-dom';
3
+ import { useAppData } from './appContext';
4
+ export function LinkWithPrefetch(props) {
5
+ const appData = useAppData();
6
+ const to = typeof props.to === 'string' ? props.to : props.to.pathname;
7
+ return (React.createElement(Link, { onMouseEnter: () => props.prefetch && appData.preloadRoute(to), to: props.to, replace: props.replace, state: props.state, reloadDocument: props.reloadDocument }, props.children));
8
+ }
package/dist/routes.d.ts CHANGED
@@ -7,6 +7,7 @@ export declare function createClientRoutes(opts: {
7
7
  loadingComponent?: React.ReactNode;
8
8
  }): {
9
9
  parentId?: string | undefined;
10
+ clientLoader?: (() => Promise<any>) | undefined;
10
11
  id: string;
11
12
  path: string | undefined;
12
13
  index: boolean | undefined;
@@ -18,6 +19,7 @@ export declare function createClientRoute(opts: {
18
19
  loadingComponent?: React.ReactNode;
19
20
  }): {
20
21
  parentId?: string | undefined;
22
+ clientLoader?: (() => Promise<any>) | undefined;
21
23
  id: string;
22
24
  path: string | undefined;
23
25
  index: boolean | undefined;
package/dist/types.d.ts CHANGED
@@ -4,6 +4,7 @@ export interface IRoute {
4
4
  index?: boolean;
5
5
  parentId?: string;
6
6
  redirect?: string;
7
+ clientLoader?: () => Promise<any>;
7
8
  }
8
9
  export interface IRoutesById {
9
10
  [id: string]: IRoute;
@@ -11,3 +12,6 @@ export interface IRoutesById {
11
12
  export interface IRouteComponents {
12
13
  [id: string]: any;
13
14
  }
15
+ export interface ILoaderData {
16
+ [routeKey: string]: any;
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/renderer-react",
3
- "version": "4.0.0-rc.15",
3
+ "version": "4.0.0-rc.18",
4
4
  "description": "@umijs/renderer-react",
5
5
  "homepage": "https://github.com/umijs/umi-next/tree/master/packages/renderer-react#readme",
6
6
  "bugs": "https://github.com/umijs/umi-next/issues",