Skip to content

Commit

Permalink
feat: Log large assets that are initially loaded on the frontend
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Jun 25, 2024
1 parent 6121325 commit ecfbcc6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/private/Template/ResourceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OC\Template;

use OC\SystemConfig;
use Psr\Log\LoggerInterface;

abstract class ResourceLocator {
Expand All @@ -20,6 +21,8 @@ abstract class ResourceLocator {

protected LoggerInterface $logger;

private const LOG_LARGE_ASSET_OFFSET = 1024 * 1024;

public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
$this->mapping = [
Expand Down Expand Up @@ -76,6 +79,10 @@ public function find($resources) {
*/
protected function appendIfExist($root, $file, $webRoot = null) {
if ($root !== false && is_file($root.'/'.$file)) {
$systemConfig = \OCP\Server::get(SystemConfig::class);
if ($systemConfig->getValue('debug', false) && filesize($root.'/'.$file) > self::LOG_LARGE_ASSET_OFFSET) {
$this->logger->debug("$root/$file is larger then 1MB, consider reducing the size for javascript entrypoints");
}
$this->append($root, $file, $webRoot, false);
return true;
}
Expand Down
9 changes: 7 additions & 2 deletions tests/lib/Template/ResourceLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ public function getResourceLocator($theme) {
$systemConfig
->expects($this->any())
->method('getValue')
->with('theme', '')
->willReturn($theme);
->willReturnCallback(function ($key, $default = null) use ($theme) {
if ($key === 'theme') {
return $theme;
}

return $default;
});
$this->overwriteService(SystemConfig::class, $systemConfig);
return $this->getMockForAbstractClass('OC\Template\ResourceLocator',
[$this->logger],
Expand Down

0 comments on commit ecfbcc6

Please sign in to comment.