From 7d7a092eee2796d683967c9d2771c63653272a19 Mon Sep 17 00:00:00 2001 From: Magefan - e-Commerce solutions you can trust Date: Fri, 5 Sep 2025 17:16:01 +0300 Subject: [PATCH] 13931-performance-optimization --- Model/Controller/ResultPlugin.php | 80 ++++++++++++++++++------------- 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/Model/Controller/ResultPlugin.php b/Model/Controller/ResultPlugin.php index ad9c5a0..feb365e 100644 --- a/Model/Controller/ResultPlugin.php +++ b/Model/Controller/ResultPlugin.php @@ -79,69 +79,85 @@ public function aroundRenderResult( return $result; } + $ignoredStrings = $this->config->getIgnoreJavaScript() ?: ''; $ignoredStrings = explode("\n", str_replace("\r", "\n", $ignoredStrings)); - foreach ($ignoredStrings as $key => $ignoredString) { - $ignoredString = trim($ignoredString); - if (!$ignoredString) { - unset($ignoredStrings[$key]); - } else { - $ignoredStrings[$key] = $ignoredString; - } - } + $ignoredStrings = array_map('trim', $ignoredStrings); + $ignoredStrings = array_filter($ignoredStrings); $html = $response->getBody(); $scripts = []; + $positions = []; $startTag = ' 1000) { - return $result; - } + // First pass: find all script tags and their positions + while (false !== ($start = stripos($html, $startTag, $start))) { $end = stripos($html, $endTag, $start); if (false === $end) { break; } - $len = $end + strlen($endTag) - $start; - $script = substr($html, $start, $len); - - if (false !== stripos($script, self::EXCLUDE_FLAG_PATTERN)) { - $start++; - continue; - } + $scriptEnd = $end + strlen($endTag); + $script = substr($html, $start, $scriptEnd - $start); - if (false !== stripos($script, 'application/ld+json')) { - $start++; + // Check for exclusion flags or ignored content + if (false !== stripos($script, self::EXCLUDE_FLAG_PATTERN) || + false !== stripos($script, 'application/ld+json')) { + $start = $scriptEnd; // Move pointer past this script continue; } + $isIgnored = false; foreach ($ignoredStrings as $ignoredString) { if (false !== stripos($script, $ignoredString)) { - $start++; - continue 2; + $isIgnored = true; + break; } } - $html = str_replace($script, '', $html); + if ($isIgnored) { + $start = $scriptEnd; + continue; + } + + // Store the script and its position $scripts[] = $script; + $positions[] = ['start' => $start, 'end' => $scriptEnd]; + $start = $scriptEnd; // Move pointer for the next search + } + + // Second pass: reconstruct HTML and append scripts + if (empty($positions)) { + return $response; // No scripts found to move } - $scripts = implode(PHP_EOL, $scripts); - $end = stripos($html, ''); - if ($end !== false) { - $html = substr($html, 0, $end) . $scripts . substr($html, $end); + $newHtml = ''; + $lastPos = 0; + + foreach ($positions as $pos) { + // Append the HTML content before the current script tag + $newHtml .= substr($html, $lastPos, $pos['start'] - $lastPos); + $lastPos = $pos['end']; + } + + // Append the remaining HTML after the last script tag + $newHtml .= substr($html, $lastPos); + + // Append the scripts before the closing tag or at the end + $allScripts = implode(PHP_EOL, $scripts); + $bodyEndPos = stripos($newHtml, ''); + if ($bodyEndPos !== false) { + $newHtml = substr_replace($newHtml, $allScripts, $bodyEndPos, 0); } else { - $html .= $scripts; + $newHtml .= $allScripts; } - $response->setBody($html); + $response->setBody($newHtml); return $result; }