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 transition router feature #11364

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/docs/api/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,15 @@ theme: { '@primary-color': '#1DA57A' }

配置全局页面 title,暂时只支持静态的 Title。

## transitionRouter

- 类型:`object`
- 默认值:`null`

仅 React 18 可用。

在切换页面时自动使用 `startTransition` 标记为非紧急更新,这可以避免 loading 态突然出现又消失的闪烁效果等[问题](https://react.dev/reference/react/Suspense#preventing-already-revealed-content-from-hiding),提升用户体验。

## verifyCommit

- 类型:`{ scope: string[]; allowEmoji: boolean }`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { winPath } from '@umijs/utils';
import { join } from 'path';
import type { IApi } from '../../types';

export default (api: IApi) => {
api.describe({
config: {
schema({ zod }) {
return zod.object({});
},
},
enableBy: api.EnableBy.config,
});

api.onGenerateFiles(() => {
if (api.userConfig.mpa || api.config.mpa) {
throw new Error('transitionRouter plugin does not support mpa mode');
}
if (api.appData.framework !== 'react') {
throw new Error('transitionRouter plugin only support react framework');
}
const isReact18 = (
api.appData.react?.version as string | undefined
)?.startsWith('18');
if (!isReact18) {
throw new Error('transitionRouter plugin only support react@18');
}

// https://github.com/remix-run/remix/issues/5763
api.writeTmpFile({
path: 'runtime.ts',
content: `
import { startTransition } from 'react';

export const modifyClientRenderOpts = (context) => {
const h = context.history
const originPush = h.push
const originReplace = h.replace
h.push = (...args) => {
startTransition(() => originPush.apply(h, args))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

试了下会影响约定式 loading 的功能,在 startTransition 中切换路由时,Suspensefallback 组件就不会渲染了,不知道有没有其他解法

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,因为 startTransition 标记这个不是紧急的渲染,这样 fallback 就不会立即出现切换 UI ,这个是符合 startTransition 预期的,由于 Suspensefallback 其实很短,会一闪而过,体验不好,所以为了提升体验,现在更推荐使用非紧急的更新切换路由,包括 nextjs 等也是一样,切换的时候会阻塞,不会闪屏 loading 。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其他框架的页面 loading 方案是怎么处理的呢,开启这个 Umi 的 loading 就失效了,希望能在解决注水错误的同时让 loading 继续生效

}
h.replace = (...args) => {
startTransition(() => originReplace.apply(h, args))
}
return context
}
`,
});
});

const pluginDir = `plugin-${api.plugin.key}`;
api.addRuntimePlugin(() => [
winPath(join(api.paths.absTmpPath, `${pluginDir}/runtime.ts`)),
]);
};
1 change: 1 addition & 0 deletions packages/preset-umi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default () => {
require.resolve('./features/swc/swc'),
require.resolve('./features/ui/ui'),
require.resolve('./features/hmrGuardian/hmrGuardian'),
require.resolve('./features/transitionRouter/transitionRouter'),

// commands
require.resolve('./commands/build'),
Expand Down