@@ -32,7 +32,7 @@ public function isEnabled(int|string|null $store = null): bool
3232 /**
3333 * Get speculation rules configuration
3434 *
35- * @return array<string, array<int, array{where: array{selector_matches: string}, eagerness: string}>>
35+ * @return array<string, array<int, array{where: array{selector_matches: string|string[] }, eagerness: string}>>
3636 */
3737 public function getSpeculationRules (int |string |null $ store = null ): array
3838 {
@@ -56,6 +56,9 @@ public function getSpeculationRules(int|string|null $store = null): array
5656 ['eagerness ' => 'conservative ' , 'mode ' => 'prerender ' , 'path ' => self ::XML_PATH_CONSERVATIVE_PRERENDER_SELECTORS ],
5757 ];
5858
59+ // Group selectors by mode and eagerness
60+ $ groupedSelectors = [];
61+
5962 foreach ($ configurations as $ config ) {
6063 $ selectorsConfig = (string ) Mage::getStoreConfig ($ config ['path ' ], $ store );
6164
@@ -71,16 +74,40 @@ public function getSpeculationRules(int|string|null $store = null): array
7174 continue ;
7275 }
7376
74- // Create rule for each selector
75- foreach ($ selectors as $ selector ) {
76- if (!empty ($ selector )) {
77- $ rules [$ config ['mode ' ]][] = [
78- 'where ' => [
79- 'selector_matches ' => $ selector ,
80- ],
81- 'eagerness ' => $ config ['eagerness ' ],
82- ];
83- }
77+ // Group selectors by mode and eagerness
78+ $ key = $ config ['mode ' ] . '_ ' . $ config ['eagerness ' ];
79+ if (!isset ($ groupedSelectors [$ key ])) {
80+ $ groupedSelectors [$ key ] = [
81+ 'mode ' => $ config ['mode ' ],
82+ 'eagerness ' => $ config ['eagerness ' ],
83+ 'selectors ' => [],
84+ ];
85+ }
86+
87+ $ groupedSelectors [$ key ]['selectors ' ] = array_merge (
88+ $ groupedSelectors [$ key ]['selectors ' ],
89+ $ selectors
90+ );
91+ }
92+
93+ // Create rules from grouped selectors
94+ foreach ($ groupedSelectors as $ group ) {
95+ if (count ($ group ['selectors ' ]) === 1 ) {
96+ // Single selector
97+ $ rules [$ group ['mode ' ]][] = [
98+ 'where ' => [
99+ 'selector_matches ' => $ group ['selectors ' ][0 ],
100+ ],
101+ 'eagerness ' => $ group ['eagerness ' ],
102+ ];
103+ } else {
104+ // Multiple selectors - use array
105+ $ rules [$ group ['mode ' ]][] = [
106+ 'where ' => [
107+ 'selector_matches ' => $ group ['selectors ' ],
108+ ],
109+ 'eagerness ' => $ group ['eagerness ' ],
110+ ];
84111 }
85112 }
86113
0 commit comments