vite-plugin-smart-prefetch 0.3.3 → 0.3.5

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,192 @@
1
+ type PrefetchStrategy = 'auto' | 'hover' | 'visible' | 'idle' | 'hybrid';
2
+ interface ModelConfig {
3
+ /** Model type */
4
+ type: 'probability' | 'ml' | 'hybrid';
5
+ /** Minimum probability threshold (0.0 - 1.0, default: 0.3) */
6
+ threshold: number;
7
+ /** Maximum routes to prefetch per page (default: 3) */
8
+ maxPrefetch: number;
9
+ /** How often to update the model */
10
+ updateInterval?: 'build' | 'daily' | 'weekly';
11
+ }
12
+ interface DataSourceMetadata {
13
+ /** Provider name */
14
+ provider: 'google-analytics' | 'firebase-analytics' | 'guess-js' | 'manual' | string;
15
+ /** Date range analyzed */
16
+ dateRange: string;
17
+ /** Total sessions analyzed */
18
+ totalSessions: number;
19
+ /** Total unique routes found */
20
+ totalRoutes?: number;
21
+ }
22
+ interface PrefetchTarget {
23
+ /** Target route to prefetch */
24
+ route: string;
25
+ /** Probability of transition (0.0 - 1.0) */
26
+ probability: number;
27
+ /** Number of observed transitions */
28
+ count: number;
29
+ /** Chunk file to prefetch (set by config generator) */
30
+ chunk?: string;
31
+ /** Dependency chunks to prefetch (extracted from Vite manifest imports) */
32
+ imports?: string[];
33
+ /** Priority level */
34
+ priority: 'high' | 'medium' | 'low';
35
+ /** Whether this is a manual rule */
36
+ manual?: boolean;
37
+ }
38
+ interface RouteMetadata {
39
+ /** Total transitions from this route */
40
+ totalTransitions: number;
41
+ /** Most common destination */
42
+ topDestination: string;
43
+ /** Average time spent on page (seconds) */
44
+ averageTimeOnPage?: number;
45
+ }
46
+ interface PrefetchConfig {
47
+ /** Schema version */
48
+ version: string;
49
+ /** Generation timestamp */
50
+ generatedAt: string;
51
+ /** Environment */
52
+ environment: string;
53
+ /** Data source info */
54
+ dataSource: DataSourceMetadata;
55
+ /** Model info */
56
+ model: {
57
+ type: ModelConfig['type'];
58
+ threshold: number;
59
+ maxPrefetch: number;
60
+ accuracy?: number;
61
+ };
62
+ /** Route predictions with chunk mappings */
63
+ routes: {
64
+ [sourceRoute: string]: {
65
+ prefetch: Array<PrefetchTarget & {
66
+ chunk: string;
67
+ }>;
68
+ metadata?: RouteMetadata;
69
+ };
70
+ };
71
+ /** Quick lookup: route -> chunk file */
72
+ chunks: {
73
+ [route: string]: string;
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Prefetch Manager (Runtime)
79
+ * Manages route prefetching in the browser
80
+ */
81
+
82
+ declare class PrefetchManager {
83
+ private config;
84
+ private prefetched;
85
+ private strategy;
86
+ private observer;
87
+ private debug;
88
+ constructor(strategy?: PrefetchStrategy, debug?: boolean);
89
+ /**
90
+ * Initialize the prefetch manager
91
+ * Loads configuration and sets up observers
92
+ */
93
+ init(): Promise<void>;
94
+ /**
95
+ * Prefetch routes based on current route
96
+ */
97
+ prefetch(currentRoute: string): void;
98
+ /**
99
+ * Prefetch a specific target
100
+ */
101
+ private prefetchTarget;
102
+ /**
103
+ * Hybrid strategy: prioritize based on probability
104
+ * FOR TESTING: Fetch all priorities to verify chunks are correct
105
+ */
106
+ private prefetchHybrid;
107
+ /**
108
+ * Prefetch during idle time
109
+ */
110
+ private prefetchOnIdle;
111
+ /**
112
+ * Inject prefetch link into DOM
113
+ */
114
+ private injectPrefetchLink;
115
+ /**
116
+ * Check if prefetching should be performed based on network conditions
117
+ */
118
+ private shouldPrefetch;
119
+ /**
120
+ * Initialize IntersectionObserver for 'visible' strategy
121
+ */
122
+ private initIntersectionObserver;
123
+ /**
124
+ * Observe an element for visibility-based prefetching
125
+ */
126
+ observeLink(element: HTMLElement, route: string): void;
127
+ /**
128
+ * Manually trigger prefetch for a specific route
129
+ * Useful for hover events
130
+ */
131
+ prefetchRoute(route: string): void;
132
+ /**
133
+ * Get prefetch statistics
134
+ */
135
+ getStats(): {
136
+ totalPrefetched: number;
137
+ prefetchedRoutes: string[];
138
+ configLoaded: boolean;
139
+ strategy: PrefetchStrategy;
140
+ };
141
+ /**
142
+ * Check if a route has been prefetched
143
+ */
144
+ isPrefetched(route: string): boolean;
145
+ /**
146
+ * Clear all prefetch state
147
+ */
148
+ clear(): void;
149
+ /**
150
+ * Get all prefetch link elements currently in the DOM
151
+ */
152
+ getActivePrefetchLinks(): HTMLLinkElement[];
153
+ /**
154
+ * Log current state for debugging
155
+ */
156
+ logDebugInfo(): void;
157
+ /**
158
+ * Log all prefetched pages and chunks (summary view)
159
+ */
160
+ logPrefetchSummary(): void;
161
+ }
162
+
163
+ /**
164
+ * Debug Utilities for Smart Prefetch Plugin
165
+ * Helper functions to inspect and debug prefetch behavior
166
+ */
167
+ /**
168
+ * Get all prefetch link elements from the DOM
169
+ */
170
+ declare function getPrefetchLinks(): HTMLLinkElement[];
171
+ /**
172
+ * Get prefetch links added by our plugin (with data attributes)
173
+ */
174
+ declare function getPluginPrefetchLinks(): HTMLLinkElement[];
175
+ /**
176
+ * Log all prefetch links in a formatted way
177
+ */
178
+ declare function logPrefetchLinks(): void;
179
+ /**
180
+ * Monitor prefetch activity in real-time
181
+ */
182
+ declare function monitorPrefetchActivity(): () => void;
183
+ /**
184
+ * Check if the smart prefetch plugin is properly configured
185
+ */
186
+ declare function checkPluginConfiguration(): void;
187
+ /**
188
+ * Expose all debug utilities globally for console access
189
+ */
190
+ declare function exposeDebugUtils(): void;
191
+
192
+ export { type PrefetchConfig, PrefetchManager, type PrefetchStrategy, type PrefetchTarget, checkPluginConfiguration, exposeDebugUtils, getPluginPrefetchLinks, getPrefetchLinks, logPrefetchLinks, monitorPrefetchActivity };
@@ -0,0 +1,192 @@
1
+ type PrefetchStrategy = 'auto' | 'hover' | 'visible' | 'idle' | 'hybrid';
2
+ interface ModelConfig {
3
+ /** Model type */
4
+ type: 'probability' | 'ml' | 'hybrid';
5
+ /** Minimum probability threshold (0.0 - 1.0, default: 0.3) */
6
+ threshold: number;
7
+ /** Maximum routes to prefetch per page (default: 3) */
8
+ maxPrefetch: number;
9
+ /** How often to update the model */
10
+ updateInterval?: 'build' | 'daily' | 'weekly';
11
+ }
12
+ interface DataSourceMetadata {
13
+ /** Provider name */
14
+ provider: 'google-analytics' | 'firebase-analytics' | 'guess-js' | 'manual' | string;
15
+ /** Date range analyzed */
16
+ dateRange: string;
17
+ /** Total sessions analyzed */
18
+ totalSessions: number;
19
+ /** Total unique routes found */
20
+ totalRoutes?: number;
21
+ }
22
+ interface PrefetchTarget {
23
+ /** Target route to prefetch */
24
+ route: string;
25
+ /** Probability of transition (0.0 - 1.0) */
26
+ probability: number;
27
+ /** Number of observed transitions */
28
+ count: number;
29
+ /** Chunk file to prefetch (set by config generator) */
30
+ chunk?: string;
31
+ /** Dependency chunks to prefetch (extracted from Vite manifest imports) */
32
+ imports?: string[];
33
+ /** Priority level */
34
+ priority: 'high' | 'medium' | 'low';
35
+ /** Whether this is a manual rule */
36
+ manual?: boolean;
37
+ }
38
+ interface RouteMetadata {
39
+ /** Total transitions from this route */
40
+ totalTransitions: number;
41
+ /** Most common destination */
42
+ topDestination: string;
43
+ /** Average time spent on page (seconds) */
44
+ averageTimeOnPage?: number;
45
+ }
46
+ interface PrefetchConfig {
47
+ /** Schema version */
48
+ version: string;
49
+ /** Generation timestamp */
50
+ generatedAt: string;
51
+ /** Environment */
52
+ environment: string;
53
+ /** Data source info */
54
+ dataSource: DataSourceMetadata;
55
+ /** Model info */
56
+ model: {
57
+ type: ModelConfig['type'];
58
+ threshold: number;
59
+ maxPrefetch: number;
60
+ accuracy?: number;
61
+ };
62
+ /** Route predictions with chunk mappings */
63
+ routes: {
64
+ [sourceRoute: string]: {
65
+ prefetch: Array<PrefetchTarget & {
66
+ chunk: string;
67
+ }>;
68
+ metadata?: RouteMetadata;
69
+ };
70
+ };
71
+ /** Quick lookup: route -> chunk file */
72
+ chunks: {
73
+ [route: string]: string;
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Prefetch Manager (Runtime)
79
+ * Manages route prefetching in the browser
80
+ */
81
+
82
+ declare class PrefetchManager {
83
+ private config;
84
+ private prefetched;
85
+ private strategy;
86
+ private observer;
87
+ private debug;
88
+ constructor(strategy?: PrefetchStrategy, debug?: boolean);
89
+ /**
90
+ * Initialize the prefetch manager
91
+ * Loads configuration and sets up observers
92
+ */
93
+ init(): Promise<void>;
94
+ /**
95
+ * Prefetch routes based on current route
96
+ */
97
+ prefetch(currentRoute: string): void;
98
+ /**
99
+ * Prefetch a specific target
100
+ */
101
+ private prefetchTarget;
102
+ /**
103
+ * Hybrid strategy: prioritize based on probability
104
+ * FOR TESTING: Fetch all priorities to verify chunks are correct
105
+ */
106
+ private prefetchHybrid;
107
+ /**
108
+ * Prefetch during idle time
109
+ */
110
+ private prefetchOnIdle;
111
+ /**
112
+ * Inject prefetch link into DOM
113
+ */
114
+ private injectPrefetchLink;
115
+ /**
116
+ * Check if prefetching should be performed based on network conditions
117
+ */
118
+ private shouldPrefetch;
119
+ /**
120
+ * Initialize IntersectionObserver for 'visible' strategy
121
+ */
122
+ private initIntersectionObserver;
123
+ /**
124
+ * Observe an element for visibility-based prefetching
125
+ */
126
+ observeLink(element: HTMLElement, route: string): void;
127
+ /**
128
+ * Manually trigger prefetch for a specific route
129
+ * Useful for hover events
130
+ */
131
+ prefetchRoute(route: string): void;
132
+ /**
133
+ * Get prefetch statistics
134
+ */
135
+ getStats(): {
136
+ totalPrefetched: number;
137
+ prefetchedRoutes: string[];
138
+ configLoaded: boolean;
139
+ strategy: PrefetchStrategy;
140
+ };
141
+ /**
142
+ * Check if a route has been prefetched
143
+ */
144
+ isPrefetched(route: string): boolean;
145
+ /**
146
+ * Clear all prefetch state
147
+ */
148
+ clear(): void;
149
+ /**
150
+ * Get all prefetch link elements currently in the DOM
151
+ */
152
+ getActivePrefetchLinks(): HTMLLinkElement[];
153
+ /**
154
+ * Log current state for debugging
155
+ */
156
+ logDebugInfo(): void;
157
+ /**
158
+ * Log all prefetched pages and chunks (summary view)
159
+ */
160
+ logPrefetchSummary(): void;
161
+ }
162
+
163
+ /**
164
+ * Debug Utilities for Smart Prefetch Plugin
165
+ * Helper functions to inspect and debug prefetch behavior
166
+ */
167
+ /**
168
+ * Get all prefetch link elements from the DOM
169
+ */
170
+ declare function getPrefetchLinks(): HTMLLinkElement[];
171
+ /**
172
+ * Get prefetch links added by our plugin (with data attributes)
173
+ */
174
+ declare function getPluginPrefetchLinks(): HTMLLinkElement[];
175
+ /**
176
+ * Log all prefetch links in a formatted way
177
+ */
178
+ declare function logPrefetchLinks(): void;
179
+ /**
180
+ * Monitor prefetch activity in real-time
181
+ */
182
+ declare function monitorPrefetchActivity(): () => void;
183
+ /**
184
+ * Check if the smart prefetch plugin is properly configured
185
+ */
186
+ declare function checkPluginConfiguration(): void;
187
+ /**
188
+ * Expose all debug utilities globally for console access
189
+ */
190
+ declare function exposeDebugUtils(): void;
191
+
192
+ export { type PrefetchConfig, PrefetchManager, type PrefetchStrategy, type PrefetchTarget, checkPluginConfiguration, exposeDebugUtils, getPluginPrefetchLinks, getPrefetchLinks, logPrefetchLinks, monitorPrefetchActivity };