@prefetchru/prefetch 1.0.9 → 1.1.0

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.
@@ -0,0 +1,58 @@
1
+ /**
2
+ * ESM entry point for prefetch.ru
3
+ * This file is bundled into prefetch.esm.js
4
+ */
5
+ import { createPrefetchCore } from './core.js'
6
+
7
+ /**
8
+ * Detect CSP nonce from the script tag that loaded this module.
9
+ * ESM modules don't have document.currentScript, so we find by import.meta.url
10
+ */
11
+ function detectNonceFromImportMeta(metaUrl) {
12
+ try {
13
+ if (!metaUrl) return null
14
+ // script.src и import.meta.url обычно оба абсолютные → можно сравнивать напрямую
15
+ var scripts = document.getElementsByTagName('script')
16
+ for (var i = 0; i < scripts.length; i++) {
17
+ var s = scripts[i]
18
+ if (!s || !s.src) continue
19
+ if (s.src === metaUrl) {
20
+ var n = s.nonce || s.getAttribute('nonce') || null
21
+ if (n) return n
22
+ break
23
+ }
24
+ }
25
+ } catch (e) {}
26
+ return null
27
+ }
28
+
29
+ // Guard от двойной инициализации
30
+ var Prefetch =
31
+ (typeof window !== 'undefined' && window.PrefetchRu && window.PrefetchRu.__prefetchRu)
32
+ ? window.PrefetchRu
33
+ : (typeof window !== 'undefined' && window.Prefetch && window.Prefetch.__prefetchRu)
34
+ ? window.Prefetch
35
+ : createPrefetchCore({
36
+ isBrowser: typeof window !== 'undefined',
37
+ getNonce: function () {
38
+ // ESM: nonce через import.meta.url
39
+ var nonce = detectNonceFromImportMeta(import.meta.url)
40
+ if (nonce) return nonce
41
+
42
+ // fallback: на случай окружений, где currentScript всё же доступен
43
+ try {
44
+ var cs = document.currentScript
45
+ if (cs && cs.nonce) return cs.nonce
46
+ } catch (e) {}
47
+ return null
48
+ }
49
+ })
50
+
51
+ // Регистрируем в window (для совместимости)
52
+ if (typeof window !== 'undefined') {
53
+ window.PrefetchRu = Prefetch
54
+ if (!window.Prefetch) window.Prefetch = Prefetch
55
+ }
56
+
57
+ export { Prefetch }
58
+ export default Prefetch
@@ -0,0 +1,27 @@
1
+ /**
2
+ * IIFE entry point for prefetch.ru
3
+ * This file is bundled into prefetch.js
4
+ * Rollup wraps this in an IIFE
5
+ */
6
+ import { createPrefetchCore } from './core.js'
7
+
8
+ // Guard от двойной инициализации (проверяем PrefetchRu первым)
9
+ if (!(window.PrefetchRu && window.PrefetchRu.__prefetchRu) &&
10
+ !(window.Prefetch && window.Prefetch.__prefetchRu)) {
11
+
12
+ var api = createPrefetchCore({
13
+ isBrowser: true,
14
+ getNonce: function () {
15
+ // IIFE: nonce доступен через document.currentScript
16
+ try {
17
+ var cs = document.currentScript
18
+ if (cs && cs.nonce) return cs.nonce
19
+ } catch (e) {}
20
+ return null
21
+ }
22
+ })
23
+
24
+ // Регистрируем в window
25
+ window.PrefetchRu = api
26
+ if (!window.Prefetch) window.Prefetch = api
27
+ }