From c266b53ff1d06f6e88bdee9c635e4aa4d42a4847 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Mon, 10 Feb 2020 20:21:52 +0100 Subject: [PATCH] Added an ugly hack to prevent including the same component JS file multiple times --- views/layout/default.blade.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/views/layout/default.blade.php b/views/layout/default.blade.php index d07a4500..577bfa84 100644 --- a/views/layout/default.blade.php +++ b/views/layout/default.blade.php @@ -468,6 +468,33 @@ @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); + + // 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))); + }, $property->getValue($__env)['componentScripts']); + + // Flatten the array into a single one, only keep unique lines, remove empty lines + $filteredStack = array_filter(array_unique(array_merge(...$filteredStack))); + + // Write it back + $env = $property->getValue($__env); + $env['componentScripts'] = $filteredStack; + $property->setValue($__env, $env); + @endphp + @stack('componentScripts') @hasSection('viewJsName')@endif