Skip to content

Commit 716c9b5

Browse files
authored
Merge pull request #1794 from algolia/fix/MAGE-1378-varnish-cache
MAGE-1378 Fix handling of Varnish X-Magento-Vary cookie
2 parents 8483eb1 + bbb48ae commit 716c9b5

File tree

6 files changed

+21
-11
lines changed

6 files changed

+21
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Fixed WSOD error on invalid creds when using manual SKU indexer (also included in 3.15.1).
2323
- Fixed Recommend model validation when configuring through the Magento admin
2424
- Fixed edge case with null queries - thank you @PromInc
25+
- Removed conditional behavior on setting the vary string from the plugin on `\Magento\Framework\App\Http\Context` so that a consistent `X-Magento-Vary` cookie is sent across all requests
2526

2627
### Updates
2728
- `beforecontent.html` is no longer used and has been deprecated. If you're overriding or referencing this file, please update your layout and customizations accordingly.
@@ -42,6 +43,7 @@
4243
- Added support for PHPUnit 10
4344
- Added support for Magento 2.4.8 on PHP 8.4 - Special shout out to @jajajaime for his help here
4445
- Refactored code to PHP 8.2 baseline standard
46+
- The InstantSearch "replace categories" feature must now be explicitly enabled on new instances aligning with our documentation: https://www.algolia.com/doc/integration/magento-2/guides/category-pages/#enable-category-pages
4547

4648
### Breaking Changes
4749
- `ProductHelper::setSettings()` is now taking `IndexOptions` objects as two first parameters instead of index names (strings).

Plugin/RenderingCacheContextPlugin.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Plugin;
44

55
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Algolia\AlgoliaSearch\Helper\Configuration\InstantSearchHelper;
67
use Magento\Framework\App\Http\Context as HttpContext;
78
use Magento\Framework\App\Request\Http;
89
use Magento\Framework\Exception\NoSuchEntityException;
@@ -25,7 +26,8 @@ class RenderingCacheContextPlugin
2526
public const CATEGORY_ROUTE = 'catalog/category/view';
2627

