-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/preset-umi/src/features/transitionRouter/transitionRouter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
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`)), | ||
]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
试了下会影响约定式 loading 的功能,在
startTransition
中切换路由时,Suspense
的fallback
组件就不会渲染了,不知道有没有其他解法There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是的,因为
startTransition
标记这个不是紧急的渲染,这样fallback
就不会立即出现切换 UI ,这个是符合startTransition
预期的,由于Suspense
的fallback
其实很短,会一闪而过,体验不好,所以为了提升体验,现在更推荐使用非紧急的更新切换路由,包括 nextjs 等也是一样,切换的时候会阻塞,不会闪屏 loading 。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其他框架的页面 loading 方案是怎么处理的呢,开启这个 Umi 的 loading 就失效了,希望能在解决注水错误的同时让 loading 继续生效