1/* 2* Page Cache Ultimate, Page Cache standard and Speed pack are powered by Jpresta (jpresta . com) 3* 4* @author Jpresta 5* @copyright Jpresta 6* @license See the license of this module in file LICENSE.txt, thank you. 7*/ 8 9function jprestaPcGetParameterValue(e) { 10 let t = "[\\?&]" + e + "=([^&#]*)"; 11 let n = new RegExp(t); 12 let r = n.exec(window.location.href); 13 if (r == null) return ""; 14 else return r[1] 15} 16 17function jprestaPcSplitUri(uri) { 18 let splitRegExp = new RegExp('^' + '(?:' + '([^:/?#.]+)' + ':)?' + '(?://' + '(?:([^/?#]*)@)?' + '([\\w\\d\\-\\u0100-\\uffff.%]*)' + '(?:(:[0-9]+))?' + ')?' + '([^?#]+)?' + '(?:(\\?[^#]*))?' + '(?:(#.*))?' + '$'); 19 let split = uri.match(splitRegExp); 20 for (let i = 1; i < 8; i++) { 21 if (typeof split[i] === 'undefined') { 22 split[i] = ''; 23 } 24 } 25 return { 26 'scheme': split[1], 27 'user_info': split[2], 28 'domain': split[3], 29 'port': split[4], 30 'path': split[5], 31 'query_data': split[6], 32 'fragment': split[7] 33 } 34} 35 36function jprestaPcSetCookie(cname, cvalue, ttl_minutes, path) { 37 let d = new Date(); 38 d.setTime(d.getTime() + (ttl_minutes*60*1000)); 39 let expires = "expires="+ d.toUTCString(); 40 document.cookie = cname + "=" + cvalue + ";" + expires + ";path=" + path; 41} 42 43function jprestaPcGetCookie(cname, defaultValue) { 44 if (defaultValue === undefined) { 45 defaultValue = null; 46 } 47 let name = cname + "="; 48 let decodedCookie = decodeURIComponent(document.cookie); 49 let ca = decodedCookie.split(';'); 50 for(let i = 0; i <ca.length; i++) { 51 let c = ca[i]; 52 while (c.charAt(0) == ' ') { 53 c = c.substring(1); 54 } 55 if (c.indexOf(name) == 0) { 56 return c.substring(name.length, c.length); 57 } 58 } 59 return defaultValue; 60} 61 62function jprestaGetTTFB() { 63 let timing = null; 64 if (typeof window.performance.timing !== 'undefined') { 65 timing = window.performance.timing; 66 } 67 else { 68 timing = window.performance.getEntriesByType('navigation')[0]; 69 } 70 return timing.responseStart - timing.requestStart; 71} 72 73function jprestaGetCacheType() {
74 let cache_type = -2; 75 let timing = null; 76 if (typeof window.performance.getEntriesByType == 'function') { 77 timing = window.performance.getEntriesByType('navigation')[0]; 78 } 79 if (!timing && typeof window.performance.timing !== 'undefined') { 80 timing = window.performance.timing; 81 } 82 if (timing.transferSize === 0) { 83 cache_type = 2; 84 } 85 else if (typeof timing.serverTiming != 'undefined') { 86 for (let i = 0; i < timing.serverTiming.length; i++) { 87 if (timing.serverTiming[i].name === 'jpresta_cache') { 88 cache_type = parseInt(timing.serverTiming[i].description); 89 break; 90 } 91 } 92 } 93 return cache_type; 94} 95 96/** 97 * Forward dbgpagecache parameter 98 */ 99function jprestaPcForwardDbgpagecacheParameter() { 100 try { 101 if (typeof baseDir === 'undefined') { 102 baseDir = prestashop.urls.base_url; 103 } 104 if (window.location.href.indexOf("dbgpagecache=") > 0) { 105 $("a:not(.pagecache)").each(function () { 106 let e = $(this).attr("href"); 107 let t = this.search; 108 let n = "dbgpagecache=" + jprestaPcGetParameterValue("dbgpagecache"); 109 let r = baseDir.replace("https", "http"); 110 if (typeof e !== "undefined" && e.substr(0, 1) !== "#" && (e.replace("https", "http").substr(0, r.length) === r || e.indexOf('://') === -1) && e.indexOf('javascript:') === -1 && e.indexOf('mailto:') === -1 && e.indexOf('tel:') === -1 && e.indexOf('callto:') === -1) { 111 if (t.length === 0) this.search = n; 112 else this.search += "&" + n 113 } 114 }) 115 console.log("Page Cache Ultimate - Parameter dbgpagecache has been added to all links"); 116 } 117 } catch (e) { 118 console.warn("Page Cache Ultimate - Cannot forward dbgpagecache parameter on all links: " + e.message, e) 119 } 120} 121 122jprestaPcStartsWith = function(str, search) { 123 return typeof str === 'string' && str.substr(0, search.length) === search; 124}; 125 126jprestaPcProcessDynamicModules = function(dyndatas) { 127 for (let key in dyndatas) { 128 if (key === 'js') { 129 // Keep spaces arround 'key', some Prestashop removes [key] otherwise (?!) 130 $('body').append(dyndatas[ key ]); 131 } 132 else if (jprestaPcStartsWith(key, 'dyn')) { 133 // Keep spaces arround 'key', some Prestashop removes [key] otherwise (?!) 134 try { 135 $('#'+key).replaceWith(dyndatas[ key ]); 136 } 137 catch (error) { 138 console.error('Page Cache Ultimate - A javasript error occured during the "eval" of the refreshed content ' + key + ': ' + error); 139 } 140 } 141 } 142 if (typeof pcRunDynamicModulesJs == 'function') { 143 pcRunDynamicModulesJs(); 144 } 145 console.timeEnd('Page Cache Ultimate - Dynamic modules have been refreshed in ') 146 if (typeof prestashop != 'undefined') { 147 prestashop.emit('jprestaDynamicContentLoaded'); 148 } 149}; 150 151/** 152 * Refresh dynamic modules 153 */ 154function jprestaPcRefreshDynamicModules(cacheSource) { 155 try { 156 // Not needed for bots 157 if (!/bot|googlebot|crawler|spider|robot|crawling|gtmetrix|chrome-lighthouse/i.test(navigator.userAgent)) { 158 console.time('Page Cache Ultimate - Dynamic modules have been refreshed in ') 159 let dynDatas = {}; 160 dynDatas['cache_source'] = cacheSource; 161 dynDatas['ttfb'] = jprestaGetTTFB(); 162 $('.dynhook').each(function(index, domhook){ 163 dynDatas['hk_' + index] = $(this).attr('id') + '|' + $(this).data('hooktype') + '|' + $(this).data('module') + '|' + $(this).data('hook') + '|' + $(this).data('hookargs'); 164 }); 165 let urlparts = jprestaPcSplitUri(document.URL); 166 let url = urlparts['scheme'] + '://' + urlparts['domain'] + urlparts['port'] + urlparts['path'] + urlparts['query_data']; 167 let indexEnd = url.indexOf('?'); 168 if (indexEnd >= 0 && indexEnd < url.length) { 169 url += '&ajax=true&page_cache_dynamics_mods=1'; 170 } 171 else { 172 url += '?ajax=true&page_cache_dynamics_mods=1'; 173 } 174 $.ajax({url: url, type: 'POST', data: dynDatas, dataType: 'json', cache: false, 175 success: jprestaPcProcessDynamicModules, 176 error: function(jqXHR, textStatus, errorThrown) { 177 try { 178 let indexStart = jqXHR.responseText.indexOf('{'); 179 let responseFixed = jqXHR.responseText.substring(indexStart, jqXHR.responseText.length); 180 dyndatas = $.parseJSON(responseFixed); 181 if (dyndatas != null) { 182 jprestaPcProcessDynamicModules(dyndatas); 183 return; 184 } 185 } 186 catch(err) { 187 console.error("Page Cache Ultimate - Cannot parse data of error=" + err, err); 188 } 189 console.error("Page Cache Ultimate - Cannot display dynamic modules: error=" + textStatus + " exception=" + errorThrown); 190 console.log("Page Cache Ultimate - Dynamic module URL: " + url); 191 }}); 192 } 193 } catch (e) { 194 console.error("Page Cache Ultimate - Cannot display dynamic modules: " + e.message, e); 195 } 196} 197 198/** 199 * Stats TTFB (when using back/forward cache) 200 */ 201function jprestaPcSendStats(cacheSource, ttfb) { 202 try { 203 // Not needed for bots 204 if (!/bot|googlebot|crawler|spider|robot|crawling|gtmetrix|chrome-lighthouse/i.test(navigator.userAgent)) { 205 console.time('Page Cache Ultimate - Sending stats in '); 206 let dynDatas = {};
207 dynDatas['cache_source'] = cacheSource; 208 dynDatas['ttfb'] = ttfb; 209 let urlparts = jprestaPcSplitUri(document.URL); 210 let url = urlparts['scheme'] + '://' + urlparts['domain'] + urlparts['port'] + urlparts['path'] + urlparts['query_data']; 211 let indexEnd = url.indexOf('?'); 212 if (indexEnd >= 0 && indexEnd < url.length) { 213 url += '&ajax=true&page_cache_dynamics_mods=1&stats'; 214 } 215 else { 216 url += '?ajax=true&page_cache_dynamics_mods=1&stats'; 217 } 218 $.ajax({url: url, type: 'POST', data: dynDatas, dataType: 'json', cache: false, 219 error: function(jqXHR, textStatus, errorThrown) { 220 console.error("Page Cache Ultimate - Cannot display dynamic modules: error=" + textStatus + " exception=" + errorThrown); 221 console.log("Page Cache Ultimate - Stats URL: " + url); 222 }, 223 complete: function() { 224 console.timeEnd('Page Cache Ultimate - Sending stats in '); 225 } 226 }); 227 } 228 } catch (e) { 229 console.error("Page Cache Ultimate - Cannot stats: " + e.message, e); 230 } 231} 232 233$(document).ready(function () { 234 // Refresh the cart when back/forward cache is used 235 window.addEventListener('pageshow', function(event) { 236 if (event.persisted) { 237 jprestaPcSendStats(4, 0); 238 // Refresh the cart 239 if (typeof prestashop !== 'undefined') { 240 // >= PS 1.7 241 console.log('Page Cache Ultimate - Back/forward cache is used, refreshing the cart'); 242 prestashop.emit('updateCart', {reason: {linkAction: 'refresh'}, resp: {errors: []}}); 243 } 244 else if(typeof ajaxCart !== 'undefined') { 245 // < PS 1.7 246 console.log('Page Cache Ultimate - Back/forward cache is used, refreshing the cart (PS 1.6)'); 247 $.ajax({ 248 type: 'POST', 249 headers: {"cache-control": "no-cache"}, 250 url: baseUri + '?rand=' + new Date().getTime(), 251 async: true, 252 cache: false, 253 dataType: "json", 254 data: 'controller=cart&ajax=true&token=' + static_token, 255 success: function (jsonData) { 256 ajaxCart.updateCart(jsonData); 257 } 258 }); 259 } 260 else { 261 console.log('Page Cache Ultimate - Back/forward cache is used'); 262 } 263 } 264 }); 265 266 // jpresta_cache_source => -1=cannot be cached; 0=no cache; 1=server cache; 2=browser cache, 3=static cache, 4=back/forward cache 267 let cacheSource = jprestaGetCacheType(); 268 switch (cacheSource) { 269 case -2: 270 jprestaPcRefreshDynamicModules(cacheSource); 271 console.log('Page Cache Ultimate - Cannot determine the cache type :-('); 272 break; 273 case -1: 274 console.log('Page Cache Ultimate - Cannot be cached'); 275 break; 276 case 0: 277 jprestaPcRefreshDynamicModules(cacheSource); 278 console.log('Page Cache Ultimate - No cache was used'); 279 break; 280 case 1: 281 jprestaPcRefreshDynamicModules(cacheSource); 282 console.log('Page Cache Ultimate - Server cache was used'); 283 break; 284 case 2: 285 jprestaPcRefreshDynamicModules(cacheSource); 286 console.log('Page Cache Ultimate - Browser cache was used'); 287 break; 288 case 3: 289 jprestaPcRefreshDynamicModules(cacheSource); 290 console.log('Page Cache Ultimate - Static cache was used'); 291 break; 292 case 4: 293 // Handled by 'pageshow' event 294 break; 295 } 296 $('.pctype' + cacheSource).show(); 297 jprestaPcForwardDbgpagecacheParameter(); 298});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.