Skip to content

Commit 478ae95

Browse files
susnuxbackportbot[bot]
authored andcommitted
fix(Util): getScripts also need to reorder core translations
Currently `core-common` and `core-main` are prepanded to the scripts, as they provide the global state. But the script ordering logic does not know about this and might sort core translations after they are used. Meaning we need to register core translations as soon as the global scope is initialized (so that `OC.L10N.register` is available). Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 56451fe commit 478ae95

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

lib/public/Util.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,27 @@ public static function getScripts(): array {
188188
// Flatten array and remove duplicates
189189
$sortedScripts = array_merge([self::$scriptsInit], $sortedScripts);
190190
$sortedScripts = array_merge(...array_values($sortedScripts));
191+
$sortedScripts = array_unique($sortedScripts);
191192

192-
// Override core-common and core-main order
193-
if (in_array('core/js/main', $sortedScripts)) {
194-
array_unshift($sortedScripts, 'core/js/main');
195-
}
196-
if (in_array('core/js/common', $sortedScripts)) {
197-
array_unshift($sortedScripts, 'core/js/common');
198-
}
193+
usort($sortedScripts, fn (string $a, string $b) => self::scriptOrderValue($b) <=> self::scriptOrderValue($a));
194+
return $sortedScripts;
195+
}
199196

200-
return array_unique($sortedScripts);
197+
/**
198+
* Gets a numeric value based on the script name.
199+
* This is used to ensure that the global state is initialized before all other scripts.
200+
*
201+
* @param string $name - The script name
202+
* @since 34.0.0
203+
*/
204+
private static function scriptOrderValue(string $name): int {
205+
return match($name) {
206+
'core/js/common' => 3,
207+
'core/js/main' => 2,
208+
default => str_starts_with($name, 'core/l10n/')
209+
? 1 // core translations have to be loaded directly after core-main
210+
: 0, // other scripts should preserve their current order
211+
};
201212
}
202213

203214
/**

0 commit comments

Comments
 (0)