@@ -26,7 +26,8 @@ private function extract_jsonld_schema(&$html) {
2626 if (preg_match ('/<head[^>]*>.*?<script[^>]*id=[" \']jsonldSchema[" \'][^>]*type=[" \']application\/ld\+json[" \'][^>]*>(.*?)<\/script>.*?<\/head>/is ' , $ html , $ matches )) {
2727 // Remove JSON-LD schema from head
2828 $ html = preg_replace ('/<script[^>]*id=[" \']jsonldSchema[" \'][^>]*type=[" \']application\/ld\+json[" \'][^>]*>.*?<\/script>/is ' , '' , $ html );
29- return '<script id="jsonldSchema" type="application/ld+json"> ' . trim ($ matches [1 ]) . '</script> ' ;
29+ // Return with Nitropack exclusion attributes to prevent reordering and optimization
30+ return '<script id="jsonldSchema" type="application/ld+json" data-nitro-exclude="all" data-nitro-ignore="true" data-nitro-no-optimize="true" data-nitro-preserve-ws="true"> ' . trim ($ matches [1 ]) . '</script> ' ;
3031 }
3132 return '' ;
3233 }
@@ -37,6 +38,7 @@ private function extract_jsonld_schema(&$html) {
3738 * Adds Nitropack exclusion attributes to prevent optimization issues
3839 */
3940 private function extract_head_resources ($ html ) {
41+ $ preconnect_links = '' ;
4042 $ font_links = '' ;
4143 $ other_links = '' ;
4244 $ scripts = '' ;
@@ -48,8 +50,15 @@ private function extract_head_resources($html) {
4850 // Extract all link tags
4951 if (preg_match_all ('/<link[^>]*>/i ' , $ head_content , $ link_matches )) {
5052 foreach ($ link_matches [0 ] as $ link ) {
53+ // Prioritize preconnect links for fonts (must come before font links)
54+ if (preg_match ('/rel=[" \']preconnect[" \']/i ' , $ link )) {
55+ if (strpos ($ link , 'data-nitro-exclude ' ) === false ) {
56+ $ link = str_replace ('> ' , ' data-nitro-exclude="all" data-nitro-ignore="true"> ' , $ link );
57+ }
58+ $ preconnect_links .= $ link . "\n" ;
59+ }
5160 // Prioritize font links (Google Fonts, etc.)
52- if (preg_match ('/fonts\.(googleapis|gstatic)/i ' , $ link ) || preg_match ('/font/i ' , $ link )) {
61+ else if (preg_match ('/fonts\.(googleapis|gstatic)/i ' , $ link ) || ( preg_match ('/rel=[" \' ]stylesheet[" \' ]/i ' , $ link ) && preg_match ( ' / font/i ' , $ link) )) {
5362 // Add Nitropack exclusion attributes to font links to prevent optimization issues
5463 if (strpos ($ link , 'data-nitro-exclude ' ) === false ) {
5564 // Insert data-nitro attributes before closing >
@@ -72,8 +81,8 @@ private function extract_head_resources($html) {
7281 }
7382 }
7483
75- // Return with font links first
76- return trim ($ font_links . $ other_links . $ scripts );
84+ // Return with preconnect links first, then font links, then other resources
85+ return trim ($ preconnect_links . $ font_links . $ other_links . $ scripts );
7786 }
7887
7988 /**
@@ -134,9 +143,15 @@ public function init($atts) {
134143 $ reviews = $ this ->remove_head_section ($ reviews );
135144
136145 // Output in correct order:
137- // 1. JSON-LD schema first (for Google crawler/SEO)
146+ // 1. JSON-LD schema first (for Google crawler/SEO) - wrapped to prevent Nitropack reordering
138147 if (!empty ($ jsonld_schema )) {
148+ // Wrap schema in a container with Nitropack exclusion to preserve position
149+ // Script tags are invisible, so this wrapper doesn't affect rendering but prevents reordering
150+ echo '<!-- OPIO JSON-LD SCHEMA START --> ' ;
151+ echo '<div data-nitro-exclude="all" data-nitro-ignore="true" data-nitro-no-optimize="true" data-nitro-preserve-ws="true" data-nitro-preserve-order="true"> ' ;
139152 echo $ jsonld_schema ;
153+ echo '</div> ' ;
154+ echo '<!-- OPIO JSON-LD SCHEMA END --> ' ;
140155 }
141156
142157 // 2. Head resources (links and scripts) - must come before div with "all: initial"
0 commit comments