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

修复 icestark App router prefetch 功能 #732

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/icestark/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ice/stark",
"version": "2.7.5",
"version": "2.7.6",
"description": "Icestark is a JavaScript library for multiple projects, Ice workbench solution.",
"scripts": {
"build": "rm -rf lib && tsc",
Expand Down
26 changes: 21 additions & 5 deletions packages/icestark/src/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface AppRouterProps {
basename?: string;
fetch?: Fetch;
prefetch?: Prefetch;
apps?: AppConfig;
Copy link
Collaborator

Choose a reason for hiding this comment

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

不增加额外参数,直接以 children 上的信息进行判断是不是也可以

Copy link
Author

Choose a reason for hiding this comment

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

也行,可以做到 didupdate 里面,我找时间改下

children?: React.ReactChildren;
}

interface AppRouterState {
Expand Down Expand Up @@ -74,11 +76,7 @@ export default class AppRouter extends React.Component<AppRouterProps, AppRouter
started: false,
};

const { fetch, prefetch: strategy, children } = props;

if (strategy) {
this.prefetch(strategy, children, fetch);
}
this.tryPrefetch(props);
}

componentDidMount() {
Expand Down Expand Up @@ -113,6 +111,24 @@ export default class AppRouter extends React.Component<AppRouterProps, AppRouter
this.setState({ started: false });
}

componentWillReceiveProps(nextProps: Readonly<AppRouterProps>, nextContext: any): void {
if (nextProps.apps !== this.props.apps) {
this.tryPrefetch(nextProps);
Copy link
Collaborator

Choose a reason for hiding this comment

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

在 componentWillReceiveProps 阶段会存在多次执行的情况,需要增加额外机制判断 apps / url 信息是否已完成预加载

Copy link
Author

Choose a reason for hiding this comment

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

ok

}
}

/**
* try to prefetch again
* @param props
*/
tryPrefetch(props: AppRouterProps): void {
const { fetch, prefetch: strategy, children } = props;

if (strategy) {
this.prefetch(strategy, children, fetch);
}
}

/**
* prefetch for resources.
* no worry to excute `prefetch` many times, for all prefetched resources have been cached, and never request twice.
Expand Down