@umijs/server 4.0.0-canary.20220323.1 → 4.0.0-rc.10

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/server.d.ts CHANGED
@@ -18,6 +18,7 @@ export interface IOpts {
18
18
  modifyHTML?: (html: string, args: {
19
19
  path?: string;
20
20
  }) => Promise<string>;
21
+ historyType?: 'hash' | 'browser';
21
22
  }
22
23
  export declare function getMarkup(opts: Omit<IOpts, 'routes'> & {
23
24
  path?: string;
package/dist/server.js CHANGED
@@ -94,19 +94,35 @@ function getMarkup(opts) {
94
94
  exports.getMarkup = getMarkup;
95
95
  function createRequestHandler(opts) {
96
96
  return (req, res, next) => __awaiter(this, void 0, void 0, function* () {
97
- // 匹配路由,不匹配走 next()
98
97
  // TODO: cache
99
- const routes = (0, routes_1.createServerRoutes)({
100
- routesById: opts.routes,
101
- });
102
- const matches = (0, react_router_dom_1.matchRoutes)(routes, req.path, opts.base);
103
- if (matches) {
98
+ let isMatch;
99
+ if (opts.historyType === 'hash') {
100
+ // 如果是 hash 路由时,不做路由匹配,只要是 / 就匹配返回 html
101
+ isMatch = req.path === '/';
102
+ }
103
+ else {
104
+ // 匹配路由,不匹配走 next()
105
+ const routes = (0, routes_1.createServerRoutes)({
106
+ routesById: opts.routes,
107
+ });
108
+ const matches = (0, react_router_dom_1.matchRoutes)(routes, req.path, opts.base);
109
+ isMatch = !!matches;
110
+ }
111
+ if (isMatch) {
104
112
  res.set('Content-Type', 'text/html');
105
113
  const markup = yield getMarkup(Object.assign(Object.assign({}, opts), { path: req.path }));
106
114
  res.end(markup);
107
115
  }
108
116
  else {
109
- next();
117
+ // 如果是 browser,并且配置了非 / base,访问 / 时 redirect 到 base 路径
118
+ if (opts.historyType === 'browser' &&
119
+ opts.base !== '/' &&
120
+ req.path === '/') {
121
+ res.redirect(opts.base);
122
+ }
123
+ else {
124
+ next();
125
+ }
110
126
  }
111
127
  });
112
128
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/server",
3
- "version": "4.0.0-canary.20220323.1",
3
+ "version": "4.0.0-rc.10",
4
4
  "description": "@umijs/server",
5
5
  "homepage": "https://github.com/umijs/umi-next/tree/master/packages/server#readme",
6
6
  "bugs": "https://github.com/umijs/umi-next/issues",
@@ -20,7 +20,7 @@
20
20
  "dev": "pnpm build -- --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@umijs/bundler-utils": "4.0.0-canary.20220323.1",
23
+ "@umijs/bundler-utils": "4.0.0-rc.10",
24
24
  "history": "5.3.0",
25
25
  "react": "17.0.2",
26
26
  "react-dom": "17.0.2",