Use the now available @once directive instead of the legacy hack to only include component scripts once

This commit is contained in:
Bernd Bestel
2021-07-05 22:49:51 +02:00
parent 10fcd9177c
commit 079437384e
16 changed files with 33 additions and 30 deletions

View File

@@ -715,36 +715,6 @@
<script src="{{ $U('/js/grocy_nightmode.js?v=', true) }}{{ $version }}"></script>
<script src="{{ $U('/js/grocy_clock.js?v=', true) }}{{ $version }}"></script>
@stack('pageScripts')
@php
// @stack('componentScripts') maybe contains the components JS file reference multiple times
// if the component was included more than once in the view
//
// So this is a ugly hack to keep only unique JS file references there
// The property is normally protected, so change that
$reflection = new \ReflectionClass($__env);
$property = $reflection->getProperty('pushes');
$property->setAccessible(true);
$env = $property->getValue($__env);
if (array_key_exists('componentScripts', $env))
{
// Take every line into a new array, one element per line
$filteredStack = array_map(function($value)
{
return explode("#SEP#", str_replace(array("\n", "\r", "\t"), '#SEP#', trim($value)));
}, $env['componentScripts']);
// Flatten the array into a single one, only keep unique lines, remove empty lines, add a defined new line
$filteredStack = preg_filter('/$/', "\n", array_filter(array_unique(array_merge(...$filteredStack))));
// Write it back
$env['componentScripts'] = $filteredStack;
$property->setValue($__env, $env);
}
@endphp
@stack('componentScripts')
@hasSection('viewJsName')<script src="{{ $U('/viewjs', true) }}/@yield('viewJsName').js?v={{ $version }}"></script>@endif