Skip to content

Commit 4ed286a

Browse files
committed
Properly implement section invalidation from Luma customerData JS
1 parent 862b0bb commit 4ed286a

3 files changed

Lines changed: 46 additions & 16 deletions

File tree

view/frontend/layout/loki_base.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
</container>
2929

3030
<container name="loki.store">
31-
<block name="loki.store.local-storage" template="Loki_Base::script/store/localstorage-store.phtml"/>
31+
<block name="loki.store.local-storage" template="Loki_Base::script/store/localstorage-store.phtml">
32+
<arguments>
33+
<argument name="section_config" xsi:type="object">Loki\Base\ViewModel\SectionConfig</argument>
34+
</arguments>
35+
</block>
36+
3237
<block name="loki.store.messages" template="Loki_Base::script/store/message-store.phtml"/>
3338
</container>
3439

view/frontend/templates/script/store/localstorage-store.phtml

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
declare(strict_types=1);
33

44

5+
use Loki\Base\ViewModel\SectionConfig;
56
use Magento\Framework\View\Element\Template;
67

78
/** @var Template $block */
9+
/** @var SectionConfig $sectionConfig */
810

9-
// @todo: Invalidate sections based on data_id
11+
$sectionConfig = $block->getSectionConfig();
1012
?>
1113
<script>
1214
document.addEventListener('alpine:init', () => {
1315
Alpine.store('LokiLocalStorage', {
1416
key: 'mage-cache-storage',
17+
sectionLifetime: <?= (int)$sectionConfig->getSectionDataLifeTime() * 60 ?>,
1518
data: {},
1619
init() {
1720
const storedData = localStorage.getItem(this.key);
@@ -22,25 +25,41 @@ use Magento\Framework\View\Element\Template;
2225
}
2326

2427
let changed = false;
28+
const cookieSections = LokiCookies.get('section_data_ids') || {};
29+
2530
Object.entries(this.data).forEach(([key, value]) => {
26-
if (value.data_id && value.data_id < Date. now()) {
31+
const isSectionExpired = !value.data_id || parseInt(value.data_id) + this.sectionLifetime < this.getCurrentTimestamp();
32+
const isCookieSectionExpired = cookieSections[key] && cookieSections[key] + this.sectionLifetime < this.getCurrentTimestamp();
33+
34+
if (isSectionExpired || isCookieSectionExpired) {
2735
delete this.data[key];
2836
changed = true;
2937
}
3038
})
3139

32-
3340
if (changed) {
3441
localStorage.setItem(this.key, JSON.stringify(this.data));
3542
}
3643
},
44+
getNewSectionLifetime() {
45+
return this.getCurrentTimestamp() + this.sectionLifetime;
46+
},
47+
getCurrentTimestamp() {
48+
return Math.floor(Date.now() / 1000);
49+
},
3750
save() {
3851
localStorage.setItem(this.key, JSON.stringify(this.data));
3952
},
40-
refresh(sections) {
41-
let url = LOKI_BASE_URL + '/customer/section/load';
53+
refresh(sections, forceNewSectionTimestamp) {
54+
let url = new URL(LOKI_BASE_URL + '/customer/section/load');
4255
if (sections) {
43-
url += '?sections=' + sections;
56+
url.searchParams.append('sections', sections);
57+
}
58+
59+
forceNewSectionTimestamp = !!forceNewSectionTimestamp;
60+
if (forceNewSectionTimestamp) {
61+
url.searchParams.append('force_new_section_timestamp', 'true');
62+
url.searchParams.append('_', Math.floor(Date.now() / 1000));
4463
}
4564

4665
fetch(url, {
@@ -54,7 +73,6 @@ use Magento\Framework\View\Element\Template;
5473
.then(newData => {
5574
if (typeof newData === 'object') {
5675
this.data = Object.assign(this.data, newData);
57-
console.log('Fetching new data', newData);
5876
this.save();
5977
}
6078
})
@@ -68,7 +86,8 @@ use Magento\Framework\View\Element\Template;
6886
return this.data;
6987
}
7088

71-
if (!this.data.hasOwnProperty(key) || !this.data[key] ) {
89+
if (!this.data[key] ) {
90+
console.log('Refreshing data:', key, this.data[key]);
7291
this.refresh(key);
7392
}
7493

@@ -79,8 +98,10 @@ use Magento\Framework\View\Element\Template;
7998
this.save();
8099
},
81100
remove(key) {
82-
delete this.data[key];
83-
this.save();
101+
if (key !== undefined) {
102+
delete this.data[key];
103+
this.save();
104+
}
84105
},
85106
reset() {
86107
this.data = {};

view/frontend/templates/script/store/message-store.phtml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use Magento\Framework\View\Element\Template;
1414
const messageSection = Alpine.store('LokiLocalStorage').get('messages');
1515
if (messageSection && messageSection.messages) {
1616
this.messages = [...this.messages, ...messageSection.messages];
17-
this.getStore().set('messages', {messages: []});
17+
this.reset();
1818
}
1919
});
2020
},
@@ -51,16 +51,20 @@ use Magento\Framework\View\Element\Template;
5151
this.addMessage('notice', text);
5252
},
5353
reset() {
54-
this.getStore().remove('messages');
54+
this.getStore().set('messages', {
55+
data_id: this.getStore().getNewSectionLifetime(),
56+
messages: []
57+
});
5558
},
5659
getMessagesFromStore() {
5760
const messagesSection = this.getStore().get('messages');
5861
return messagesSection && messagesSection.messages ? messagesSection.messages : [];
5962
},
6063
setMessageInStore(messages) {
61-
const messagesSection = this.getStore().get('messages');
62-
messagesSection.messages = messages;
63-
this.getStore().set('messages', messagesSection);
64+
this.getStore().set('messages', {
65+
data_id: this.getStore().getNewSectionLifetime(),
66+
messages
67+
});
6468
},
6569
getStore() {
6670
return Alpine.store('LokiLocalStorage');

0 commit comments

Comments
 (0)