Skip to content

Commit 996ac00

Browse files
fix live router & csr
1 parent f72fda4 commit 996ac00

File tree

11 files changed

+2361
-2352
lines changed

11 files changed

+2361
-2352
lines changed

package.json

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-ssr",
3-
"version": "1.2.1",
3+
"version": "1.2.8",
44
"author": "mrwang",
55
"license": "MIT",
66
"scripts": {
@@ -19,17 +19,17 @@
1919
"@emotion/styled": "^11.6.0",
2020
"@loadable/component": "^5.15.2",
2121
"@loadable/server": "^5.15.2",
22-
"@mui/material": "^5.3.1",
22+
"@mui/material": "^5.4.0",
2323
"antd": "^4.18.5",
2424
"axios": "^0.25.0",
2525
"chalk": "4",
2626
"compression": "^1.7.4",
2727
"cookie-parser": "^1.4.6",
2828
"cors": "^2.8.5",
29-
"dotenv": "^14.3.2",
29+
"dotenv": "^16.0.0",
3030
"express": "^4.17.2",
3131
"express-session": "^1.17.1",
32-
"framer-motion": "^6.2.3",
32+
"framer-motion": "^6.2.4",
3333
"immer": "^9.0.12",
3434
"js-cookie": "^3.0.1",
3535
"lodash": "^4.17.21",
@@ -46,21 +46,21 @@
4646
"redux-saga": "^1.1.3",
4747
"redux-thunk": "^2.4.1",
4848
"spark-md5": "^3.0.1",
49-
"webpack": "^5.67.0",
49+
"webpack": "^5.68.0",
5050
"zustand": "^3.6.9"
5151
},
5252
"devDependencies": {
53-
"@babel/cli": "^7.16.8",
54-
"@babel/core": "^7.16.12",
53+
"@babel/cli": "^7.17.0",
54+
"@babel/core": "^7.17.0",
5555
"@babel/plugin-proposal-class-properties": "^7.16.7",
56-
"@babel/plugin-proposal-decorators": "^7.16.7",
56+
"@babel/plugin-proposal-decorators": "^7.17.0",
5757
"@babel/plugin-proposal-export-default-from": "^7.16.7",
5858
"@babel/plugin-proposal-object-rest-spread": "^7.16.7",
5959
"@babel/plugin-proposal-optional-chaining": "^7.16.7",
6060
"@babel/plugin-proposal-private-methods": "^7.16.11",
6161
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
6262
"@babel/plugin-transform-modules-commonjs": "^7.16.8",
63-
"@babel/plugin-transform-runtime": "^7.16.10",
63+
"@babel/plugin-transform-runtime": "^7.17.0",
6464
"@babel/preset-env": "^7.16.11",
6565
"@babel/preset-react": "^7.16.7",
6666
"@babel/preset-typescript": "^7.16.7",
@@ -77,24 +77,24 @@
7777
"@types/loadable__server": "^5.12.3",
7878
"@types/lodash": "^4.14.178",
7979
"@types/multer": "^1.4.7",
80-
"@types/node": "^17.0.13",
81-
"@types/react": "^17.0.38",
80+
"@types/node": "^17.0.14",
81+
"@types/react": "^17.0.39",
8282
"@types/react-dom": "^17.0.11",
8383
"@types/react-redux": "^7.1.22",
8484
"@types/webpack": "^5.28.0",
8585
"@types/webpack-env": "^1.16.3",
8686
"@types/webpack-hot-middleware": "^2.25.4",
87-
"@typescript-eslint/eslint-plugin": "^5.10.1",
88-
"@typescript-eslint/parser": "^5.10.1",
87+
"@typescript-eslint/eslint-plugin": "^5.10.2",
88+
"@typescript-eslint/parser": "^5.10.2",
8989
"autoprefixer": "^10.4.2",
9090
"babel-loader": "^8.2.2",
9191
"babel-plugin-import": "^1.13.3",
9292
"clean-webpack-plugin": "^4.0.0",
93-
"core-js": "^3.20.3",
93+
"core-js": "^3.21.0",
9494
"cross-env": "^7.0.3",
95-
"css-loader": "^6.5.1",
95+
"css-loader": "^6.6.0",
9696
"css-minimizer-webpack-plugin": "^3.4.1",
97-
"eslint": "^8.7.0",
97+
"eslint": "^8.8.0",
9898
"eslint-config-prettier": "^8.3.0",
9999
"eslint-import-resolver-typescript": "^2.5.0",
100100
"eslint-plugin-import": "^2.25.4",
@@ -104,23 +104,23 @@
104104
"eslint-plugin-react-hooks": "^4.3.0",
105105
"eslint-webpack-plugin": "^3.1.1",
106106
"file-loader": "^6.2.0",
107-
"fork-ts-checker-webpack-plugin": "^6.5.0",
107+
"fork-ts-checker-webpack-plugin": "^7.0.0",
108108
"mini-css-extract-plugin": "^2.5.3",
109109
"node-sass": "^7.0.1",
110110
"nodemon": "^2.0.15",
111-
"postcss": "^8.4.5",
111+
"postcss": "^8.4.6",
112112
"postcss-loader": "^6.2.1",
113113
"prettier": "^2.5.1",
114114
"react-refresh": "^0.11.0",
115-
"sass": "^1.49.0",
115+
"sass": "^1.49.7",
116116
"sass-loader": "^12.4.0",
117117
"style-loader": "^3.2.1",
118118
"thread-loader": "^3.0.4",
119119
"typescript": "^4.5.5",
120120
"webpack-bundle-analyzer": "^4.4.0",
121121
"webpack-cli": "^4.9.2",
122-
"webpack-dev-middleware": "^5.3.0",
123-
"webpack-dev-server": "^4.7.3",
122+
"webpack-dev-middleware": "^5.3.1",
123+
"webpack-dev-server": "^4.7.4",
124124
"webpack-hot-middleware": "^2.25.1",
125125
"webpack-manifest-plugin": "^4.1.1",
126126
"webpack-merge": "^5.7.3",

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ yarn run start
5454

5555
## 静态路由
5656

57-
## 文件路由 自动 query 参数支持 使用 :参数名(:id.tsx) 文件作为的约定 开发环境支持 page 新建删除的实时路由更新(middleware 模式下支持, 加载一次后删除会出错)
57+
## 文件路由 自动 query 参数支持 使用 :参数名(:id.tsx) 文件作为的约定 开发环境支持 page 新建删除的实时路由更新(middleware 模式下支持)
5858

5959
## 装饰器
6060

src/client/entry.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ if (__UI__ === "material") {
5050

5151
if (__CSR__) {
5252
log("pure render by client", "warn");
53-
const { allRoutes } = require("router/routes");
54-
const { preLoad, preLoadLang } = require("utils/preLoad");
55-
Promise.all([preLoadLang({ store, lang: window.__ENV__.LANG }), preLoad(allRoutes, location.pathname, store)]).then(() =>
56-
loadableReady(() => render(<Root store={store} />, place))
57-
);
53+
const { preLoadLang } = require("utils/preLoad");
54+
preLoadLang({ store, lang: window.__ENV__.LANG }).then(() => loadableReady(() => render(<Root store={store} />, place)));
5855
} else {
5956
if (!window.__ENV__.isSSR) {
6057
loadableReady(() => render(<Root store={store} />, place));

src/components/WrapperRoute/index.tsx

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { createContext, useContext } from "react";
22

3-
import { useHydrate } from "hooks/useHydrate";
43
import { usePreLoad } from "hooks/usePreLoad";
5-
import { preLoad } from "utils/preLoad";
4+
import { hydrateLoad, preLoad } from "utils/preLoad";
65

76
import type { useLocation } from "react-router";
87
import type { WrapperRouteType } from "types/components";
98

109
export const LoadedLocationContext = createContext<ReturnType<typeof useLocation> | Record<string, never>>({});
1110

1211
export const WrapperRoute: WrapperRouteType = ({ children, routes, LoadingBar }) => {
13-
const { location } = usePreLoad({ routes, preLoad });
14-
useHydrate({ routes, pathName: location.pathname });
12+
const { location } = usePreLoad({ routes, preLoad, hydrate: hydrateLoad });
13+
14+
// for pure client render
15+
if (!location) return null;
1516

1617
return (
1718
<LoadedLocationContext.Provider value={location}>

src/hooks/useHydrate.ts

-16
This file was deleted.

src/hooks/usePreLoad.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cookie from "js-cookie";
2-
import { useEffect, useRef, useState } from "react";
2+
import { useEffect, useMemo, useRef, useState } from "react";
33
import { useStore } from "react-redux";
44
import { useLocation, useNavigate } from "react-router";
55

@@ -10,28 +10,37 @@ import { useChangeLoadingWithoutRedux } from "./useLoadingBar";
1010
import type { UsePreLoadType } from "types/hooks";
1111

1212
/* WrapperRoute */
13-
const usePreLoad: UsePreLoadType = ({ routes, preLoad }) => {
13+
const usePreLoad: UsePreLoadType = ({ routes, preLoad, hydrate }) => {
1414
const isRedirect = useRef<string | undefined>();
1515
const store = useStore();
1616
const location = useLocation();
1717
const navigate = useNavigate();
1818
const { start, end } = useChangeLoadingWithoutRedux();
19-
const firstLoad = useRef(true);
20-
const loadedPath = useRef<string | null>("");
19+
// for pure client render, need preload data
20+
const firstLoad = useRef(__CSR__ ? false : true);
21+
const loadedPath = useRef<string | undefined>("");
2122
const loadingPath = useRef<string | null>("");
2223
const timer1 = useRef<NodeJS.Timeout | null>(null);
2324
const timer2 = useRef<NodeJS.Timeout | null>(null);
2425
const storeRef = useRef(store);
25-
const [loadedLocation, setLoadedLocation] = useState(location);
26+
// for pure client render, there are not exist loaded location
27+
const [loadedLocation, setLoadedLocation] = useState(__CSR__ ? undefined : location);
2628

2729
loadingPath.current = location.pathname;
2830

29-
loadedPath.current = loadedLocation.pathname;
31+
loadedPath.current = loadedLocation?.pathname;
3032

3133
storeRef.current = store;
3234

35+
useMemo(() => {
36+
if (loadedLocation?.pathname) {
37+
hydrate(routes, loadedLocation.pathname);
38+
}
39+
// eslint-disable-next-line react-hooks/exhaustive-deps
40+
}, []);
41+
3342
useEffect(() => {
34-
// skip first load
43+
// skip first load if need
3544
if (!firstLoad.current) {
3645
const isRedirectCurrentPath = isRedirect.current && isRedirect.current === location.pathname;
3746
if (!isRedirectCurrentPath) {

src/server/middleware/renderPage/middleware/globalEnv.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const globalEnv: Middleware = (next) => async (args) => {
66
const { PUBLIC_API_HOST, CRYPTO_KEY } = process.env;
77
args.env = {
88
UI: __UI__,
9-
isSSR: getIsSSR() || args.req.query.isSSR,
9+
isSSR: getIsSSR() || args.req.query.isSSR || false,
1010
CRYPTO_KEY,
1111
PUBLIC_API_HOST,
1212
isMIDDLEWARE: getIsMiddleware(),

src/server/middleware/renderPage/renderCSR.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { manifestLoadable } from "utils/manifest";
88
import { composeRender } from "./compose";
99
import { globalEnv, initLang, initStore, loadLang, loadStore } from "./middleware";
1010

11-
import type { AnyAction} from "./compose";
11+
import type { AnyAction } from "./compose";
1212

1313
// 客户端渲染
1414
const targetRender: AnyAction = async ({ res, store, lang, env, serverSideProps = {} }) => {

src/types/hooks/index.d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type { useLocation } from "react-router";
22
import type { PreLoadRouteConfig } from "types/router";
3-
import type { preLoad } from "utils/preLoad";
3+
import type { hydrateLoad, preLoad } from "utils/preLoad";
44

55
interface UsePreLoadProps {
66
routes: PreLoadRouteConfig[];
77
preLoad: typeof preLoad;
8+
hydrate: typeof hydrateLoad;
89
}
910
export interface UsePreLoadType {
1011
(props: UsePreLoadProps): {
1112
loading?: boolean;
12-
location: ReturnType<typeof useLocation>;
13+
location?: ReturnType<typeof useLocation>;
1314
};
1415
}
1516

webpack/optimization.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
33

44
const optimizationConfig = ({ env, isDev = true }) => {
55
if (env === "client") {
6-
return isDev
6+
// 开发模式下 为了使动态路由即使生效不出错, 不能使用这个配置
7+
return isDev && false
78
? {
89
usedExports: true,
910
runtimeChunk: "single",

0 commit comments

Comments
 (0)