Skip to content

Commit 657500d

Browse files
committed
ACP2E-2025: Refix ACP2E-1445 for multiple store views in the frontend.
- Fixed the issue.
1 parent b0c72f3 commit 657500d

File tree

8 files changed

+186
-15
lines changed

8 files changed

+186
-15
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\ViewModel\Customer;
9+
10+
use Magento\Customer\Model\Context;
11+
use Magento\Framework\App\Http\Context as HttpContext;
12+
use Magento\Framework\Serialize\Serializer\Json as Json;
13+
use Magento\Framework\View\Element\Block\ArgumentInterface;
14+
15+
/**
16+
* Customer's data view model
17+
*/
18+
class Data implements ArgumentInterface
19+
{
20+
/**
21+
* @var Json
22+
*/
23+
private $jsonEncoder;
24+
25+
/**
26+
*
27+
* @var HttpContext
28+
*/
29+
private $httpContext;
30+
31+
/**
32+
* @param HttpContext $httpContext
33+
* @param Json $jsonEncoder
34+
*/
35+
public function __construct(
36+
HttpContext $httpContext,
37+
Json $jsonEncoder
38+
) {
39+
$this->httpContext = $httpContext;
40+
$this->jsonEncoder = $jsonEncoder;
41+
}
42+
43+
/**
44+
* Check is user login
45+
*
46+
* @return bool
47+
*/
48+
public function isLoggedIn()
49+
{
50+
return $this->httpContext->getValue(Context::CONTEXT_AUTH);
51+
}
52+
53+
/**
54+
* Encode the mixed $valueToEncode into the JSON format
55+
*
56+
* @param mixed $valueToEncode
57+
* @return string
58+
*/
59+
public function jsonEncode($valueToEncode)
60+
{
61+
return $this->jsonEncoder->serialize($valueToEncode);
62+
}
63+
}

app/code/Magento/Customer/view/frontend/layout/default.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@
4848
</arguments>
4949
</block>
5050
<block name="customer.customer.data" class="Magento\Customer\Block\CustomerData"
51-
template="Magento_Customer::js/customer-data.phtml"/>
51+
template="Magento_Customer::js/customer-data.phtml">
52+
<arguments>
53+
<argument name="view_model" xsi:type="object">Magento\Customer\ViewModel\Customer\Data</argument>
54+
</arguments>
55+
</block>
5256
<block name="customer.data.invalidation.rules" class="Magento\Customer\Block\CustomerScopeData"
5357
template="Magento_Customer::js/customer-data/invalidation-rules.phtml"/>
5458
</referenceContainer>

app/code/Magento/Customer/view/frontend/templates/js/customer-data.phtml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
use Magento\Customer\ViewModel\Customer\Data;
7+
use Magento\Framework\App\ObjectManager;
68

79
/** @var \Magento\Customer\Block\CustomerData $block */
8-
/** @var \Magento\Framework\Json\Helper\Data $jsonHelper */
9-
$expirableSectionNames = $block->getExpirableSectionNames();
1010

1111
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundHelper
12-
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundThis
13-
$jsonHelper = $this->helper(\Magento\Framework\Json\Helper\Data::class);
12+
/** @var Data $viewModel */
13+
$viewModel = $block->getViewModel() ?? ObjectManager::getInstance()->get(Data::class);
14+
$customerDataUrl = $block->getCustomerDataUrl('customer/account/updateSession');
15+
$expirableSectionNames = $block->getExpirableSectionNames();
1416
?>
1517
<script type="text/x-magento-init">
1618
{
1719
"*": {
1820
"Magento_Customer/js/customer-data": {
1921
"sectionLoadUrl": "<?= $block->escapeJs($block->getCustomerDataUrl('customer/section/load')) ?>",
2022
"expirableSectionLifetime": <?= (int)$block->getExpirableSectionLifetime() ?>,
21-
"expirableSectionNames": <?= /* @noEscape */ $jsonHelper->jsonEncode(
22-
$block->getExpirableSectionNames()
23-
) ?>,
23+
"expirableSectionNames": <?= /* @noEscape */ $viewModel->jsonEncode($expirableSectionNames) ?>,
2424
"cookieLifeTime": "<?= $block->escapeJs($block->getCookieLifeTime()) ?>",
25-
"updateSessionUrl": "<?= $block->escapeJs(
26-
$block->getCustomerDataUrl('customer/account/updateSession')
27-
) ?>"
25+
"updateSessionUrl": "<?= $block->escapeJs($customerDataUrl) ?>",
26+
"isLoggedIn": "<?= /* @noEscape */ $viewModel->isLoggedIn() ?>"
2827
}
2928
}
3029
}