2728
public function __construct(
28-
protected ConfigHelper $configHelper,
29+
protected ConfigHelper $baseConfig,
30+
protected InstantSearchHelper $isConfig,
2931
protected StoreManagerInterface $storeManager,
3032
protected Http $request,
3133
protected UrlFinderInterface $urlFinder
@@ -50,7 +52,7 @@ public function afterGetData(HttpContext $subject, array $data): array {
5052
return $data;
5153
}
5254

53-
$context = $this->configHelper->preventBackendRendering() ?
55+
$context = $this->baseConfig->preventBackendRendering() ?
5456
self::RENDERING_WITHOUT_BACKEND :
5557
self::RENDERING_WITH_BACKEND;
5658

@@ -89,6 +91,7 @@ protected function isCategoryRoute(string $path): bool {
8991
*
9092
* @param int $storeId
9193
* @return bool
94+
* @deprecated This method will be removed in a future version
9295
*/
9396
protected function isCategoryPage(int $storeId): bool
9497
{
@@ -104,6 +107,6 @@ protected function isCategoryPage(int $storeId): bool
104107
protected function shouldApplyCacheContext(): bool
105108
{
106109
$storeId = $this->storeManager->getStore()->getId();
107-
return $this->isCategoryPage($storeId) && $this->configHelper->replaceCategories($storeId);
110+
return $this->isConfig->shouldReplaceCategories($storeId);
108111
}
109112
}

Setup/Patch/Schema/ConfigPatch.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class ConfigPatch implements SchemaPatchInterface
5151
'algoliasearch_instant/instant/instant_selector' => '.columns',
5252
'algoliasearch_instant/instant/number_product_results' => '9',
5353
'algoliasearch_instant/instant/max_values_per_facet' => '10',
54-
'algoliasearch_instant/instant/replace_categories' => '1',
5554
'algoliasearch_instant/instant/show_suggestions_on_no_result_page' => '1',
5655
'algoliasearch_instant/instant/add_to_cart_enable' => '1',
5756
'algoliasearch_instant/instant/infinite_scroll_enable' => '0',

Test/Unit/Plugin/RenderingCacheContextPluginTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,34 @@
33
namespace Algolia\AlgoliaSearch\Test\Unit\Plugin;
44

55
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Algolia\AlgoliaSearch\Helper\Configuration\InstantSearchHelper;
67
use Algolia\AlgoliaSearch\Plugin\RenderingCacheContextPlugin;
78
use Magento\Framework\App\Http\Context as HttpContext;
89
use Magento\Framework\App\Request\Http;
910
use Magento\Store\Api\Data\StoreInterface;
1011
use Magento\Store\Model\StoreManagerInterface;
1112
use Magento\UrlRewrite\Model\UrlFinderInterface;
1213
use PHPUnit\Framework\TestCase;
13-
1414
class RenderingCacheContextPluginTest extends TestCase
1515
{
1616
protected ?RenderingCacheContextPlugin $plugin;
1717
protected ?ConfigHelper $configHelper;
18+
19+
protected ?InstantSearchHelper $instantSearchHelper;
1820
protected ?StoreManagerInterface $storeManager;
1921
protected ?Http $request;
2022
protected ?UrlFinderInterface $urlFinder;
21-
2223
protected function setUp(): void
2324
{
2425
$this->configHelper = $this->createMock(ConfigHelper::class);
26+
$this->instantSearchHelper = $this->createMock(InstantSearchHelper::class);
2527
$this->storeManager = $this->createMock(StoreManagerInterface::class);
2628
$this->request = $this->createMock(Http::class);
2729
$this->urlFinder = $this->createMock(UrlFinderInterface::class);
2830

2931
$this->plugin = new RenderingCacheContextPluginTestable(
3032
$this->configHelper,
33+
$this->instantSearchHelper,
3134
$this->storeManager,
3235
$this->request,
3336
$this->urlFinder
@@ -47,7 +50,7 @@ public function testAfterGetDataAddsRenderingContextNoBackendRender(): void
4750
$this->storeManager->method('getStore')->willReturn($this->getStoreMock());
4851

4952
$this->request->method('getControllerName')->willReturn('category');
50-
$this->configHelper->method('replaceCategories')->willReturn(true);
53+
$this->instantSearchHelper->method('shouldReplaceCategories')->willReturn(true);
5154

5255
$result = $this->plugin->afterGetData(
5356
$this->createMock(HttpContext::class),
@@ -66,7 +69,7 @@ public function testAfterGetDataAddsRenderingContextWithBackendRender(): void
6669
$this->storeManager->method('getStore')->willReturn($this->getStoreMock());
6770

6871
$this->request->method('getControllerName')->willReturn('category');
69-
$this->configHelper->method('replaceCategories')->willReturn(true);
72+
$this->instantSearchHelper->method('shouldReplaceCategories')->willReturn(true);
7073

7174
$result = $this->plugin->afterGetData(
7275
$this->createMock(HttpContext::class),
@@ -86,7 +89,7 @@ public function testAfterGetDataDoesNotModifyDataIfNotApplicable(): void
8689

8790
$this->request->method('getControllerName')->willReturn('product');
8891
$this->request->method('getRequestUri')->willReturn('some-product.html');
89-
$this->configHelper->method('replaceCategories')->willReturn(false);
92+
$this->instantSearchHelper->method('shouldReplaceCategories')->willReturn(false);
9093

9194
$data = ['existing_key' => 'existing_value'];
9295
$result = $this->plugin->afterGetData($subject, $data);
@@ -121,7 +124,7 @@ public function testShouldApplyCacheContext(): void
121124
$this->storeManager->method('getStore')->willReturn($this->getStoreMock());
122125

123126
$this->request->method('getControllerName')->willReturn('category');
124-
$this->configHelper->method('replaceCategories')->willReturn(true);
127+
$this->instantSearchHelper->method('shouldReplaceCategories')->willReturn(true);
125128

126129
$this->assertTrue($this->plugin->shouldApplyCacheContext());
127130
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"license": ["MIT"],
66
"version": "3.16.0",
77
"require": {
8-
"php": "~8.1|~8.2|~8.3",
8+
"php": "~8.2|~8.3|~8.4",
99
"magento/framework": "~103.0",
1010
"algolia/algoliasearch-client-php": "4.18.3",
1111
"guzzlehttp/guzzle": "^6.3.3|^7.3.0",

etc/config.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
</redirects>
3030
</algoliasearch_autocomplete>
3131
<algoliasearch_instant>
32+
<instant>
33+
<replace_categories>0</replace_categories>
34+
</instant>
3235
<instant_facets>
3336
<facets><![CDATA[{"_1458145454535_587":{"attribute":"price","type":"slider","label":"Price","searchable":"2","create_rule":"2"},"_2541608784525_123":{"attribute":"categories","type":"conjunctive","label":"Categories","searchable":"2","create_rule":"2"},"_3211608784535_456":{"attribute":"color","type":"disjunctive","label":"Colors","searchable":"1","create_rule":"2"}}]]></facets>
3437
<max_values_per_facet>10</max_values_per_facet>

0 commit comments

Comments
 (0)