|
@@ -186,49 +186,53 @@ function _loadConfig({ contextRoot, host, protocol, room }) {
|
186
|
186
|
|
187
|
187
|
// TDOO userinfo
|
188
|
188
|
|
189
|
|
- const rootUrl = `${protocol}//${host}${contextRoot || '/'}`;
|
190
|
|
- let url = `${rootUrl}config.js`;
|
|
189
|
+ const baseURL = `${protocol}//${host}${contextRoot || '/'}`;
|
|
190
|
+ let url = `${baseURL}config.js`;
|
191
|
191
|
|
192
|
192
|
// XXX In order to support multiple shards, tell the room to the deployment.
|
193
|
193
|
room && (url += `?room=${room.toLowerCase()}`);
|
194
|
194
|
|
195
|
195
|
/* eslint-enable no-param-reassign */
|
196
|
196
|
|
197
|
|
- const key = `config/${rootUrl}`;
|
|
197
|
+ const key = `config.js/${baseURL}`;
|
198
|
198
|
|
199
|
|
- return (
|
200
|
|
- loadConfig(url)
|
201
|
|
- .then(config => {
|
202
|
|
- // Try to store the configuration in localStorage. If the
|
203
|
|
- // deployment specified the 'getroom' option as a function, for
|
204
|
|
- // example, we cannot store it, so don't.
|
205
|
|
- try {
|
|
199
|
+ return loadConfig(url).then(
|
|
200
|
+ /* onFulfilled */ config => {
|
|
201
|
+ // Try to store the configuration in localStorage. If the deployment
|
|
202
|
+ // specified 'getroom' as a function, for example, it does not make
|
|
203
|
+ // sense to and it will not be stored.
|
|
204
|
+ try {
|
|
205
|
+ if (typeof window.config === 'undefined'
|
|
206
|
+ || window.config !== config) {
|
206
|
207
|
window.localStorage.setItem(key, JSON.stringify(config));
|
207
|
|
- } catch (e) {
|
208
|
|
-
|
209
|
|
- // Ignore the error, we won't cache this config.
|
210
|
208
|
}
|
|
209
|
+ } catch (e) {
|
|
210
|
+ // Ignore the error because the caching is optional.
|
|
211
|
+ }
|
|
212
|
+
|
|
213
|
+ return config;
|
|
214
|
+ },
|
|
215
|
+ /* onRejected */ error => {
|
|
216
|
+ // XXX The (down)loading of config failed. Try to use the last
|
|
217
|
+ // successfully fetched for that deployment. It may not match the
|
|
218
|
+ // shard.
|
|
219
|
+ let storage;
|
211
|
220
|
|
212
|
|
- return config;
|
213
|
|
- })
|
214
|
|
- .catch(error => {
|
215
|
|
- // We failed to load the requested config, try to use the last
|
216
|
|
- // one which was fetched for that deployment. It may not match
|
217
|
|
- // the shard, but it's probably better than nothing.
|
218
|
|
- const config = window.localStorage.getItem(key);
|
|
221
|
+ try {
|
|
222
|
+ // XXX Even reading the property localStorage of window may
|
|
223
|
+ // throw an error (which is user agent-specific behavior).
|
|
224
|
+ storage = window.localStorage;
|
|
225
|
+
|
|
226
|
+ const config = storage.getItem(key);
|
219
|
227
|
|
220
|
228
|
if (config) {
|
221
|
|
- try {
|
222
|
|
- return JSON.parse(config);
|
223
|
|
- } catch (e) {
|
224
|
|
-
|
225
|
|
- // Somehow incorrect data ended up in the storage. Clean
|
226
|
|
- // up.
|
227
|
|
- window.localStorage.removeItem(key);
|
228
|
|
- }
|
|
229
|
+ return JSON.parse(config);
|
229
|
230
|
}
|
|
231
|
+ } catch (e) {
|
|
232
|
+ // Somehow incorrect data ended up in the storage. Clean it up.
|
|
233
|
+ storage && storage.removeItem(key);
|
|
234
|
+ }
|
230
|
235
|
|
231
|
|
- throw error;
|
232
|
|
- })
|
233
|
|
- );
|
|
236
|
+ throw error;
|
|
237
|
+ });
|
234
|
238
|
}
|