Skip to content

Commit

Permalink
feat: Add dedicated method to load init scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
Co-authored-by: John Molakvoæ <[email protected]>

Update lib/public/Util.php

Co-authored-by: Côme Chilliet <[email protected]>
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr and come-nc committed Sep 7, 2023
1 parent eb55101 commit bd65f1e
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions lib/public/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
use OC\AppScriptDependency;
use OC\AppScriptSort;
use bantu\IniGetWrapper\IniGetWrapper;
use OCP\Share\IManager;
use Psr\Container\ContainerExceptionInterface;

/**
Expand All @@ -57,17 +58,11 @@
* @since 4.0.0
*/
class Util {
/** @var \OCP\Share\IManager */
private static $shareManager;
private static ?IManager $shareManager = null;

/** @var array */
private static $scripts = [];

/** @var array */
private static $scriptDeps = [];

/** @var array */
private static $sortedScriptDeps = [];
private static array $scriptsInit = [];
private static array $scripts = [];
private static array $scriptDeps = [];

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

/**
* Add a standalone init js file that is loaded for initialization
*
* Be careful loading scripts using this method as they are loaded early
* and block the initial page rendering. They should not have dependencies
* on any other scripts than core-common and core-main.
*
* @since 28.0.0
*/
public static function addInitScript(string $application, string $file): void {
if (!empty($application)) {
$path = "$application/js/$file";
} else {
$path = "js/$file";
}

self::$scriptsInit[] = $path;
}

/**
* add a javascript file
*
Expand Down Expand Up @@ -214,7 +228,8 @@ public static function getScripts(): array {
$sortedScripts = $scriptSort->sort(self::$scripts, self::$scriptDeps);

// Flatten array and remove duplicates
$sortedScripts = $sortedScripts ? array_merge(...array_values(($sortedScripts))) : [];
$sortedScripts = array_merge([self::$scriptsInit], $sortedScripts);
$sortedScripts = array_merge(...array_values($sortedScripts));

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

0 comments on commit bd65f1e

Please sign in to comment.