Skip to content

Commit 81d2c52

Browse files
committed
Fix plotly.js url on the window at initial mount.
1 parent d501d63 commit 81d2c52

File tree

4 files changed

+32
-71
lines changed

4 files changed

+32
-71
lines changed

components/dash-core-components/src/components/Graph.react.js

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -124,44 +124,20 @@ class PlotlyGraph extends Component {
124124
}
125125
}
126126

127-
let loadingProm = null;
128-
let loadedGraph = null;
129-
130-
const getPlotlyGraph = config => {
131-
if (loadedGraph) {
132-
if (loadedGraph.default) {
133-
// Hot reload get a default
134-
return loadedGraph.default;
135-
}
136-
return loadedGraph;
137-
}
138-
if (loadingProm) {
139-
return loadingProm;
140-
}
141-
142-
loadingProm = asyncDecorator(PlotlyGraph, () =>
143-
Promise.all([
144-
graph(),
145-
plotly(config),
146-
PlotlyGraph._loadMathjax ? lazyLoadMathJax() : undefined,
147-
]).then(([graph]) => {
148-
loadedGraph = graph;
149-
loadingProm = null;
150-
return graph;
151-
})
152-
);
153-
return loadingProm;
154-
};
127+
const RealPlotlyGraph = asyncDecorator(PlotlyGraph, () =>
128+
Promise.all([
129+
graph(),
130+
plotly(),
131+
PlotlyGraph._loadMathjax ? lazyLoadMathJax() : undefined,
132+
]).then(([graph]) => graph)
133+
);
155134

156135
const ControlledPlotlyGraph = memo(props => {
157136
const {className, id} = props;
158137

159138
const extendedClassName = className
160139
? 'dash-graph ' + className
161140
: 'dash-graph';
162-
const config = window._dashUseConfig();
163-
164-
const RealPlotlyGraph = getPlotlyGraph(config);
165141

166142
return (
167143
<Suspense

components/dash-core-components/src/utils/LazyLoader/plotly.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
export default (config) => {
2-
let url;
3-
if (config.serve_locally) {
4-
url = `${config.requests_pathname_prefix}_dash-component-suites/plotly/package_data/plotly.min.js`;
5-
} else {
6-
url = config.plotlyjs_url;
7-
}
1+
export default () => {
82
return Promise.resolve(window.Plotly || new Promise((resolve, reject) => {
93
/* eslint-disable prefer-const */
104
let timeoutId;
115

126
const element = document.createElement('script');
13-
element.src = url;
7+
element.src = window._dashPlotlyJSURL;
148
element.async = true;
159
element.onload = () => {
1610
clearTimeout(timeoutId);
@@ -23,7 +17,7 @@ export default (config) => {
2317

2418
timeoutId = setTimeout(() => {
2519
element.src = '';
26-
reject(new Error(`${url} did not load after 30 seconds`));
20+
reject(new Error(`plotly.js did not load after 30 seconds`));
2721
}, 3 * 10 * 1000);
2822

2923
document.querySelector('body').appendChild(element);

dash/dash-renderer/src/APIController.react.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {getAppState} from './reducers/constants';
2121
import {STATUS} from './constants/constants';
2222
import {getLoadingState, getLoadingHash} from './utils/TreeContainer';
2323
import wait from './utils/wait';
24-
import {ConfigContext} from './config';
2524

2625
export const DashContext = createContext({});
2726

@@ -73,6 +72,14 @@ const UnconnectedContainer = props => {
7372
}
7473
});
7574

75+
useEffect(() => {
76+
if (config.serve_locally) {
77+
window._dashPlotlyJSURL = `${config.requests_pathname_prefix}_dash-component-suites/plotly/package_data/plotly.min.js`;
78+
} else {
79+
window._dashPlotlyJSURL = config.plotlyjs_url;
80+
}
81+
}, []);
82+
7683
let content;
7784
if (
7885
layoutRequest.status &&
@@ -90,22 +97,20 @@ const UnconnectedContainer = props => {
9097

9198
content = (
9299
<DashContext.Provider value={provider.current}>
93-
<ConfigContext.Provider value={config}>
94-
<TreeContainer
95-
_dashprivate_error={error}
96-
_dashprivate_layout={layout}
97-
_dashprivate_loadingState={getLoadingState(
98-
layout,
99-
[],
100-
loadingMap
101-
)}
102-
_dashprivate_loadingStateHash={getLoadingHash(
103-
[],
104-
loadingMap
105-
)}
106-
_dashprivate_path={JSON.stringify([])}
107-
/>
108-
</ConfigContext.Provider>
100+
<TreeContainer
101+
_dashprivate_error={error}
102+
_dashprivate_layout={layout}
103+
_dashprivate_loadingState={getLoadingState(
104+
layout,
105+
[],
106+
loadingMap
107+
)}
108+
_dashprivate_loadingStateHash={getLoadingHash(
109+
[],
110+
loadingMap
111+
)}
112+
_dashprivate_path={JSON.stringify([])}
113+
/>
109114
</DashContext.Provider>
110115
);
111116
} else {

dash/dash-renderer/src/config.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import React from 'react';
2-
31
type Config = {
42
url_base_pathname: string;
53
requests_pathname_prefix: string;
@@ -31,15 +29,3 @@ export default function getConfigFromDOM(): Config {
3129
configElement?.textContent ? configElement?.textContent : '{}'
3230
);
3331
}
34-
35-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
36-
// @ts-ignore
37-
export const ConfigContext = React.createContext<Config>({});
38-
39-
export function useConfig() {
40-
return React.useContext<Config>(ConfigContext);
41-
}
42-
43-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
44-
// @ts-ignore
45-
window._dashUseConfig = useConfig;

0 commit comments

Comments
 (0)