Skip to content

Commit 573d366

Browse files
[5.x] Update nocache map on response (#11650)
Co-authored-by: Jason Varga <jason@pixelfear.com>
1 parent d999a9c commit 573d366

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/StaticCaching/Cachers/FileCacher.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,18 @@ public function getNocacheJs(): string
207207

208208
$default = <<<EOT
209209
(function() {
210-
var els = document.getElementsByClassName('nocache');
211-
var map = {};
212-
for (var i = 0; i < els.length; i++) {
213-
var section = els[i].getAttribute('data-nocache');
214-
map[section] = els[i];
210+
function createMap() {
211+
var map = {};
212+
var els = document.getElementsByClassName('nocache');
213+
for (var i = 0; i < els.length; i++) {
214+
var section = els[i].getAttribute('data-nocache');
215+
map[section] = els[i];
216+
}
217+
return map;
215218
}
216219
220+
var map = createMap();
221+
217222
fetch('/!/nocache', {
218223
method: 'POST',
219224
headers: { 'Content-Type': 'application/json' },
@@ -224,6 +229,8 @@ public function getNocacheJs(): string
224229
})
225230
.then((response) => response.json())
226231
.then((data) => {
232+
map = createMap(); // Recreate map in case the DOM changed.
233+
227234
const regions = data.regions;
228235
for (var key in regions) {
229236
if (map[key]) map[key].outerHTML = regions[key];

0 commit comments

Comments
 (0)