-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Lazy load uncommon CFG_GLPI values #21528
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * --------------------------------------------------------------------- | ||
| * | ||
| * GLPI - Gestionnaire Libre de Parc Informatique | ||
| * | ||
| * http://glpi-project.org | ||
| * | ||
| * @copyright 2015-2025 Teclib' and contributors. | ||
| * @copyright 2003-2014 by the INDEPNET Development Team. | ||
| * @licence https://www.gnu.org/licenses/gpl-3.0.html | ||
| * | ||
| * --------------------------------------------------------------------- | ||
| * | ||
| * LICENSE | ||
| * | ||
| * This file is part of GLPI. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| * | ||
| * --------------------------------------------------------------------- | ||
| */ | ||
|
|
||
| use Glpi\Exception\Http\NotFoundHttpException; | ||
| use function Safe\json_encode; | ||
|
|
||
| Session::checkLoginUser(); | ||
| Session::writeClose(); | ||
|
|
||
| header("Content-Type: application/json; charset=UTF-8"); | ||
| $safe_config = Config::getSafeConfig(true); | ||
| if (!isset($_GET['key']) || !array_key_exists($_GET['key'], $safe_config)) { | ||
| throw new NotFoundHttpException(); | ||
| } | ||
|
|
||
| echo json_encode($safe_config[$_GET['key']]); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -293,14 +293,54 @@ public function configJs(): string | |
| $cfg_glpi += Config::getSafeConfig(true); | ||
| } | ||
|
|
||
| // Common config keys used by front-end code. | ||
| // These will be immediately available while others will be lazy-loaded via a proxy handler. | ||
| $common_configs = [ | ||
| 'root_doc', 'url_base', 'planning_begin', 'planning_end', 'planning_work_days', 'toast_location', | ||
| 'is_demo_dashboards', 'priority_1', 'priority_2', 'priority_3', 'priority_4', 'priority_5', 'priority_6', | ||
| 'date_format' | ||
| ]; | ||
| $common_config = array_filter( | ||
| $cfg_glpi, | ||
| static fn($key) => in_array($key, $common_configs, true), | ||
| ARRAY_FILTER_USE_KEY | ||
| ); | ||
| $common_config = json_encode($common_config); | ||
|
|
||
| $plugins_path = \array_combine( | ||
| Plugin::getPlugins(), | ||
| \array_map(fn(string $plugin_key) => "/plugins/{$plugin_key}", Plugin::getPlugins()) | ||
| \array_map(static fn(string $plugin_key) => "/plugins/{$plugin_key}", Plugin::getPlugins()) | ||
| ); | ||
|
|
||
| $script = sprintf('window.CFG_GLPI = %s;', json_encode($cfg_glpi, JSON_PRETTY_PRINT)) | ||
| . "\n" | ||
| . sprintf('window.GLPI_PLUGINS_PATH = %s;', json_encode($plugins_path, JSON_PRETTY_PRINT)); | ||
| $plugins_path = json_encode($plugins_path); | ||
|
|
||
| $script = <<<JS | ||
| window.CFG_GLPI = new Proxy($common_config, { | ||
| get: (target, prop, receiver) => { | ||
| if (prop in target) { | ||
| return Reflect.get(target, prop, receiver); | ||
| } | ||
| // Note there was a request for a config not in the common set to indicate a potential optimization | ||
| console.debug("Lazy loading a CFG_GLPI property that was not available by default: " + prop + '. This may be an optimization opportunity.'); | ||
| // Hydrate the missing config via a synchronous AJAX call | ||
| $.ajax({ | ||
| type: 'GET', | ||
| url: CFG_GLPI.root_doc + '/ajax/config.php', | ||
| async: false, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed on similar changes, we might disagree on this but I am against adding synchronous calls. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't disagree on this point. If it is async though, something will try using a config value that isn't loaded, trigger it to be loaded, and immediately get no value since it didn't wait for the response. I don't want it to be synchronous but it needs to be to make this work with existing code. We could close the PR and leave the entire dump available, but I'm just trying to improve performance as best I can with what we have for now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think your idea is great, it is a nightmare to send 50k chars for the config object in every pages and it would definitely be an improvement to only send useful values (since 99% of the config is likely never used as you said). I'm just not sold on the solution because I really think synchronous requests should never be added into new code. If you can make it work another way, I'll gladly approve the PR :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My only other solution is a comprehensive rework which people aren't onboard with currently, or at least conversion of the legacy JS to modules/classes where we can pass the needed configs through in Twig which cannot really be done in a bugfix version either IMO. Either way, I cannot fix the plugins which may use other config values so we are very limited in terms of solutions at this point if we want to avoid BC breaks. |
||
| dataType: 'json', | ||
| data: { key: prop }, | ||
| success: (value) => { | ||
| target[prop] = value; | ||
| }, | ||
| error: () => { | ||
| console.warn('Failed to load config property ' + prop); | ||
| target[prop] = undefined; | ||
| } | ||
| }); | ||
| return Reflect.get(target, prop, receiver); | ||
| } | ||
| }); | ||
| window.GLPI_PLUGINS_PATH = $plugins_path; | ||
| JS; | ||
|
|
||
| return Html::scriptBlock($script); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.