Do not use function name to track components (#6542)
UglifyJS2 is allowed to mangle function names, and function names can also be duplicate if they are from different scopes. Therefore function names are not reliable as identifiers. Functions as keys for Map object is a cheaper and more reliable alternative.
This commit is contained in:
parent
4d8c0d9959
commit
7cb49eaa3a
1 changed files with 5 additions and 6 deletions
|
@ -26,7 +26,7 @@ class Bundle extends React.Component {
|
||||||
onFetchFail: noop,
|
onFetchFail: noop,
|
||||||
}
|
}
|
||||||
|
|
||||||
static cache = {}
|
static cache = new Map
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
mod: undefined,
|
mod: undefined,
|
||||||
|
@ -51,13 +51,12 @@ class Bundle extends React.Component {
|
||||||
|
|
||||||
load = (props) => {
|
load = (props) => {
|
||||||
const { fetchComponent, onFetch, onFetchSuccess, onFetchFail, renderDelay } = props || this.props;
|
const { fetchComponent, onFetch, onFetchSuccess, onFetchFail, renderDelay } = props || this.props;
|
||||||
|
const cachedMod = Bundle.cache.get(fetchComponent);
|
||||||
|
|
||||||
onFetch();
|
onFetch();
|
||||||
|
|
||||||
if (Bundle.cache[fetchComponent.name]) {
|
if (cachedMod) {
|
||||||
const mod = Bundle.cache[fetchComponent.name];
|
this.setState({ mod: cachedMod.default });
|
||||||
|
|
||||||
this.setState({ mod: mod.default });
|
|
||||||
onFetchSuccess();
|
onFetchSuccess();
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
}
|
}
|
||||||
|
@ -71,7 +70,7 @@ class Bundle extends React.Component {
|
||||||
|
|
||||||
return fetchComponent()
|
return fetchComponent()
|
||||||
.then((mod) => {
|
.then((mod) => {
|
||||||
Bundle.cache[fetchComponent.name] = mod;
|
Bundle.cache.set(fetchComponent, mod);
|
||||||
this.setState({ mod: mod.default });
|
this.setState({ mod: mod.default });
|
||||||
onFetchSuccess();
|
onFetchSuccess();
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue