diff --git a/client/api/misc.js b/client/api/misc.js index 0ece914b..696e71cb 100644 --- a/client/api/misc.js +++ b/client/api/misc.js @@ -1,12 +1,15 @@ import wsc from './webSocketConnection'; class Misc { - async loadConfig() { + async loadConfig(_configHash) { - const query = {params: [ - 'name', 'version', 'mode', 'maxUploadFileSize', 'useExternalBookConverter', - 'acceptFileExt', 'bucEnabled', 'branch', 'networkLibraryLink', 'restricted' - ]}; + const query = { + params: [ + 'name', 'version', 'mode', 'maxUploadFileSize', 'useExternalBookConverter', + 'acceptFileExt', 'bucEnabled', 'branch', 'networkLibraryLink', 'restricted' + ], + _configHash, + }; const config = await wsc.message(await wsc.send(Object.assign({action: 'get-config'}, query))); if (config.error) diff --git a/client/components/App.vue b/client/components/App.vue index 99c4ca0a..1b76c6f2 100644 --- a/client/components/App.vue +++ b/client/components/App.vue @@ -154,8 +154,11 @@ class App { (async() => { //загрузим конфиг сервера try { - const config = await miscApi.loadConfig(); - this.commit('config/setConfig', config); + const config = await miscApi.loadConfig(this.config._configHash); + + if (!config._useCached) + this.commit('config/setConfig', config); + this.showPage = true; } catch(e) { //проверим, не получен ли конфиг ранее diff --git a/client/store/modules/config.js b/client/store/modules/config.js index 32852f7f..41a3ad92 100644 --- a/client/store/modules/config.js +++ b/client/store/modules/config.js @@ -1,4 +1,3 @@ -import miscApi from '../../api/misc'; // initial state const state = { name: null, diff --git a/server/controllers/WebSocketController.js b/server/controllers/WebSocketController.js index c17aa98d..c0f5606a 100644 --- a/server/controllers/WebSocketController.js +++ b/server/controllers/WebSocketController.js @@ -20,6 +20,8 @@ class WebSocketController { this.readerWorker = new ReaderWorker(config); this.workerState = new WorkerState(); + this.configHash = ''; + if (config.bucEnabled) { this.bucClient = new BUCClient(config); } @@ -119,8 +121,22 @@ class WebSocketController { async getConfig(req, ws) { if (Array.isArray(req.params)) { const paramsSet = new Set(req.params); + const _configHash = req._configHash; - this.send(_.pick(this.config, this.config.webConfigParams.filter(x => paramsSet.has(x))), req, ws); + let response = {_useCached: true}; + + //оптимизация, чтобы не отдавал большой конфиг каждый раз при обновлении страницы + if (!_configHash || _configHash !== this.configHash) { + if (!this.configHash) { + const webConfig = _.pick(this.config, this.config.webConfigParams); + this.configHash = await utils.getBufHash(Buffer.from(JSON.stringify(webConfig)), 'sha256', 'hex'); + } + + response = _.pick(this.config, this.config.webConfigParams.filter(x => paramsSet.has(x))); + response._configHash = this.configHash; + } + + this.send(response, req, ws); } else { throw new Error('params is not an array'); }