Skip to content

Commit bd65f1e

Browse files
juliusknorrcome-nc
andcommitted
feat: Add dedicated method to load init scripts
Signed-off-by: Julius Härtl <jus@bitgrid.net> Co-authored-by: John Molakvoæ <skjnldsv@protonmail.com> Update lib/public/Util.php Co-authored-by: Côme Chilliet <91878298+come-nc@users.noreply.github.com> Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent eb55101 commit bd65f1e

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

lib/public/Util.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
use OC\AppScriptDependency;
5050
use OC\AppScriptSort;
5151
use bantu\IniGetWrapper\IniGetWrapper;
52+
use OCP\Share\IManager;
5253
use Psr\Container\ContainerExceptionInterface;
5354

5455
/**
@@ -57,17 +58,11 @@
5758
* @since 4.0.0
5859
*/
5960
class Util {
60-
/** @var \OCP\Share\IManager */
61-
private static $shareManager;
61+
private static ?IManager $shareManager = null;
6262

63-
/** @var array */
64-
private static $scripts = [];
65-
66-
/** @var array */
67-
private static $scriptDeps = [];
68-
69-
/** @var array */
70-
private static $sortedScriptDeps = [];
63+
private static array $scriptsInit = [];
64+
private static array $scripts = [];
65+
private static array $scriptDeps = [];
7166

7267
/**
7368
* get the current installed version of Nextcloud
@@ -163,6 +158,25 @@ public static function addStyle($application, $file = null) {
163158
\OC_Util::addStyle($application, $file);
164159
}
165160

161+
/**
162+
* Add a standalone init js file that is loaded for initialization
163+
*
164+
* Be careful loading scripts using this method as they are loaded early
165+
* and block the initial page rendering. They should not have dependencies
166+
* on any other scripts than core-common and core-main.
167+
*
168+
* @since 28.0.0
169+
*/
170+
public static function addInitScript(string $application, string $file): void {
171+
if (!empty($application)) {
172+
$path = "$application/js/$file";
173+
} else {
174+
$path = "js/$file";
175+
}
176+
177+
self::$scriptsInit[] = $path;
178+
}
179+
166180
/**
167181
* add a javascript file
168182
*
@@ -214,7 +228,8 @@ public static function getScripts(): array {
214228
$sortedScripts = $scriptSort->sort(self::$scripts, self::$scriptDeps);
215229

216230
// Flatten array and remove duplicates
217-
$sortedScripts = $sortedScripts ? array_merge(...array_values(($sortedScripts))) : [];
231+
$sortedScripts = array_merge([self::$scriptsInit], $sortedScripts);
232+
$sortedScripts = array_merge(...array_values($sortedScripts));
218233

219234
// Override core-common and core-main order
220235
if (in_array('core/js/main', $sortedScripts)) {

0 commit comments

Comments
 (0)