@prefetchru/prefetch 1.0.10 → 1.1.1
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/README.md +34 -6
- package/dist/prefetch.esm.min.js +2 -2
- package/dist/prefetch.min.js +2 -2
- package/package.json +3 -1
- package/prefetch.esm.js +630 -273
- package/prefetch.js +854 -486
- package/src/core.js +925 -0
- package/src/entry-esm.js +58 -0
- package/src/entry-iife.js +27 -0
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@prefetchru/prefetch)
|
|
4
4
|
[](https://www.npmjs.com/package/@prefetchru/prefetch)
|
|
5
|
-
[](https://prefetch.ru)
|
|
6
6
|
[](LICENSE)
|
|
7
7
|
[](https://github.com/prefetch-ru/prefetch/releases)
|
|
8
8
|
[](https://github.com/prefetch-ru/prefetch/commits)
|
|
@@ -27,7 +27,7 @@ Prefetch pages 65ms before user clicks. Pre-loads links on hover with smart fall
|
|
|
27
27
|
- Специальная поддержка 1С-Битрикс и Tilda
|
|
28
28
|
- Отключается на медленных соединениях (2G/3G) и режиме экономии трафика
|
|
29
29
|
- Не влияет на работу аналитики (Яндекс.Метрика, Google Analytics)
|
|
30
|
-
- ~
|
|
30
|
+
- ~4KB gzip, без зависимостей
|
|
31
31
|
|
|
32
32
|
---
|
|
33
33
|
|
|
@@ -103,6 +103,7 @@ ESM версия автоматически определяет `nonce` чер
|
|
|
103
103
|
| `data-prefetch-specrules` | _(пусто)_ | Включить Speculation Rules (prefetch) |
|
|
104
104
|
| | `prerender` | Полный prerender страницы в фоне |
|
|
105
105
|
| | `no` | Отключить Speculation Rules |
|
|
106
|
+
| `data-prefetch-specrules-fallback` | — | Включить fallback при Speculation Rules |
|
|
106
107
|
| `data-prefetch-whitelist` | — | Режим белого списка (только с `data-prefetch`) |
|
|
107
108
|
| `data-prefetch-allow-query-string` | — | Разрешить ссылки с query-параметрами |
|
|
108
109
|
| `data-prefetch-allow-external-links` | — | Разрешить внешние домены |
|
|
@@ -134,13 +135,21 @@ ESM версия автоматически определяет `nonce` чер
|
|
|
134
135
|
### JavaScript API
|
|
135
136
|
|
|
136
137
|
```javascript
|
|
137
|
-
// Доступен объект window.
|
|
138
|
+
// Доступен объект window.PrefetchRu после загрузки скрипта
|
|
139
|
+
// (window.Prefetch также доступен, если не занят другой библиотекой)
|
|
138
140
|
|
|
139
141
|
// Версия библиотеки
|
|
140
|
-
console.log(
|
|
142
|
+
console.log(PrefetchRu.version) // "1.1.1"
|
|
141
143
|
|
|
142
144
|
// Программная предзагрузка URL
|
|
143
|
-
|
|
145
|
+
// ВАЖНО: URL проходит те же проверки, что и автоматические ссылки
|
|
146
|
+
PrefetchRu.preload('/catalog/product-123')
|
|
147
|
+
|
|
148
|
+
// Обновить состояние при навигации в SPA (pushState)
|
|
149
|
+
PrefetchRu.refresh()
|
|
150
|
+
|
|
151
|
+
// Отключить библиотеку (снять обработчики)
|
|
152
|
+
PrefetchRu.destroy()
|
|
144
153
|
```
|
|
145
154
|
|
|
146
155
|
---
|
|
@@ -251,6 +260,12 @@ Prerender только для важных страниц:
|
|
|
251
260
|
|
|
252
261
|
Выбор метода происходит автоматически в зависимости от браузера.
|
|
253
262
|
|
|
263
|
+
### Техническое примечание (Speculation Rules)
|
|
264
|
+
|
|
265
|
+
При использовании Speculation Rules библиотека создаёт `<script type="speculationrules">` и **немедленно удаляет его из DOM** после вставки. Это безопасно: браузер применяет правила при `appendChild`, элемент в DOM не нужен для их работы. Такой подход предотвращает раздувание `<head>` на долгоживущих страницах.
|
|
266
|
+
|
|
267
|
+
**Важно (v1.0.11):** По умолчанию при включённых Speculation Rules fallback на `<link rel="prefetch">` или `fetch()` **не выполняется** — это избавляет от двойного трафика. Если вам нужна гарантированная работа при строгом CSP без nonce, добавьте `data-prefetch-specrules-fallback`.
|
|
268
|
+
|
|
254
269
|
---
|
|
255
270
|
|
|
256
271
|
## Совместимость с платформами
|
|
@@ -303,12 +318,13 @@ Prerender только для важных страниц:
|
|
|
303
318
|
|
|
304
319
|
### Приватность
|
|
305
320
|
|
|
306
|
-
- Не использует cookies
|
|
307
321
|
- Не собирает аналитику
|
|
308
322
|
- Не отслеживает пользователей
|
|
309
323
|
- Работает локально в браузере
|
|
310
324
|
- Не делает запросов к внешним серверам
|
|
311
325
|
|
|
326
|
+
**Важно о cookies:** Same-origin prefetch выполняется с cookies (как обычная навигация) для корректного прогрева персонализированных страниц.
|
|
327
|
+
|
|
312
328
|
### Соответствие законодательству РФ
|
|
313
329
|
|
|
314
330
|
- **152-ФЗ** — не обрабатывает персональные данные
|
|
@@ -332,6 +348,18 @@ Prerender только для важных страниц:
|
|
|
332
348
|
- Используйте `data-no-prefetch` для операций с побочными эффектами
|
|
333
349
|
- На highload проектах рекомендуется мониторить нагрузку на сервер
|
|
334
350
|
|
|
351
|
+
### CSP и WAF (edge-cases)
|
|
352
|
+
|
|
353
|
+
**Content Security Policy (CSP):**
|
|
354
|
+
- При строгом CSP без nonce, Speculation Rules могут быть заблокированы **тихо** (без ошибки)
|
|
355
|
+
- Добавьте `data-prefetch-specrules-fallback` для гарантированного fallback на `<link>` или `fetch()`
|
|
356
|
+
- Или передайте nonce через `data-prefetch-nonce`
|
|
357
|
+
|
|
358
|
+
**Web Application Firewall (WAF):**
|
|
359
|
+
- Prefetch-запросы отправляют заголовок `Purpose: prefetch`
|
|
360
|
+
- Некоторые WAF могут возвращать 403/499 на такие запросы
|
|
361
|
+
- Если наблюдаете странные ошибки — проверьте правила WAF
|
|
362
|
+
|
|
335
363
|
---
|
|
336
364
|
|
|
337
365
|
## Лицензия
|
package/dist/prefetch.esm.min.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* prefetch.ru v1.
|
|
2
|
+
* prefetch.ru v1.1.1 (ESM) - Мгновенная загрузка страниц
|
|
3
3
|
* © 2026 Сергей Макаров | MIT License
|
|
4
4
|
* https://prefetch.ru | https://github.com/prefetch-ru
|
|
5
5
|
*/
|
|
6
|
-
var e="undefined"!=typeof window&&window.Prefetch&&window.Prefetch.__prefetchRu?window.Prefetch:function(e){if("undefined"==typeof window||"undefined"==typeof document)return{__prefetchRu:!0,version:"1.0.10",preload:function(){}};var t=new Set,n=new WeakMap,r=0,o=0,i=null,a=!1,c=!1,u=null,l=null,s=!1,f=null,d=null,h=!1,p=65,m=80,v=50,g=!1,w=!1,y=!1,x=!1,b="none",L=!1,E=!1,T=!1,A=!1;function O(){var e=document.body;if(e){l=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(g="prefetchAllowQueryString"in t||"instantAllowQueryString"in t,w="prefetchAllowExternalLinks"in t||"instantAllowExternalLinks"in t,y="prefetchWhitelist"in t||"instantWhitelist"in t,t.prefetchNonce&&(d=t.prefetchNonce),!d&&t.instantNonce&&(d=t.instantNonce),!c&&("prefetchSpecrules"in t||"instantSpecrules"in t)&&HTMLScriptElement.supports&&HTMLScriptElement.supports("speculationrules")){var n=t.prefetchSpecrules||t.instantSpecrules;"prerender"===n?(b="prerender",x=!0):"no"!==n&&(b="prefetch",x=!0)}L="prefetchPrerenderAll"in t||"instantPrerenderAll"in t;var r=t.prefetchIntensity||t.instantIntensity;if("mousedown"===r)E=!0;else if("viewport"===r||"viewport-all"===r)("viewport-all"===r||a&&S())&&(T=!0);else if(r){var o=parseInt(r,10);!isNaN(o)&&o>=0&&(p=o)}a&&(m=Math.max(60,Math.min(p||0,150))),(A="prefetchObserveDom"in t)||"bitrix"!==l&&"tilda"!==l||(A=!0),"tilda"===l&&p<100&&(p=100);var i={capture:!0,passive:!0};if(document.addEventListener("touchstart",P,i),E?document.addEventListener("mousedown",C,i):document.addEventListener("mouseover",k,i),T&&"undefined"==typeof IntersectionObserver&&(T=!1),T)(window.requestIdleCallback||function(e){setTimeout(e,1)})(B,{timeout:1500});A&&T&&"undefined"!=typeof MutationObserver&&function(){if(U)return;(U=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)})})&&q&&(clearTimeout(z),z=setTimeout(K,100))})).observe(document.body,{childList:!0,subtree:!0})}()}}function S(){return!s&&("slow-2g"!==f&&"2g"!==f)}function N(e){return e?(e.nodeType&&1!==e.nodeType&&(e=e.parentElement),e&&"function"==typeof e.closest?e.closest("a"):null):null}function P(e){r=Date.now();var t=N(e.target);if(I(t)){o&&(clearTimeout(o),o=0),i&&(document.removeEventListener("touchmove",i,!0),document.removeEventListener("scroll",i,!0),i=null);var n=!1;i=function(){n=!0,o&&(clearTimeout(o),o=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}),o=setTimeout(function(){i&&(document.removeEventListener("touchmove",i,!0),document.removeEventListener("scroll",i,!0),i=null),o=0,n||W(t.href,t)},m)}}function k(e){if(!(r&&Date.now()-r<2500)){var t=N(e.target);if(I(t)&&!n.has(t)){t.addEventListener("mouseleave",M,{passive:!0,once:!0});var o=setTimeout(function(){W(t.href,t),n.delete(t)},p);n.set(t,o)}}}function M(e){var t=e.currentTarget;if(t){var r=n.get(t);r&&(clearTimeout(r),n.delete(t))}}function C(e){if(("number"!=typeof e.button||2!==e.button)&&!(e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||r&&Date.now()-r<2500)){var t=N(e.target);I(t)&&W(t.href,t)}}function I(e){if(!e)return!1;var n=e.getAttribute("href");if(null===n||""===n.trim())return!1;if(!e.href)return!1;if(e.target&&"_self"!==e.target)return!1;if(e.hasAttribute("download"))return!1;if("noPrefetch"in e.dataset||"prefetchNo"in e.dataset)return!1;if(y&&!("prefetch"in e.dataset)&&!("instant"in e.dataset))return!1;if("http:"!==e.protocol&&"https:"!==e.protocol)return!1;if("http:"===e.protocol&&"https:"===location.protocol)return!1;if(e.origin!==location.origin){if(!w&&!("prefetch"in e.dataset)&&!("instant"in e.dataset))return!1;if(!u)return!1}if(e.search&&!g&&!("prefetch"in e.dataset)&&!("instant"in e.dataset))return!1;if(e.hash&&e.pathname+e.search===location.pathname+location.search)return!1;if(R(e.href)===R(location.href))return!1;var r=R(e.href);return!t.has(r)&&(!!function(e){var t=e.href,n="",r="";try{var o=new URL(t,location.href);n=o.pathname||"",r=o.hash||""}catch(e){n="",r=""}if("bitrix"===l||"bitrix24"===l){if(-1!==t.indexOf("/bitrix/")||-1!==t.indexOf("sessid="))return!1;if(e.classList.contains("bx-ajax"))return!1}if("tilda"===l&&(-1!==r.indexOf("#popup:")||-1!==r.indexOf("#rec")))return!1;return!/(^|\/)(login|logout|auth|register|cart|basket|add|delete|remove)(\/|$|\.)/i.test(n)&&!/\.(pdf|doc|docx|xls|xlsx|zip|rar|exe)($|\?)/i.test(t)}(e)&&!!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")))))))}(e))}function R(e){try{var t=new URL(e,location.href);return t.origin+t.pathname+t.search}catch(t){return e}}function W(e,n){if(S()){var r=function(e){try{var t=new URL(e,location.href);return t.hash="",t.href}catch(t){return e}}(e),o=R(r);if(!t.has(o)){t.size>=v&&t.delete(t.values().next().value),t.add(o);var i=function(e){return x&&"none"!==b?"prerender"!==b?b:L||y||e&&e.dataset&&("prefetchPrerender"in e.dataset||"instantPrerender"in e.dataset)?"prerender":"prefetch":"none"}(n);if("none"!==i)return function(e,t){var n=document.head;if(!n)return;var r=document.createElement("script");r.type="speculationrules",d&&(r.nonce=d);var o={};o[t]=[{source:"list",urls:[e]}],r.textContent=JSON.stringify(o),n.appendChild(r)}(r,i),void(c||!h?D(r,o):_(r,o));c||!h?D(r,o):_(r,o)}}}function _(e,n){var r=document.head;if(r){var o=document.createElement("link");o.rel="prefetch",o.href=e,o.as="document";try{o.fetchPriority="low"}catch(e){}o.onload=i,o.onerror=function(){t.delete(n),i()},r.appendChild(o)}else t.delete(n);function i(){o.onload=o.onerror=null,o.parentNode&&o.parentNode.removeChild(o)}}function D(e,n){if("function"==typeof fetch){var r=null,o=0;"undefined"!=typeof AbortController&&(r=new AbortController,o=setTimeout(function(){try{r.abort()}catch(e){}},5e3));var i={method:"GET",credentials:"same-origin",cache:"force-cache",headers:{Purpose:"prefetch"}};r&&(i.signal=r.signal);try{fetch(e,i).then(function(e){o&&clearTimeout(o),e&&e.ok||t.delete(n)}).catch(function(){o&&clearTimeout(o),t.delete(n)})}catch(e){o&&clearTimeout(o),t.delete(n)}}else t.delete(n)}!function(){if(!(d=function(e){try{if(!e)return null;for(var t=document.getElementsByTagName("script"),n=0;n<t.length;n++){var r=t[n];if(r&&r.src&&r.src===e){var o=r.nonce||r.getAttribute("nonce")||null;if(o)return o;break}}}catch(e){}return null}(e)))try{var t=document.currentScript;t&&t.nonce&&(d=t.nonce)}catch(e){}try{var n=document.createElement("link");n.relList&&"function"==typeof n.relList.supports&&(h=n.relList.supports("prefetch"))}catch(e){}var r=navigator.userAgent;c=/iPad|iPhone/.test(r)||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1;var o=/Android/.test(r);(a=(c||o)&&Math.min(screen.width,screen.height)<768)&&(v=20);var i=r.match(/Chrome\/(\d+)/);i&&(u=parseInt(i[1],10));var l=navigator.connection;l&&(f=l.effectiveType,s=l.saveData||!1),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",O):O()}();var q=null;function B(){q||(q=new IntersectionObserver(function(e){e.forEach(function(e){e.isIntersecting&&(q.unobserve(e.target),I(e.target)&&W(e.target.href,e.target))})},{rootMargin:a?"100px":"200px"}),K())}function K(){q&&document.querySelectorAll("a").forEach(function(e){I(e)&&q.observe(e)})}var U=null,z=null,H={__prefetchRu:!0,version:"1.0.10",preload:function(e){W(e)}};return window.Prefetch=H,H}(import.meta.url);export{e as Prefetch};export default e;
|
|
6
|
+
var e="undefined"!=typeof window&&window.PrefetchRu&&window.PrefetchRu.__prefetchRu?window.PrefetchRu:"undefined"!=typeof window&&window.Prefetch&&window.Prefetch.__prefetchRu?window.Prefetch:function(e){var t=e&&e.getNonce;if(!(!e||e.isBrowser)||"undefined"==typeof window||"undefined"==typeof document)return{__prefetchRu:!0,version:"1.1.1",preload:function(){},destroy:function(){},refresh:function(){}};var n=new Set,r=new WeakMap,o=!1,i=0,a=[],c=new Set,s={prefetch:[],prerender:[],crossOrigin:[]},u=0,l="",d=0,f=0,h=null,p=!1,m=!1,v=null,g=!1,w=null,y=null,b=!1,E=65,L=80,x=50,T=!1,k=!1,O=!1,P=!1,A="none",S=!1,N=!1,C=!1,q=!1,I=!1,R=/(^|\/)(login|logout|auth|register|cart|basket|add|delete|remove)(\/|$|\.)/i,M=/\.(pdf|doc|docx|xls|xlsx|zip|rar|exe)($|\?)/i;function _(){if(!o){var e=document.body;if(e){l=location.origin+location.pathname+location.search,v=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(T="prefetchAllowQueryString"in t||"instantAllowQueryString"in t,k="prefetchAllowExternalLinks"in t||"instantAllowExternalLinks"in t,O="prefetchWhitelist"in t||"instantWhitelist"in t,t.prefetchNonce&&(y=t.prefetchNonce),!y&&t.instantNonce&&(y=t.instantNonce),!m&&("prefetchSpecrules"in t||"instantSpecrules"in t)&&HTMLScriptElement.supports&&HTMLScriptElement.supports("speculationrules")){var n=t.prefetchSpecrules||t.instantSpecrules;"prerender"===n?(A="prerender",P=!0):"no"!==n&&(A="prefetch",P=!0)}S="prefetchSpecrulesFallback"in t||"instantSpecrulesFallback"in t,N="prefetchPrerenderAll"in t||"instantPrerenderAll"in t;var r=t.prefetchIntensity||t.instantIntensity;if("mousedown"===r)C=!0;else if("viewport"===r||"viewport-all"===r)("viewport-all"===r||p&&D())&&(q=!0);else if(r){var i=parseInt(r,10);!isNaN(i)&&i>=0&&(E=i)}p&&(L=Math.max(60,Math.min(E||0,150))),(I="prefetchObserveDom"in t||"instantObserveDom"in t)||"bitrix"!==v&&"tilda"!==v||(I=!0),window.addEventListener("popstate",U),window.addEventListener("hashchange",U),window.addEventListener("pageshow",W),"tilda"===v&&E<100&&(E=100);var a={capture:!0,passive:!0};if(document.addEventListener("touchstart",z,a),C?document.addEventListener("mousedown",F,a):document.addEventListener("mouseover",K,a),q&&"undefined"==typeof IntersectionObserver&&(q=!1),q)(window.requestIdleCallback||function(e){setTimeout(e,1)})(Z,{timeout:1500});I&&q&&"undefined"!=typeof MutationObserver&&function(){if(o)return;if(te)return;(te=new MutationObserver(function(e){var t=!1;e:for(var n=0;n<e.length;n++)for(var r=e[n].addedNodes,o=0;o<r.length;o++){var i=r[o];if(1===i.nodeType&&("A"===i.tagName||i.querySelector&&i.querySelector("a"))){t=!0;break e}}t&&Y&&(clearTimeout(ne),ne=setTimeout(ee,100))})).observe(document.body,{childList:!0,subtree:!0})}()}}}function D(){return!g&&("slow-2g"!==w&&"2g"!==w&&"3g"!==w)}function U(){l=location.origin+location.pathname+location.search}function W(e){e&&e.persisted&&U()}function B(e){return e?(e.nodeType&&1!==e.nodeType&&(e=e.parentElement),e&&"function"==typeof e.closest?e.closest("a"):null):null}function z(e){if(!(o||e&&!1===e.isTrusted)){d=Date.now();var t=B(e.target);if(G(t)){f&&(clearTimeout(f),f=0),h&&(document.removeEventListener("touchmove",h,!0),document.removeEventListener("scroll",h,!0),h=null);var n=!1;h=function(){n=!0,f&&(clearTimeout(f),f=0),document.removeEventListener("touchmove",h,!0),document.removeEventListener("scroll",h,!0),h=null},document.addEventListener("touchmove",h,{capture:!0,passive:!0,once:!0}),document.addEventListener("scroll",h,{capture:!0,passive:!0,once:!0}),f=setTimeout(function(){h&&(document.removeEventListener("touchmove",h,!0),document.removeEventListener("scroll",h,!0),h=null),f=0,n||H(t.href,t)},L)}}}function K(e){if(!o&&!(e&&!1===e.isTrusted||d&&Date.now()-d<2500)){var t=B(e.target);if(t&&!r.has(t)&&G(t)){t.addEventListener("mouseleave",j,{passive:!0,once:!0});var n=setTimeout(function(){H(t.href,t),r.delete(t)},E);r.set(t,n)}}}function j(e){var t=e.currentTarget;if(t){var n=r.get(t);n&&(clearTimeout(n),r.delete(t))}}function F(e){if(!o&&!(e&&!1===e.isTrusted||"number"==typeof e.button&&(1===e.button||2===e.button)||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||d&&Date.now()-d<2500)){var t=B(e.target);G(t)&&H(t.href,t)}}function G(e){if(!e)return!1;var t=e.getAttribute("href");if(null===t||""===t.trim())return!1;if(!e.href)return!1;if(e.target&&"_self"!==e.target)return!1;if(e.hasAttribute("download"))return!1;if("noPrefetch"in e.dataset||"prefetchNo"in e.dataset)return!1;if(O&&!("prefetch"in e.dataset)&&!("instant"in e.dataset))return!1;if("http:"!==e.protocol&&"https:"!==e.protocol)return!1;if("http:"===e.protocol&&"https:"===location.protocol)return!1;if(e.origin!==location.origin&&!k&&!("prefetch"in e.dataset)&&!("instant"in e.dataset))return!1;if(e.search&&!T&&!("prefetch"in e.dataset)&&!("instant"in e.dataset))return!1;if(e.hash&&e.pathname+e.search===location.pathname+location.search)return!1;var r=e.origin+e.pathname+e.search;return r!==l&&(!n.has(r)&&(!!function(e){var t=e.href,n=e.pathname||"",r=e.hash||"";if("bitrix"===v||"bitrix24"===v){if(-1!==t.indexOf("/bitrix/")||-1!==t.indexOf("sessid="))return!1;if(e.classList.contains("bx-ajax"))return!1}if("tilda"===v&&(-1!==r.indexOf("#popup:")||-1!==r.indexOf("#rec")))return!1;return!R.test(n)&&!M.test(n)}(e)&&!!function(e){var t=e.className||"",n=e.hostname||"";return-1===t.indexOf("ym-")&&("mc.yandex.ru"!==n&&"metrika.yandex.ru"!==n&&(-1===t.indexOf("ga-")&&-1===t.indexOf("gtm-")&&("google-analytics.com"!==n&&!n.endsWith(".google-analytics.com")&&("googletagmanager.com"!==n&&!n.endsWith(".googletagmanager.com")&&(-1===t.indexOf("piwik")&&-1===t.indexOf("matomo")&&("matomo.org"!==n&&!n.endsWith(".matomo.org")&&"piwik.org"!==n&&!n.endsWith(".piwik.org")))))))}(e)))}function H(e,t){if(!o&&D()){var r=function(e){try{var t=new URL(e,location.href),n=t.origin+t.pathname+t.search;return t.hash="",{requestUrl:t.href,key:n}}catch(t){return{requestUrl:e,key:e}}}(e),c=r.requestUrl,s=r.key;if(!n.has(s)){n.size>=x&&n.delete(n.values().next().value),n.add(s);var u=function(e){return P&&"none"!==A?"prerender"!==A?A:N||O||e&&e.dataset&&("prefetchPrerender"in e.dataset||"instantPrerender"in e.dataset)?"prerender":"prefetch":"none"}(t);if(i>=4)return a.length>=50?void n.delete(s):void a.push({url:c,key:s,mode:u});Q(c,s,u)}}}function Q(e,t,n){if("none"===n)m||!b?V(e,t):J(e,t);else{var r=!1;try{!function(e,t){var n=!1;try{n=new URL(e,location.href).origin!==location.origin}catch(e){}n&&"prerender"===t&&(t="prefetch");n?s.crossOrigin.push(e):"prerender"===t?s.prerender.push(e):s.prefetch.push(e);if(!u){var r=window.requestIdleCallback||function(e){setTimeout(e,1)};u=r($,{timeout:50})}}(e,n),r=!0}catch(e){}!S&&r||(m||!b?V(e,t):J(e,t))}}function X(){for(;a.length>0&&i<4;){var e=a.shift();Q(e.url,e.key,e.mode)}}function $(){if(u=0,!o){var e=document.head;if(e){var t={};if(s.prefetch.length>0&&(t.prefetch=t.prefetch||[],t.prefetch.push({source:"list",urls:s.prefetch.slice()}),s.prefetch.length=0),s.prerender.length>0&&(t.prerender=t.prerender||[],t.prerender.push({source:"list",urls:s.prerender.slice()}),s.prerender.length=0),s.crossOrigin.length>0&&(t.prefetch=t.prefetch||[],t.prefetch.push({source:"list",urls:s.crossOrigin.slice(),referrer_policy:"no-referrer",requires:["anonymous-client-ip-when-cross-origin"]}),s.crossOrigin.length=0),t.prefetch||t.prerender){var n=document.createElement("script");n.type="speculationrules",y&&(n.nonce=y),n.textContent=JSON.stringify(t),e.appendChild(n),e.removeChild(n)}}}}function J(e,t){var r=document.head;if(r){i++;var o=document.createElement("link");o.rel="prefetch",o.href=e,o.as="document";try{o.fetchPriority="low"}catch(e){}try{new URL(e,location.href).origin!==location.origin&&(o.referrerPolicy="no-referrer",o.crossOrigin="anonymous")}catch(e){}var a=setTimeout(function(){a=0,n.delete(t),c()},3e4);o.onload=c,o.onerror=function(){n.delete(t),c()},r.appendChild(o)}else n.delete(t);function c(){a&&(clearTimeout(a),a=0),o.onload=o.onerror=null,o.parentNode&&o.parentNode.removeChild(o),i--,X()}}function V(e,t){if("function"==typeof fetch){i++;var r=!1,o=null,a=0;"undefined"!=typeof AbortController&&(o=new AbortController,c.add(o),a=setTimeout(function(){try{o.abort()}catch(e){}l(!1)},5e3));var s=!1;try{s=new URL(e,location.href).origin!==location.origin}catch(e){}var u={method:"GET",cache:"force-cache",credentials:s?"omit":"same-origin",mode:s?"no-cors":"cors"};s||(u.headers={Accept:"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",Purpose:"prefetch"}),s&&(u.referrerPolicy="no-referrer"),o&&(u.signal=o.signal);try{fetch(e,u).then(function(e){l(e&&(e.ok||304===e.status||"opaque"===e.type))}).catch(function(){l(!1)})}catch(e){l(!1)}}else n.delete(t);function l(e){r||(r=!0,a&&clearTimeout(a),o&&c.delete(o),e||n.delete(t),i--,X())}}!function(){if(t)try{y=t()}catch(e){}try{var e=document.createElement("link");e.relList&&"function"==typeof e.relList.supports&&(b=e.relList.supports("prefetch"))}catch(e){}var n=navigator.userAgent,r=navigator.userAgentData;if(r){m=!1;var o="Android"===r.platform;p=r.mobile||!1;for(var i=r.brands||[],a=0;a<i.length;a++){var c=i[a];if("Chromium"===c.brand||"Google Chrome"===c.brand){parseInt(c.version,10);break}}}else{m=/iPad|iPhone/.test(n)||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1;o=/Android/.test(n);p=(m||o)&&Math.min(screen.width,screen.height)<768;var s=n.match(/Chrome\/(\d+)/);s&&parseInt(s[1],10)}p&&(x=20);var u=navigator.connection;u&&(w=u.effectiveType,g=u.saveData||!1),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",_):_()}();var Y=null;function Z(){o||Y||(Y=new IntersectionObserver(function(e){e.forEach(function(e){e.isIntersecting&&(Y.unobserve(e.target),G(e.target)&&H(e.target.href,e.target))})},{rootMargin:p?"100px":"200px"}),ee())}function ee(){Y&&document.querySelectorAll("a").forEach(function(e){G(e)&&Y.observe(e)})}var te=null,ne=null;return{__prefetchRu:!0,version:"1.1.1",preload:function(e){if(function(e){return!(!e||"string"!=typeof e||!(e=e.trim())||/^\/\//.test(e)||/^(javascript|data|vbscript|file):/i.test(e)||!(/^https?:\/\//i.test(e)||/^\//.test(e)||/^\./.test(e))&&/^[a-z][a-z0-9+.-]*:/i.test(e))}(e)){var t=document.createElement("a");t.setAttribute("href",e.trim()),G(t)&&H(t.href,t)}},destroy:function(){o=!0,a.length=0,f&&(clearTimeout(f),f=0),h&&(document.removeEventListener("touchmove",h,!0),document.removeEventListener("scroll",h,!0),h=null),c.forEach(function(e){try{e.abort()}catch(e){}}),c.clear(),u&&((window.cancelIdleCallback||clearTimeout)(u),u=0),s.prefetch.length=s.prerender.length=s.crossOrigin.length=0;var e={capture:!0,passive:!0};document.removeEventListener("touchstart",z,e),document.removeEventListener("mouseover",K,e),document.removeEventListener("mousedown",F,e),window.removeEventListener("popstate",U),window.removeEventListener("hashchange",U),window.removeEventListener("pageshow",W),Y&&(Y.disconnect(),Y=null),te&&(te.disconnect(),te=null)},refresh:U}}({isBrowser:"undefined"!=typeof window,getNonce:function(){var e=function(e){try{if(!e)return null;for(var t=document.getElementsByTagName("script"),n=0;n<t.length;n++){var r=t[n];if(r&&r.src&&r.src===e){var o=r.nonce||r.getAttribute("nonce")||null;if(o)return o;break}}}catch(e){}return null}(import.meta.url);if(e)return e;try{var t=document.currentScript;if(t&&t.nonce)return t.nonce}catch(e){}return null}});"undefined"!=typeof window&&(window.PrefetchRu=e,window.Prefetch||(window.Prefetch=e));export{e as Prefetch,e as default};
|
package/dist/prefetch.min.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* prefetch.ru v1.
|
|
2
|
+
* prefetch.ru v1.1.1 - Мгновенная загрузка страниц
|
|
3
3
|
* © 2026 Сергей Макаров | MIT License
|
|
4
4
|
* https://prefetch.ru | https://github.com/prefetch-ru
|
|
5
5
|
*/
|
|
6
|
-
!function(){"use strict";if(!window.Prefetch||!window.Prefetch.__prefetchRu){var e=new Set,t=new WeakMap,n=0,r=0,o=null,i=!1,a=!1,c=null,u=null,s=!1,l=null,d=null,f=!1,h=65,p=80,m=50,v=!1,g=!1,w=!1,y=!1,x="none",b=!1,L=!1,E=!1,T=!1;!function(){try{var e=document.currentScript;e&&e.nonce&&(d=e.nonce)}catch(e){}try{var t=document.createElement("link");t.relList&&"function"==typeof t.relList.supports&&(f=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);(i=(a||r)&&Math.min(screen.width,screen.height)<768)&&(m=20);var o=n.match(/Chrome\/(\d+)/);o&&(c=parseInt(o[1],10));var u=navigator.connection;u&&(l=u.effectiveType,s=u.saveData||!1),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",N):N()}();var A=null,O=null,S=null;window.Prefetch={__prefetchRu:!0,version:"1.0.10",preload:function(e){q(e)}}}function N(){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&&(d=t.prefetchNonce),!d&&t.instantNonce&&(d=t.instantNonce),!a&&("prefetchSpecrules"in t||"instantSpecrules"in t)&&HTMLScriptElement.supports&&HTMLScriptElement.supports("speculationrules")){var n=t.prefetchSpecrules||t.instantSpecrules;"prerender"===n?(x="prerender",y=!0):"no"!==n&&(x="prefetch",y=!0)}b="prefetchPrerenderAll"in t||"instantPrerenderAll"in t;var r=t.prefetchIntensity||t.instantIntensity;if("mousedown"===r)L=!0;else if("viewport"===r||"viewport-all"===r)("viewport-all"===r||i&&P())&&(E=!0);else if(r){var o=parseInt(r,10);!isNaN(o)&&o>=0&&(h=o)}i&&(p=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",M,c),L?document.addEventListener("mousedown",W,c):document.addEventListener("mouseover",C,c),E&&"undefined"==typeof IntersectionObserver&&(E=!1),E)(window.requestIdleCallback||function(e){setTimeout(e,1)})(U,{timeout:1500});T&&E&&"undefined"!=typeof MutationObserver&&function(){if(O)return;(O=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)})})&&A&&(clearTimeout(S),S=setTimeout(B,100))})).observe(document.body,{childList:!0,subtree:!0})}()}}function P(){return!s&&("slow-2g"!==l&&"2g"!==l)}function k(e){return e?(e.nodeType&&1!==e.nodeType&&(e=e.parentElement),e&&"function"==typeof e.closest?e.closest("a"):null):null}function M(e){n=Date.now();var t=k(e.target);if(D(t)){r&&(clearTimeout(r),r=0),o&&(document.removeEventListener("touchmove",o,!0),document.removeEventListener("scroll",o,!0),o=null);var i=!1;o=function(){i=!0,r&&(clearTimeout(r),r=0),document.removeEventListener("touchmove",o,!0),document.removeEventListener("scroll",o,!0),o=null},document.addEventListener("touchmove",o,{capture:!0,passive:!0,once:!0}),document.addEventListener("scroll",o,{capture:!0,passive:!0,once:!0}),r=setTimeout(function(){o&&(document.removeEventListener("touchmove",o,!0),document.removeEventListener("scroll",o,!0),o=null),r=0,i||q(t.href,t)},p)}}function C(e){if(!(n&&Date.now()-n<2500)){var r=k(e.target);if(D(r)&&!t.has(r)){r.addEventListener("mouseleave",I,{passive:!0,once:!0});var o=setTimeout(function(){q(r.href,r),t.delete(r)},h);t.set(r,o)}}}function I(e){var n=e.currentTarget;if(n){var r=t.get(n);r&&(clearTimeout(r),t.delete(n))}}function W(e){if(("number"!=typeof e.button||2!==e.button)&&!(e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||n&&Date.now()-n<2500)){var t=k(e.target);D(t)&&q(t.href,t)}}function D(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(R(t.href)===R(location.href))return!1;var r=R(t.href);return!e.has(r)&&(!!function(e){var t=e.href,n="",r="";try{var o=new URL(t,location.href);n=o.pathname||"",r=o.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;return!/(^|\/)(login|logout|auth|register|cart|basket|add|delete|remove)(\/|$|\.)/i.test(n)&&!/\.(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 R(e){try{var t=new URL(e,location.href);return t.origin+t.pathname+t.search}catch(t){return e}}function q(t,n){if(P()){var r=function(e){try{var t=new URL(e,location.href);return t.hash="",t.href}catch(t){return e}}(t),o=R(r);if(!e.has(o)){e.size>=m&&e.delete(e.values().next().value),e.add(o);var i=function(e){return y&&"none"!==x?"prerender"!==x?x:b||w||e&&e.dataset&&("prefetchPrerender"in e.dataset||"instantPrerender"in e.dataset)?"prerender":"prefetch":"none"}(n);if("none"!==i)return function(e,t){var n=document.head;if(!n)return;var r=document.createElement("script");r.type="speculationrules",d&&(r.nonce=d);var o={};o[t]=[{source:"list",urls:[e]}],r.textContent=JSON.stringify(o),n.appendChild(r)}(r,i),void(a||!f?K(r,o):_(r,o));a||!f?K(r,o):_(r,o)}}}function _(t,n){var r=document.head;if(r){var o=document.createElement("link");o.rel="prefetch",o.href=t,o.as="document";try{o.fetchPriority="low"}catch(e){}o.onload=i,o.onerror=function(){e.delete(n),i()},r.appendChild(o)}else e.delete(n);function i(){o.onload=o.onerror=null,o.parentNode&&o.parentNode.removeChild(o)}}function K(t,n){if("function"==typeof fetch){var r=null,o=0;"undefined"!=typeof AbortController&&(r=new AbortController,o=setTimeout(function(){try{r.abort()}catch(e){}},5e3));var i={method:"GET",credentials:"same-origin",cache:"force-cache",headers:{Purpose:"prefetch"}};r&&(i.signal=r.signal);try{fetch(t,i).then(function(t){o&&clearTimeout(o),t&&t.ok||e.delete(n)}).catch(function(){o&&clearTimeout(o),e.delete(n)})}catch(t){o&&clearTimeout(o),e.delete(n)}}else e.delete(n)}function U(){A||(A=new IntersectionObserver(function(e){e.forEach(function(e){e.isIntersecting&&(A.unobserve(e.target),D(e.target)&&q(e.target.href,e.target))})},{rootMargin:i?"100px":"200px"}),B())}function B(){A&&document.querySelectorAll("a").forEach(function(e){D(e)&&A.observe(e)})}}();
|
|
6
|
+
!function(){"use strict";if(!(window.PrefetchRu&&window.PrefetchRu.__prefetchRu||window.Prefetch&&window.Prefetch.__prefetchRu)){var e=function(e){var t=e&&e.getNonce;if(e&&!e.isBrowser||"undefined"==typeof window||"undefined"==typeof document)return{__prefetchRu:!0,version:"1.1.1",preload:function(){},destroy:function(){},refresh:function(){}};var n=new Set,r=new WeakMap,o=!1,i=0,a=[],c=new Set,s={prefetch:[],prerender:[],crossOrigin:[]},u=0,l="",d=0,f=0,h=null,p=!1,m=!1,v=null,g=!1,w=null,y=null,b=!1,L=65,E=80,x=50,T=!1,O=!1,k=!1,P=!1,S="none",A=!1,N=!1,C=!1,q=!1,I=!1,M=/(^|\/)(login|logout|auth|register|cart|basket|add|delete|remove)(\/|$|\.)/i,R=/\.(pdf|doc|docx|xls|xlsx|zip|rar|exe)($|\?)/i;function _(){if(!o){var e=document.body;if(e){l=location.origin+location.pathname+location.search,v=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(T="prefetchAllowQueryString"in t||"instantAllowQueryString"in t,O="prefetchAllowExternalLinks"in t||"instantAllowExternalLinks"in t,k="prefetchWhitelist"in t||"instantWhitelist"in t,t.prefetchNonce&&(y=t.prefetchNonce),!y&&t.instantNonce&&(y=t.instantNonce),!m&&("prefetchSpecrules"in t||"instantSpecrules"in t)&&HTMLScriptElement.supports&&HTMLScriptElement.supports("speculationrules")){var n=t.prefetchSpecrules||t.instantSpecrules;"prerender"===n?(S="prerender",P=!0):"no"!==n&&(S="prefetch",P=!0)}A="prefetchSpecrulesFallback"in t||"instantSpecrulesFallback"in t,N="prefetchPrerenderAll"in t||"instantPrerenderAll"in t;var r=t.prefetchIntensity||t.instantIntensity;if("mousedown"===r)C=!0;else if("viewport"===r||"viewport-all"===r)("viewport-all"===r||p&&D())&&(q=!0);else if(r){var i=parseInt(r,10);!isNaN(i)&&i>=0&&(L=i)}p&&(E=Math.max(60,Math.min(L||0,150))),(I="prefetchObserveDom"in t||"instantObserveDom"in t)||"bitrix"!==v&&"tilda"!==v||(I=!0),window.addEventListener("popstate",U),window.addEventListener("hashchange",U),window.addEventListener("pageshow",W),"tilda"===v&&L<100&&(L=100);var a={capture:!0,passive:!0};document.addEventListener("touchstart",z,a),C?document.addEventListener("mousedown",F,a):document.addEventListener("mouseover",K,a),q&&"undefined"==typeof IntersectionObserver&&(q=!1),q&&(window.requestIdleCallback||function(e){setTimeout(e,1)})(Z,{timeout:1500}),I&&q&&"undefined"!=typeof MutationObserver&&(o||te||(te=new MutationObserver(function(e){var t=!1;e:for(var n=0;n<e.length;n++)for(var r=e[n].addedNodes,o=0;o<r.length;o++){var i=r[o];if(1===i.nodeType&&("A"===i.tagName||i.querySelector&&i.querySelector("a"))){t=!0;break e}}t&&Y&&(clearTimeout(ne),ne=setTimeout(ee,100))})).observe(document.body,{childList:!0,subtree:!0}))}}}function D(){return!g&&"slow-2g"!==w&&"2g"!==w&&"3g"!==w}function U(){l=location.origin+location.pathname+location.search}function W(e){e&&e.persisted&&U()}function B(e){return e?(e.nodeType&&1!==e.nodeType&&(e=e.parentElement),e&&"function"==typeof e.closest?e.closest("a"):null):null}function z(e){if(!(o||e&&!1===e.isTrusted)){d=Date.now();var t=B(e.target);if(G(t)){f&&(clearTimeout(f),f=0),h&&(document.removeEventListener("touchmove",h,!0),document.removeEventListener("scroll",h,!0),h=null);var n=!1;h=function(){n=!0,f&&(clearTimeout(f),f=0),document.removeEventListener("touchmove",h,!0),document.removeEventListener("scroll",h,!0),h=null},document.addEventListener("touchmove",h,{capture:!0,passive:!0,once:!0}),document.addEventListener("scroll",h,{capture:!0,passive:!0,once:!0}),f=setTimeout(function(){h&&(document.removeEventListener("touchmove",h,!0),document.removeEventListener("scroll",h,!0),h=null),f=0,n||H(t.href,t)},E)}}}function K(e){if(!o&&!(e&&!1===e.isTrusted||d&&Date.now()-d<2500)){var t=B(e.target);if(t&&!r.has(t)&&G(t)){t.addEventListener("mouseleave",j,{passive:!0,once:!0});var n=setTimeout(function(){H(t.href,t),r.delete(t)},L);r.set(t,n)}}}function j(e){var t=e.currentTarget;if(t){var n=r.get(t);n&&(clearTimeout(n),r.delete(t))}}function F(e){if(!o&&!(e&&!1===e.isTrusted||"number"==typeof e.button&&(1===e.button||2===e.button)||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||d&&Date.now()-d<2500)){var t=B(e.target);G(t)&&H(t.href,t)}}function G(e){if(!e)return!1;var t=e.getAttribute("href");if(null===t||""===t.trim())return!1;if(!e.href)return!1;if(e.target&&"_self"!==e.target)return!1;if(e.hasAttribute("download"))return!1;if("noPrefetch"in e.dataset||"prefetchNo"in e.dataset)return!1;if(k&&!("prefetch"in e.dataset)&&!("instant"in e.dataset))return!1;if("http:"!==e.protocol&&"https:"!==e.protocol)return!1;if("http:"===e.protocol&&"https:"===location.protocol)return!1;if(e.origin!==location.origin&&!O&&!("prefetch"in e.dataset)&&!("instant"in e.dataset))return!1;if(e.search&&!T&&!("prefetch"in e.dataset)&&!("instant"in e.dataset))return!1;if(e.hash&&e.pathname+e.search===location.pathname+location.search)return!1;var r=e.origin+e.pathname+e.search;return r!==l&&!n.has(r)&&!!function(e){var t=e.href,n=e.pathname||"",r=e.hash||"";if("bitrix"===v||"bitrix24"===v){if(-1!==t.indexOf("/bitrix/")||-1!==t.indexOf("sessid="))return!1;if(e.classList.contains("bx-ajax"))return!1}return("tilda"!==v||-1===r.indexOf("#popup:")&&-1===r.indexOf("#rec"))&&(!M.test(n)&&!R.test(n))}(e)&&!!function(e){var t=e.className||"",n=e.hostname||"";return!(-1!==t.indexOf("ym-")||"mc.yandex.ru"===n||"metrika.yandex.ru"===n||-1!==t.indexOf("ga-")||-1!==t.indexOf("gtm-")||"google-analytics.com"===n||n.endsWith(".google-analytics.com")||"googletagmanager.com"===n||n.endsWith(".googletagmanager.com")||-1!==t.indexOf("piwik")||-1!==t.indexOf("matomo")||"matomo.org"===n||n.endsWith(".matomo.org")||"piwik.org"===n||n.endsWith(".piwik.org"))}(e)}function H(e,t){if(!o&&D()){var r=function(e){try{var t=new URL(e,location.href),n=t.origin+t.pathname+t.search;return t.hash="",{requestUrl:t.href,key:n}}catch(t){return{requestUrl:e,key:e}}}(e),c=r.requestUrl,s=r.key;if(!n.has(s)){n.size>=x&&n.delete(n.values().next().value),n.add(s);var u=function(e){return P&&"none"!==S?"prerender"!==S?S:N||k||e&&e.dataset&&("prefetchPrerender"in e.dataset||"instantPrerender"in e.dataset)?"prerender":"prefetch":"none"}(t);if(i>=4)return a.length>=50?void n.delete(s):void a.push({url:c,key:s,mode:u});Q(c,s,u)}}}function Q(e,t,n){if("none"===n)m||!b?V(e,t):J(e,t);else{var r=!1;try{!function(e,t){var n=!1;try{n=new URL(e,location.href).origin!==location.origin}catch(e){}if(n&&"prerender"===t&&(t="prefetch"),n?s.crossOrigin.push(e):"prerender"===t?s.prerender.push(e):s.prefetch.push(e),!u){var r=window.requestIdleCallback||function(e){setTimeout(e,1)};u=r($,{timeout:50})}}(e,n),r=!0}catch(e){}!A&&r||(m||!b?V(e,t):J(e,t))}}function X(){for(;a.length>0&&i<4;){var e=a.shift();Q(e.url,e.key,e.mode)}}function $(){if(u=0,!o){var e=document.head;if(e){var t={};if(s.prefetch.length>0&&(t.prefetch=t.prefetch||[],t.prefetch.push({source:"list",urls:s.prefetch.slice()}),s.prefetch.length=0),s.prerender.length>0&&(t.prerender=t.prerender||[],t.prerender.push({source:"list",urls:s.prerender.slice()}),s.prerender.length=0),s.crossOrigin.length>0&&(t.prefetch=t.prefetch||[],t.prefetch.push({source:"list",urls:s.crossOrigin.slice(),referrer_policy:"no-referrer",requires:["anonymous-client-ip-when-cross-origin"]}),s.crossOrigin.length=0),t.prefetch||t.prerender){var n=document.createElement("script");n.type="speculationrules",y&&(n.nonce=y),n.textContent=JSON.stringify(t),e.appendChild(n),e.removeChild(n)}}}}function J(e,t){var r=document.head;if(r){i++;var o=document.createElement("link");o.rel="prefetch",o.href=e,o.as="document";try{o.fetchPriority="low"}catch(e){}try{new URL(e,location.href).origin!==location.origin&&(o.referrerPolicy="no-referrer",o.crossOrigin="anonymous")}catch(e){}var a=setTimeout(function(){a=0,n.delete(t),c()},3e4);o.onload=c,o.onerror=function(){n.delete(t),c()},r.appendChild(o)}else n.delete(t);function c(){a&&(clearTimeout(a),a=0),o.onload=o.onerror=null,o.parentNode&&o.parentNode.removeChild(o),i--,X()}}function V(e,t){if("function"==typeof fetch){i++;var r=!1,o=null,a=0;"undefined"!=typeof AbortController&&(o=new AbortController,c.add(o),a=setTimeout(function(){try{o.abort()}catch(e){}l(!1)},5e3));var s=!1;try{s=new URL(e,location.href).origin!==location.origin}catch(e){}var u={method:"GET",cache:"force-cache",credentials:s?"omit":"same-origin",mode:s?"no-cors":"cors"};s||(u.headers={Accept:"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",Purpose:"prefetch"}),s&&(u.referrerPolicy="no-referrer"),o&&(u.signal=o.signal);try{fetch(e,u).then(function(e){l(e&&(e.ok||304===e.status||"opaque"===e.type))}).catch(function(){l(!1)})}catch(e){l(!1)}}else n.delete(t);function l(e){r||(r=!0,a&&clearTimeout(a),o&&c.delete(o),e||n.delete(t),i--,X())}}!function(){if(t)try{y=t()}catch(e){}try{var e=document.createElement("link");e.relList&&"function"==typeof e.relList.supports&&(b=e.relList.supports("prefetch"))}catch(e){}var n=navigator.userAgent,r=navigator.userAgentData;if(r){m=!1;var o="Android"===r.platform;p=r.mobile||!1;for(var i=r.brands||[],a=0;a<i.length;a++){var c=i[a];if("Chromium"===c.brand||"Google Chrome"===c.brand){parseInt(c.version,10);break}}}else{m=/iPad|iPhone/.test(n)||"MacIntel"===navigator.platform&&navigator.maxTouchPoints>1,o=/Android/.test(n),p=(m||o)&&Math.min(screen.width,screen.height)<768;var s=n.match(/Chrome\/(\d+)/);s&&parseInt(s[1],10)}p&&(x=20);var u=navigator.connection;u&&(w=u.effectiveType,g=u.saveData||!1),"loading"===document.readyState?document.addEventListener("DOMContentLoaded",_):_()}();var Y=null;function Z(){o||Y||(Y=new IntersectionObserver(function(e){e.forEach(function(e){e.isIntersecting&&(Y.unobserve(e.target),G(e.target)&&H(e.target.href,e.target))})},{rootMargin:p?"100px":"200px"}),ee())}function ee(){Y&&document.querySelectorAll("a").forEach(function(e){G(e)&&Y.observe(e)})}var te=null,ne=null;return{__prefetchRu:!0,version:"1.1.1",preload:function(e){if(function(e){return!(!e||"string"!=typeof e||!(e=e.trim())||/^\/\//.test(e)||/^(javascript|data|vbscript|file):/i.test(e)||!(/^https?:\/\//i.test(e)||/^\//.test(e)||/^\./.test(e))&&/^[a-z][a-z0-9+.-]*:/i.test(e))}(e)){var t=document.createElement("a");t.setAttribute("href",e.trim()),G(t)&&H(t.href,t)}},destroy:function(){o=!0,a.length=0,f&&(clearTimeout(f),f=0),h&&(document.removeEventListener("touchmove",h,!0),document.removeEventListener("scroll",h,!0),h=null),c.forEach(function(e){try{e.abort()}catch(e){}}),c.clear(),u&&((window.cancelIdleCallback||clearTimeout)(u),u=0),s.prefetch.length=s.prerender.length=s.crossOrigin.length=0;var e={capture:!0,passive:!0};document.removeEventListener("touchstart",z,e),document.removeEventListener("mouseover",K,e),document.removeEventListener("mousedown",F,e),window.removeEventListener("popstate",U),window.removeEventListener("hashchange",U),window.removeEventListener("pageshow",W),Y&&(Y.disconnect(),Y=null),te&&(te.disconnect(),te=null)},refresh:U}}({isBrowser:!0,getNonce:function(){try{var e=document.currentScript;if(e&&e.nonce)return e.nonce}catch(e){}return null}});window.PrefetchRu=e,window.Prefetch||(window.Prefetch=e)}}();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prefetchru/prefetch",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Мгновенная загрузка страниц для российского веба. Instant page loading for Russian web.",
|
|
5
5
|
"main": "prefetch.js",
|
|
6
6
|
"module": "prefetch.esm.js",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
"./esm/min": "./dist/prefetch.esm.min.js"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
|
+
"src/",
|
|
20
21
|
"prefetch.js",
|
|
21
22
|
"prefetch.esm.js",
|
|
22
23
|
"dist/prefetch.min.js",
|
|
@@ -55,6 +56,7 @@
|
|
|
55
56
|
"access": "public"
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
59
|
+
"rollup": "^4.28.0",
|
|
58
60
|
"terser": "^5.27.0"
|
|
59
61
|
}
|
|
60
62
|
}
|