Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add config.disableRedirectHoist #1091

Merged
merged 3 commits into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ Configure routing.
If `routes` is configured, the negotiated route will not take effect.
:::

### disableRedirectHoist

* 类型:`Boolean`
* 默认值:`false`

For some reason, we hoist all redirect when parsing the route config, but this caused some problems, so add this configuration to disable redirect hoist.

### history

* Type: `String`
Expand Down Expand Up @@ -73,6 +80,13 @@ Specifies the publicPath of the webpack, pointing to the path where the static r

Use the `window.publicPath` specified in the HTML when the value is `true`.

### hash

* Type: `Boolean`
* Default: `false`

Whether to enable the hash file suffix.

### context

* Type: `Object`
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ Make sure node version is above 8 and then execute:
$ node --inspect-brk ./node_modules/.bin/umi test
```

Then open [chrome://inspect/#devices](chrome://inspect/#devices) in the browser for inspect and breakpoints.
Then open chrome://inspect/#devices in the browser for inspect and breakpoints.

## Deployment

Expand Down
16 changes: 16 additions & 0 deletions docs/zh/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ export default {
如果配置了 `routes`,则约定式路由会不生效。
:::

### disableRedirectHoist

* 类型:`Boolean`
* 默认值:`false`

禁用 redirect 上提。

出于一些原因的考虑,我们在处理路由时把所有 redirect 声明提到路由最前面进行匹配,但这导致了一些问题,所以添加了这个配置项,禁用 redirect 上提。

### history

* 类型:`String`
Expand Down Expand Up @@ -73,6 +82,13 @@ export default {

值为 `true` 时使用 HTML 里指定的 `window.publicPath`。

### hash

* Type: `Boolean`
* Default: `false`

是否开启 hash 文件后缀。

### context

* 类型:`Object`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import assert from 'assert';

export default function(api) {
return {
name: 'disableRedirectHoist',
validate(val) {
assert(
typeof val === 'boolean',
`disableRedirectHoist should be Boolean, but got ${val.toString()}.`,
);
},
onChange() {
api.service.rebuildTmpFiles();
},
};
}
14 changes: 9 additions & 5 deletions packages/umi-build-dev/src/routes/patchRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ let redirects;
export default (routes, config = {}, isProduction, onPatchRoute) => {
redirects = [];
patchRoutes(routes, config, isProduction, onPatchRoute);
routes.unshift(...redirects);
if (!config.disableRedirectHoist) {
routes.unshift(...redirects);
}
return routes;
};

Expand Down Expand Up @@ -37,10 +39,12 @@ function patchRoutes(routes, config, isProduction, onPatchRoute) {
});
}

const removedRoutes = remove(routes, route => {
return route.redirect;
});
redirects = redirects.concat(removedRoutes);
if (!config.disableRedirectHoist) {
const removedRoutes = remove(routes, route => {
return route.redirect;
});
redirects = redirects.concat(removedRoutes);
}
}

function patchRoute(route, config, isProduction, onPatchRoute) {
Expand Down
32 changes: 32 additions & 0 deletions packages/umi-build-dev/src/routes/patchRoutes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,36 @@ describe('patchRoutes', () => {
},
]);
});

it('disable redirect hoist', () => {
const routes = patchRoutes(
[
{ path: '/a', component: './A' },
{ path: '/b', redirect: '/c' },
{
path: '/c',
routes: [
{ path: '/c/d', component: 'D' },
{ path: '/c/e', redirect: '/c/f' },
{ path: '/c/f', component: 'F' },
],
},
],
{
disableRedirectHoist: true,
},
);
expect(routes).toEqual([
{ path: '/a', component: './A' },
{ path: '/b', redirect: '/c' },
{
path: '/c',
routes: [
{ path: '/c/d', component: 'D' },
{ path: '/c/e', redirect: '/c/f' },
{ path: '/c/f', component: 'F' },
],
},
]);
});
});