@prefetchru/prefetch 1.0.5 → 1.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/prefetch.min.js +2 -2
- package/package.json +1 -2
- package/prefetch.js +50 -14
package/dist/prefetch.min.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* prefetch.ru v1.0.
|
|
2
|
+
* prefetch.ru v1.0.7 - Мгновенная загрузка страниц
|
|
3
3
|
* © 2026 Сергей Макаров | MIT License
|
|
4
4
|
* https://prefetch.ru | https://github.com/prefetch-ru
|
|
5
5
|
*/
|
|
6
|
-
!function(){"use strict";var e=new Set,t=new WeakMap,n=0,r=0,i=null,o=!1,a=!1,c=null,u=null,s=!1,l=null,
|
|
6
|
+
!function(){"use strict";var e=new Set,t=new WeakMap,n=0,r=0,i=null,o=!1,a=!1,c=null,u=null,s=!1,l=null,f=null,d=!1,h=65,m=80,p=50,v=!1,g=!1,w=!1,x=!1,y="none",L=!1,b=!1,E=!1,T=!1;function S(){var e=document.body;if(e){u=void 0!==window.BX?"bitrix":void 0!==window.B24||void 0!==window.BX24?"bitrix24":document.querySelector(".t-records")||void 0!==window.Tilda?"tilda":null;var t=e.dataset;if(v="prefetchAllowQueryString"in t||"instantAllowQueryString"in t,g="prefetchAllowExternalLinks"in t||"instantAllowExternalLinks"in t,w="prefetchWhitelist"in t||"instantWhitelist"in t,t.prefetchNonce&&(f=t.prefetchNonce),!f&&t.instantNonce&&(f=t.instantNonce),!a&&("prefetchSpecrules"in t||"instantSpecrules"in t)&&HTMLScriptElement.supports&&HTMLScriptElement.supports("speculationrules")){var n=t.prefetchSpecrules||t.instantSpecrules;"prerender"===n?(y="prerender",x=!0):"no"!==n&&(y="prefetch",x=!0)}L="prefetchPrerenderAll"in t||"instantPrerenderAll"in t;var r=t.prefetchIntensity||t.instantIntensity;if("mousedown"===r)b=!0;else if("viewport"===r||"viewport-all"===r)("viewport-all"===r||o&&O())&&(E=!0);else if(r){var i=parseInt(r,10);!isNaN(i)&&i>=0&&(h=i)}o&&(m=Math.max(60,Math.min(h||0,150))),(T="prefetchObserveDom"in t)||"bitrix"!==u&&"tilda"!==u||(T=!0),"tilda"===u&&h<100&&(h=100);var c={capture:!0,passive:!0};if(document.addEventListener("touchstart",k,c),b?document.addEventListener("mousedown",M,c):document.addEventListener("mouseover",N,c),E)(window.requestIdleCallback||function(e){setTimeout(e,1)})(U,{timeout:1500});T&&function(){if(z)return;(z=new MutationObserver(function(e){e.some(function(e){return Array.from(e.addedNodes).some(function(e){return 1===e.nodeType&&("A"===e.tagName||e.querySelectorAll&&e.querySelectorAll("a").length)})})&&R&&(clearTimeout(H),H=setTimeout(B,100))})).observe(document.body,{childList:!0,subtree:!0})}()}}function O(){return!s&&("slow-2g"!==l&&"2g"!==l)}function A(e){return e?(e.nodeType&&1!==e.nodeType&&(e=e.parentElement),e&&"function"==typeof e.closest?e.closest("a"):null):null}function k(e){n=e.timeStamp||Date.now();var t=A(e.target);if(C(t)){r&&(clearTimeout(r),r=0),i&&(document.removeEventListener("touchmove",i,!0),document.removeEventListener("scroll",i,!0),i=null);var o=!1;i=function(){o=!0,r&&(clearTimeout(r),r=0),document.removeEventListener("touchmove",i,!0),document.removeEventListener("scroll",i,!0),i=null},document.addEventListener("touchmove",i,{capture:!0,passive:!0,once:!0}),document.addEventListener("scroll",i,{capture:!0,passive:!0,once:!0}),r=setTimeout(function(){i&&(document.removeEventListener("touchmove",i,!0),document.removeEventListener("scroll",i,!0),i=null),r=0,o||W(t.href,t)},m)}}function N(e){if(!(n&&e.timeStamp&&e.timeStamp-n<2500)){var r=A(e.target);if(C(r)&&!t.has(r)){r.addEventListener("mouseleave",P,{passive:!0,once:!0});var i=setTimeout(function(){W(r.href,r),t.delete(r)},h);t.set(r,i)}}}function P(e){var n=e.currentTarget;if(n){var r=t.get(n);r&&(clearTimeout(r),t.delete(n))}}function M(e){if(!("number"==typeof e.button&&2===e.button||n&&e.timeStamp&&e.timeStamp-n<2500)){var t=A(e.target);C(t)&&W(t.href,t)}}function C(t){if(!t)return!1;var n=t.getAttribute("href");if(null===n||""===n.trim())return!1;if(!t.href)return!1;if(t.target&&"_self"!==t.target)return!1;if(t.hasAttribute("download"))return!1;if("noPrefetch"in t.dataset||"prefetchNo"in t.dataset)return!1;if(w&&!("prefetch"in t.dataset)&&!("instant"in t.dataset))return!1;if("http:"!==t.protocol&&"https:"!==t.protocol)return!1;if("http:"===t.protocol&&"https:"===location.protocol)return!1;if(t.origin!==location.origin){if(!g&&!("prefetch"in t.dataset)&&!("instant"in t.dataset))return!1;if(!c)return!1}if(t.search&&!v&&!("prefetch"in t.dataset)&&!("instant"in t.dataset))return!1;if(t.hash&&t.pathname+t.search===location.pathname+location.search)return!1;if(I(t.href)===I(location.href))return!1;var r=I(t.href);return!e.has(r)&&(!!function(e){var t=e.href,n="",r="";try{var i=new URL(t,location.href);n=i.pathname||"",r=i.hash||""}catch(e){n="",r=""}if("bitrix"===u||"bitrix24"===u){if(-1!==t.indexOf("/bitrix/")||-1!==t.indexOf("sessid="))return!1;if(e.classList.contains("bx-ajax"))return!1}if("tilda"===u&&(-1!==r.indexOf("#popup:")||-1!==r.indexOf("#rec")))return!1;var o=/(^|\/)(add|delete|remove)(\/|$|\.)/i.test(n);return-1===t.indexOf("/login")&&-1===t.indexOf("/logout")&&-1===t.indexOf("/auth")&&-1===t.indexOf("/register")&&-1===t.indexOf("/cart")&&-1===t.indexOf("/basket")&&!o&&!/\.(pdf|doc|docx|xls|xlsx|zip|rar|exe)($|\?)/i.test(t)}(t)&&!!function(e){var t=e.href,n=e.className||"",r="";try{r=new URL(t,location.href).hostname}catch(e){r=""}return-1===n.indexOf("ym-")&&("mc.yandex.ru"!==r&&"metrika.yandex.ru"!==r&&(-1===n.indexOf("ga-")&&-1===n.indexOf("gtm-")&&("google-analytics.com"!==r&&!r.endsWith(".google-analytics.com")&&("googletagmanager.com"!==r&&!r.endsWith(".googletagmanager.com")&&(-1===n.indexOf("piwik")&&-1===n.indexOf("matomo")&&("matomo.org"!==r&&!r.endsWith(".matomo.org")&&"piwik.org"!==r&&!r.endsWith(".piwik.org")))))))}(t))}function I(e){try{var t=new URL(e,location.href);return t.origin+t.pathname+t.search}catch(t){return e}}function W(t,n){if(O()){var r=function(e){try{var t=new URL(e,location.href);return t.hash="",t.href}catch(t){return e}}(t),i=I(r);if(!e.has(i)){e.size>=p&&e.delete(e.values().next().value),e.add(i);var o=function(e){return x&&"none"!==y?"prerender"!==y?y:L||w||e&&e.dataset&&("prefetchPrerender"in e.dataset||"instantPrerender"in e.dataset)?"prerender":"prefetch":"none"}(n);if("none"!==o)return function(e,t){var n=document.head;if(!n)return;var r=document.createElement("script");r.type="speculationrules",f&&(r.nonce=f);var i={};i[t]=[{source:"list",urls:[e]}],r.textContent=JSON.stringify(i),n.appendChild(r)}(r,o),void(a||!d?D(r,i):q(r,i));a||!d?D(r,i):q(r,i)}}}function q(t,n){var r=document.head;if(r){var i=document.createElement("link");i.rel="prefetch",i.href=t,i.as="document";try{i.fetchPriority="low"}catch(e){}i.onerror=function(){e.delete(n)},r.appendChild(i)}}function D(t,n){if("function"==typeof fetch){var r=null,i=0;"undefined"!=typeof AbortController&&(r=new AbortController,i=setTimeout(function(){try{r.abort()}catch(e){}},5e3));var o={method:"GET",credentials:"same-origin",cache:"force-cache",headers:{Purpose:"prefetch"}};r&&(o.signal=r.signal);try{fetch(t,o).then(function(t){i&&clearTimeout(i),t&&t.ok||e.delete(n)}).catch(function(){i&&clearTimeout(i),e.delete(n)})}catch(t){i&&clearTimeout(i),e.delete(n)}}}!function(){try{var e=document.currentScript;e&&e.nonce&&(f=e.nonce)}catch(e){}try{var t=document.createElement("link");t.relList&&"function"==typeof t.relList.supports&&(d=t.relList.supports("prefetch"))}catch(e){}var n=navigator.userAgent;a=/iPad|iPhone/.test(n)||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1;var r=/Android/.test(n);(o=(a||r)&&Math.min(screen.width,screen.height)<768)&&(p=20);var i=n.match(/Chrome\/(\d+)/);i&&(c=parseInt(i[1],10));var u=navigator.connection;u&&(l=u.effectiveType,s=u.saveData||!1),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",S):S()}();var R=null;function U(){R||(R=new IntersectionObserver(function(e){e.forEach(function(e){e.isIntersecting&&(R.unobserve(e.target),C(e.target)&&W(e.target.href,e.target))})},{rootMargin:o?"100px":"200px"}),B())}function B(){R&&document.querySelectorAll("a").forEach(function(e){C(e)&&R.observe(e)})}var z=null,H=null;window.Prefetch={version:"1.0.7",preload:function(e){W(e)}}}();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prefetchru/prefetch",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "Мгновенная загрузка страниц для российского веба. Instant page loading for Russian web.",
|
|
5
5
|
"main": "prefetch.js",
|
|
6
6
|
"module": "prefetch.js",
|
|
@@ -46,4 +46,3 @@
|
|
|
46
46
|
"terser": "^5.27.0"
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
-
|
package/prefetch.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* prefetch.ru v1.0.
|
|
2
|
+
* prefetch.ru v1.0.7 - Мгновенная загрузка страниц
|
|
3
3
|
* © 2026 Сергей Макаров | MIT License
|
|
4
4
|
* https://prefetch.ru | https://github.com/prefetch-ru
|
|
5
5
|
*/
|
|
@@ -108,7 +108,12 @@
|
|
|
108
108
|
// <body data-prefetch-specrules="prerender">
|
|
109
109
|
// <body data-prefetch-specrules="no">
|
|
110
110
|
var hasSr = 'prefetchSpecrules' in ds || 'instantSpecrules' in ds
|
|
111
|
-
if (
|
|
111
|
+
if (
|
|
112
|
+
!isIOS &&
|
|
113
|
+
hasSr &&
|
|
114
|
+
HTMLScriptElement.supports &&
|
|
115
|
+
HTMLScriptElement.supports('speculationrules')
|
|
116
|
+
) {
|
|
112
117
|
var sr = ds.prefetchSpecrules || ds.instantSpecrules
|
|
113
118
|
if (sr === 'prerender') {
|
|
114
119
|
specMode = 'prerender'
|
|
@@ -238,7 +243,11 @@
|
|
|
238
243
|
var a = getAnchorFromEventTarget(e.target)
|
|
239
244
|
if (!canPreload(a)) return
|
|
240
245
|
|
|
241
|
-
|
|
246
|
+
// v1.0.7: защита от множественных mouseover по вложенным элементам (не плодим таймеры)
|
|
247
|
+
if (hoverTimers.has(a)) return
|
|
248
|
+
|
|
249
|
+
// mouseleave не срабатывает при перемещении внутри ссылки (в отличие от mouseout)
|
|
250
|
+
a.addEventListener('mouseleave', onMouseLeave, { passive: true, once: true })
|
|
242
251
|
|
|
243
252
|
var t = setTimeout(function () {
|
|
244
253
|
preload(a.href, a)
|
|
@@ -247,10 +256,9 @@
|
|
|
247
256
|
hoverTimers.set(a, t)
|
|
248
257
|
}
|
|
249
258
|
|
|
250
|
-
function
|
|
251
|
-
var a =
|
|
259
|
+
function onMouseLeave(e) {
|
|
260
|
+
var a = e.currentTarget
|
|
252
261
|
if (!a) return
|
|
253
|
-
if (e.relatedTarget && e.relatedTarget.closest && a === e.relatedTarget.closest('a')) return
|
|
254
262
|
|
|
255
263
|
var t = hoverTimers.get(a)
|
|
256
264
|
if (t) {
|
|
@@ -268,7 +276,13 @@
|
|
|
268
276
|
}
|
|
269
277
|
|
|
270
278
|
function canPreload(a) {
|
|
271
|
-
if (!a
|
|
279
|
+
if (!a) return false
|
|
280
|
+
|
|
281
|
+
// v1.0.7: исключаем <a href=""> и <a> без href (часто используются как кнопки)
|
|
282
|
+
var hrefAttr = a.getAttribute('href')
|
|
283
|
+
if (hrefAttr === null || hrefAttr.trim() === '') return false
|
|
284
|
+
|
|
285
|
+
if (!a.href) return false
|
|
272
286
|
|
|
273
287
|
// Не навигация в текущей вкладке
|
|
274
288
|
if (a.target && a.target !== '_self') return false
|
|
@@ -296,6 +310,9 @@
|
|
|
296
310
|
// Якорь на той же странице
|
|
297
311
|
if (a.hash && a.pathname + a.search === location.pathname + location.search) return false
|
|
298
312
|
|
|
313
|
+
// v1.0.7: не префетчим текущую страницу (в т.ч. для случаев вроде href="")
|
|
314
|
+
if (urlKey(a.href) === urlKey(location.href)) return false
|
|
315
|
+
|
|
299
316
|
// Уже загружено (ключ НЕ модифицирует реальный URL запроса!)
|
|
300
317
|
var key = urlKey(a.href)
|
|
301
318
|
if (preloaded.has(key)) return false
|
|
@@ -309,15 +326,31 @@
|
|
|
309
326
|
function checkPlatform(a) {
|
|
310
327
|
var href = a.href
|
|
311
328
|
|
|
329
|
+
// v1.0.7: для точных проверок /add /delete /remove используем pathname
|
|
330
|
+
var pathname = ''
|
|
331
|
+
var hash = ''
|
|
332
|
+
try {
|
|
333
|
+
var u = new URL(href, location.href)
|
|
334
|
+
pathname = u.pathname || ''
|
|
335
|
+
hash = u.hash || ''
|
|
336
|
+
} catch (e) {
|
|
337
|
+
pathname = ''
|
|
338
|
+
hash = ''
|
|
339
|
+
}
|
|
340
|
+
|
|
312
341
|
if (platform === 'bitrix' || platform === 'bitrix24') {
|
|
313
342
|
if (href.indexOf('/bitrix/') !== -1 || href.indexOf('sessid=') !== -1) return false
|
|
314
343
|
if (a.classList.contains('bx-ajax')) return false
|
|
315
344
|
}
|
|
316
345
|
|
|
317
346
|
if (platform === 'tilda') {
|
|
318
|
-
|
|
347
|
+
// Можно проверять и по href, но hash надёжнее/дешевле
|
|
348
|
+
if (hash.indexOf('#popup:') !== -1 || hash.indexOf('#rec') !== -1) return false
|
|
319
349
|
}
|
|
320
350
|
|
|
351
|
+
// v1.0.7: /add /delete /remove — только как отдельный сегмент пути (или имя файла типа /delete.php)
|
|
352
|
+
var isActionPath = /(^|\/)(add|delete|remove)(\/|$|\.)/i.test(pathname)
|
|
353
|
+
|
|
321
354
|
if (
|
|
322
355
|
href.indexOf('/login') !== -1 ||
|
|
323
356
|
href.indexOf('/logout') !== -1 ||
|
|
@@ -325,12 +358,10 @@
|
|
|
325
358
|
href.indexOf('/register') !== -1 ||
|
|
326
359
|
href.indexOf('/cart') !== -1 ||
|
|
327
360
|
href.indexOf('/basket') !== -1 ||
|
|
328
|
-
|
|
329
|
-
href.indexOf('/delete') !== -1 ||
|
|
330
|
-
href.indexOf('/remove') !== -1
|
|
361
|
+
isActionPath
|
|
331
362
|
) return false
|
|
332
363
|
|
|
333
|
-
if (/\.(pdf|doc|docx|xls|xlsx|zip|rar|exe)($|\?)
|
|
364
|
+
if (/\.(pdf|doc|docx|xls|xlsx|zip|rar|exe)($|\?)/i.test(href)) return false
|
|
334
365
|
|
|
335
366
|
return true
|
|
336
367
|
}
|
|
@@ -351,7 +382,12 @@
|
|
|
351
382
|
if (host === 'googletagmanager.com' || host.endsWith('.googletagmanager.com')) return false
|
|
352
383
|
|
|
353
384
|
if (cls.indexOf('piwik') !== -1 || cls.indexOf('matomo') !== -1) return false
|
|
354
|
-
if (
|
|
385
|
+
if (
|
|
386
|
+
host === 'matomo.org' ||
|
|
387
|
+
host.endsWith('.matomo.org') ||
|
|
388
|
+
host === 'piwik.org' ||
|
|
389
|
+
host.endsWith('.piwik.org')
|
|
390
|
+
) return false
|
|
355
391
|
|
|
356
392
|
return true
|
|
357
393
|
}
|
|
@@ -535,7 +571,7 @@
|
|
|
535
571
|
|
|
536
572
|
// Минимальный публичный API
|
|
537
573
|
window.Prefetch = {
|
|
538
|
-
version: '1.0.
|
|
574
|
+
version: '1.0.7',
|
|
539
575
|
preload: function (url) { preload(url) }
|
|
540
576
|
}
|
|
541
577
|
})()
|