app/code/Magento/Customer/view/frontend/web/js/customer-data.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,20 @@ define([
4747
* Invalidate Cache By Close Cookie Session
4848
*/
4949
invalidateCacheByCloseCookieSession = function () {
50+
var isLoggedIn = parseInt(options.isLoggedIn, 10) || 0;
51+
5052
if (!$.cookieStorage.isSet('mage-cache-sessid')) {
5153
storage.removeAll();
5254
}
5355

56+
if (!$.localStorage.isSet('mage-customer-login')) {
57+
$.localStorage.set('mage-customer-login', isLoggedIn);
58+
}
59+
if ($.localStorage.get('mage-customer-login') !== isLoggedIn) {
60+
$.localStorage.set('mage-customer-login', isLoggedIn);
61+
storage.removeAll();
62+
}
63+
5464
$.cookieStorage.set('mage-cache-sessid', true);
5565
};
5666

lib/internal/Magento/Framework/App/PageCache/Identifier.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* Page unique identifier
1313
*/
14-
class Identifier
14+
class Identifier implements IdentifierInterface
1515
{
1616
/**
1717
* @var \Magento\Framework\App\Request\Http
@@ -48,7 +48,7 @@ public function __construct(
4848
*
4949
* @return string
5050
*/
51-
public function getValue()
51+
public function getValue(): string
5252
{
5353
$data = [
5454
$this->request->isSecure(),
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\App\PageCache;
7+
8+
use Magento\Framework\App\Http\Context;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Framework\App\Request\Http;
11+
use Magento\Framework\Serialize\Serializer\Json;
12+
13+
/**
14+
* Page unique identifier
15+
*/
16+
class IdentifierForSave implements IdentifierInterface
17+
{
18+
/**
19+
* @var Http
20+
*/
21+
protected $request;
22+
23+
/**
24+
* @var Context
25+
*/
26+
protected $context;
27+
28+
/**
29+
* @var Json
30+
*/
31+
private $serializer;
32+
33+
/**
34+
* @param Http $request
35+
* @param Context $context
36+
* @param Json|null $serializer
37+
*/
38+
public function __construct(
39+
Http $request,
40+
Context $context,
41+
Json $serializer = null
42+
) {
43+
$this->request = $request;
44+
$this->context = $context;
45+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
46+
}
47+
48+
/**
49+
* Return unique page identifier
50+
*
51+
* @return string
52+
*/
53+
public function getValue(): string
54+
{
55+
$data = [
56+
$this->request->isSecure(),
57+
$this->request->getUriString(),
58+
$this->context->getVaryString()
59+
];
60+
61+
return sha1($this->serializer->serialize($data));
62+
}
63+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
namespace Magento\Framework\App\PageCache;
9+
10+
/**
11+
* @api
12+
* @since 100.0.2
13+
*/
14+
interface IdentifierInterface
15+
{
16+
/**
17+
* Return unique page identifier
18+
*
19+
* @return string
20+
*/
21+
public function getValue(): string;
22+
}

lib/internal/Magento/Framework/App/PageCache/Kernel.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ class Kernel
6363
*/
6464
private $state;
6565

66+
/**
67+
* @var \Magento\Framework\App\PageCache\IdentifierForSave
68+
*/
69+
private $identifierForSave;
70+
6671
/**
6772
* @param Cache $cache
6873
* @param Identifier $identifier
@@ -73,6 +78,7 @@ class Kernel
7378
* @param \Magento\Framework\Serialize\SerializerInterface|null $serializer
7479
* @param AppState|null $state
7580
* @param \Magento\PageCache\Model\Cache\Type|null $fullPageCache
81+
* @param \Magento\Framework\App\PageCache\IdentifierForSave $identifierForSave
7682
*/
7783
public function __construct(
7884
\Magento\Framework\App\PageCache\Cache $cache,
@@ -83,7 +89,8 @@ public function __construct(
8389
\Magento\Framework\App\Response\HttpFactory $httpFactory = null,
8490
\Magento\Framework\Serialize\SerializerInterface $serializer = null,
8591
AppState $state = null,
86-
\Magento\PageCache\Model\Cache\Type $fullPageCache = null
92+
\Magento\PageCache\Model\Cache\Type $fullPageCache = null,
93+
\Magento\Framework\App\PageCache\IdentifierForSave $identifierForSave = null,
8794
) {
8895
$this->cache = $cache;
8996
$this->identifier = $identifier;
@@ -102,6 +109,9 @@ public function __construct(
102109
$this->fullPageCache = $fullPageCache ?? ObjectManager::getInstance()->get(
103110
\Magento\PageCache\Model\Cache\Type::class
104111
);
112+
$this->identifierForSave = $identifierForSave ?? ObjectManager::getInstance()->get(
113+
\Magento\Framework\App\PageCache\IdentifierForSave::class
114+
);
105115
}
106116

107117
/**
@@ -158,7 +168,7 @@ public function process(\Magento\Framework\App\Response\Http $response)
158168

159169
$this->fullPageCache->save(
160170
$this->serializer->serialize($this->getPreparedData($response)),
161-
$this->identifier->getValue(),
171+
$this->identifierForSave->getValue(),
162172
$tags,
163173
$maxAge
164174
);

0 commit comments

Comments
 (0)