vite-plugin-smart-prefetch 0.4.1 → 0.4.2

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.
@@ -1,8 +1,88 @@
1
- import React from 'react';
2
- import { LinkProps } from 'react-router-dom';
3
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
4
2
 
5
3
  type PrefetchStrategy = 'auto' | 'hover' | 'visible' | 'idle' | 'hybrid';
4
+ interface ModelConfig {
5
+ /** Model type */
6
+ type: 'probability' | 'ml' | 'hybrid';
7
+ /** Minimum probability threshold (0.0 - 1.0, default: 0.3) */
8
+ threshold: number;
9
+ /** Maximum routes to prefetch per page (default: 3) */
10
+ maxPrefetch: number;
11
+ /** How often to update the model */
12
+ updateInterval?: 'build' | 'daily' | 'weekly';
13
+ }
14
+ interface DataSourceMetadata {
15
+ /** Provider name */
16
+ provider: 'bigquery' | 'markov-chain' | 'manual' | string;
17
+ /** Date range analyzed */
18
+ dateRange: string;
19
+ /** Total sessions analyzed */
20
+ totalSessions: number;
21
+ /** Total unique routes found */
22
+ totalRoutes?: number;
23
+ }
24
+ interface PrefetchTarget {
25
+ /** Target route to prefetch */
26
+ route: string;
27
+ /** Probability of transition (0.0 - 1.0) */
28
+ probability: number;
29
+ /** Number of observed transitions */
30
+ count: number;
31
+ /** Chunk file to prefetch (set by config generator) */
32
+ chunk?: string;
33
+ /** Production chunk file path (for production builds) */
34
+ chunk_prod?: string;
35
+ /** Dependency chunks to prefetch (extracted from Vite manifest imports) */
36
+ imports?: string[];
37
+ /** Priority level */
38
+ priority: 'high' | 'medium' | 'low';
39
+ /** Whether this is a manual rule */
40
+ manual?: boolean;
41
+ }
42
+ interface RouteMetadata {
43
+ /** Total transitions from this route */
44
+ totalTransitions: number;
45
+ /** Most common destination */
46
+ topDestination: string;
47
+ /** Average time spent on page (seconds) */
48
+ averageTimeOnPage?: number;
49
+ }
50
+ interface PrefetchConfig {
51
+ /** Schema version */
52
+ version: string;
53
+ /** Generation timestamp */
54
+ generatedAt: string;
55
+ /** Environment */
56
+ environment: string;
57
+ /** Data source info */
58
+ dataSource: DataSourceMetadata;
59
+ /** Model info */
60
+ model: {
61
+ type: ModelConfig['type'];
62
+ threshold: number;
63
+ maxPrefetch: number;
64
+ accuracy?: number;
65
+ };
66
+ /** Global route patterns for dynamic route matching */
67
+ routePatterns?: Record<string, string[]>;
68
+ /** Route predictions with chunk mappings */
69
+ routes: {
70
+ [sourceRoute: string]: {
71
+ /** Route pattern variants (for matching dynamic routes like /order/:id) */
72
+ patterns?: string[];
73
+ /** Prefetch rules for this route */
74
+ prefetch: Array<PrefetchTarget & {
75
+ chunk: string;
76
+ }>;
77
+ /** Metadata about this route */
78
+ metadata?: RouteMetadata;
79
+ };
80
+ };
81
+ /** Quick lookup: route -> chunk file */
82
+ chunks: {
83
+ [route: string]: string;
84
+ };
85
+ }
6
86
 
7
87
  /**
8
88
  * Prefetch Manager (Runtime)
@@ -15,7 +95,14 @@ declare class PrefetchManager {
15
95
  private strategy;
16
96
  private observer;
17
97
  private debug;
18
- constructor(strategy?: PrefetchStrategy, debug?: boolean);
98
+ private currentLocation;
99
+ /**
100
+ * Constructor
101
+ * @param strategy Prefetch strategy: 'auto', 'hover', 'visible', 'idle', or 'hybrid'
102
+ * @param debug Enable debug logging
103
+ * @param initialLocation Optional initial location (pathname)
104
+ */
105
+ constructor(strategy?: PrefetchStrategy, debug?: boolean, initialLocation?: string);
19
106
  /**
20
107
  * Initialize the prefetch manager
21
108
  * Loads configuration and sets up observers
@@ -30,6 +117,27 @@ declare class PrefetchManager {
30
117
  * matchRoutePattern('/admin/settings', '/admin/*') → true
31
118
  */
32
119
  private matchRoutePattern;
120
+ /**
121
+ * Update current location and trigger prefetch
122
+ * Call this whenever the application's location changes
123
+ *
124
+ * @param pathname The current pathname (can be from location.pathname, useLocation().pathname, etc.)
125
+ * @example
126
+ * ```tsx
127
+ * // In React Router DOM
128
+ * const location = useLocation();
129
+ * useEffect(() => {
130
+ * manager.onLocationChange(location.pathname);
131
+ * }, [location.pathname]);
132
+ *
133
+ * // In TanStack Router
134
+ * const location = useLocation();
135
+ * useEffect(() => {
136
+ * manager.onLocationChange(location.pathname);
137
+ * }, [location.pathname]);
138
+ * ```
139
+ */
140
+ onLocationChange(pathname: string): void;
33
141
  /**
34
142
  * Prefetch routes based on current route
35
143
  */
@@ -105,117 +213,6 @@ declare class PrefetchManager {
105
213
  logPrefetchSummary(): void;
106
214
  }
107
215
 
108
- /**
109
- * React Hook for Smart Prefetch
110
- * Automatically prefetches routes based on navigation
111
- */
112
-
113
- /**
114
- * React hook to enable smart prefetching
115
- * Call this once in your root App component
116
- *
117
- * @example
118
- * ```tsx
119
- * function App() {
120
- * usePrefetch();
121
- * return <Routes>...</Routes>;
122
- * }
123
- * ```
124
- */
125
- declare function usePrefetch(): PrefetchManager | null;
126
- /**
127
- * Get the prefetch manager instance
128
- * Useful for manual prefetch control
129
- */
130
- declare function getPrefetchManager(): PrefetchManager | null;
131
-
132
- /**
133
- * TanStack Router Hook for Smart Prefetch
134
- * Automatically prefetches routes based on navigation
135
- *
136
- * @example
137
- * ```tsx
138
- * import { usePrefetchTanStack } from 'vite-plugin-smart-prefetch/react';
139
- *
140
- * export function RootLayout() {
141
- * usePrefetchTanStack();
142
- * return <Outlet />;
143
- * }
144
- * ```
145
- */
146
-
147
- /**
148
- * React hook to enable smart prefetching with TanStack Router
149
- * Call this once in your root layout component
150
- *
151
- * Supports dynamic routes with patterns:
152
- * - Static routes: /dashboard, /orders
153
- * - Dynamic routes: /orders/:id, /users/$userId
154
- *
155
- * @example
156
- * ```tsx
157
- * function RootLayout() {
158
- * usePrefetchTanStack();
159
- * return <Outlet />;
160
- * }
161
- * ```
162
- */
163
- declare function usePrefetchTanStack(): PrefetchManager | null;
164
- /**
165
- * Get the prefetch manager instance
166
- * Useful for manual prefetch control
167
- *
168
- * @example
169
- * ```tsx
170
- * const manager = getPrefetchManager();
171
- * manager?.setSegment('premium');
172
- * ```
173
- */
174
- declare function getPrefetchManagerTanStack(): PrefetchManager | null;
175
- /**
176
- * Advanced: Use route pattern matching directly
177
- * Useful if you need to manually check pattern matching logic
178
- *
179
- * @example
180
- * ```tsx
181
- * const matchesPattern = matchPrefetchPattern('/orders/123', '/orders/:id');
182
- * ```
183
- */
184
- declare function matchPrefetchPattern(pathname: string, pattern: string): boolean;
185
-
186
- /**
187
- * Enhanced Link Component with Prefetch Support
188
- * Extends React Router Link with smart prefetching
189
- */
190
-
191
- interface PrefetchLinkProps extends Omit<LinkProps, 'prefetch'> {
192
- /**
193
- * Prefetch behavior
194
- * - 'hover': Prefetch on mouse enter
195
- * - 'visible': Prefetch when link becomes visible
196
- * - 'manual': No automatic prefetch (use onClick)
197
- * - undefined: Use default strategy from config
198
- */
199
- prefetch?: 'hover' | 'visible' | 'manual';
200
- }
201
- /**
202
- * Enhanced Link component with prefetching
203
- *
204
- * @example
205
- * ```tsx
206
- * // Prefetch on hover
207
- * <PrefetchLink to="/orders" prefetch="hover">
208
- * View Orders
209
- * </PrefetchLink>
210
- *
211
- * // Prefetch when visible
212
- * <PrefetchLink to="/dispatch-order" prefetch="visible">
213
- * Create Dispatch
214
- * </PrefetchLink>
215
- * ```
216
- */
217
- declare const PrefetchLink: React.ForwardRefExoticComponent<PrefetchLinkProps & React.RefAttributes<HTMLAnchorElement>>;
218
-
219
216
  interface PrefetchDebugPanelProps {
220
217
  manager: PrefetchManager | null;
221
218
  position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
@@ -223,4 +220,4 @@ interface PrefetchDebugPanelProps {
223
220
  }
224
221
  declare function PrefetchDebugPanel({ manager, position, defaultOpen, }: PrefetchDebugPanelProps): react_jsx_runtime.JSX.Element | null;
225
222
 
226
- export { PrefetchDebugPanel, type PrefetchDebugPanelProps, PrefetchLink, type PrefetchLinkProps, getPrefetchManager, getPrefetchManagerTanStack, matchPrefetchPattern, usePrefetch, usePrefetchTanStack };
223
+ export { type PrefetchConfig, PrefetchDebugPanel, type PrefetchDebugPanelProps, PrefetchManager, type PrefetchStrategy, type PrefetchTarget };
@@ -1,8 +1,88 @@
1
- import React from 'react';
2
- import { LinkProps } from 'react-router-dom';
3
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
4
2
 
5
3
  type PrefetchStrategy = 'auto' | 'hover' | 'visible' | 'idle' | 'hybrid';
4
+ interface ModelConfig {
5
+ /** Model type */
6
+ type: 'probability' | 'ml' | 'hybrid';
7
+ /** Minimum probability threshold (0.0 - 1.0, default: 0.3) */
8
+ threshold: number;
9
+ /** Maximum routes to prefetch per page (default: 3) */
10
+ maxPrefetch: number;
11
+ /** How often to update the model */
12
+ updateInterval?: 'build' | 'daily' | 'weekly';
13
+ }
14
+ interface DataSourceMetadata {
15
+ /** Provider name */
16
+ provider: 'bigquery' | 'markov-chain' | 'manual' | string;
17
+ /** Date range analyzed */
18
+ dateRange: string;
19
+ /** Total sessions analyzed */
20
+ totalSessions: number;
21
+ /** Total unique routes found */
22
+ totalRoutes?: number;
23
+ }
24
+ interface PrefetchTarget {
25
+ /** Target route to prefetch */
26
+ route: string;
27
+ /** Probability of transition (0.0 - 1.0) */
28
+ probability: number;
29
+ /** Number of observed transitions */
30
+ count: number;
31
+ /** Chunk file to prefetch (set by config generator) */
32
+ chunk?: string;
33
+ /** Production chunk file path (for production builds) */
34
+ chunk_prod?: string;
35
+ /** Dependency chunks to prefetch (extracted from Vite manifest imports) */
36
+ imports?: string[];
37
+ /** Priority level */
38
+ priority: 'high' | 'medium' | 'low';
39
+ /** Whether this is a manual rule */
40
+ manual?: boolean;
41
+ }
42
+ interface RouteMetadata {
43
+ /** Total transitions from this route */
44
+ totalTransitions: number;
45
+ /** Most common destination */
46
+ topDestination: string;
47
+ /** Average time spent on page (seconds) */
48
+ averageTimeOnPage?: number;
49
+ }
50
+ interface PrefetchConfig {
51
+ /** Schema version */
52
+ version: string;
53
+ /** Generation timestamp */
54
+ generatedAt: string;
55
+ /** Environment */
56
+ environment: string;
57
+ /** Data source info */
58
+ dataSource: DataSourceMetadata;
59
+ /** Model info */
60
+ model: {
61
+ type: ModelConfig['type'];
62
+ threshold: number;
63
+ maxPrefetch: number;
64
+ accuracy?: number;
65
+ };
66
+ /** Global route patterns for dynamic route matching */
67
+ routePatterns?: Record<string, string[]>;
68
+ /** Route predictions with chunk mappings */
69
+ routes: {
70
+ [sourceRoute: string]: {
71
+ /** Route pattern variants (for matching dynamic routes like /order/:id) */
72
+ patterns?: string[];
73
+ /** Prefetch rules for this route */
74
+ prefetch: Array<PrefetchTarget & {
75
+ chunk: string;
76
+ }>;
77
+ /** Metadata about this route */
78
+ metadata?: RouteMetadata;
79
+ };
80
+ };
81
+ /** Quick lookup: route -> chunk file */
82
+ chunks: {
83
+ [route: string]: string;
84
+ };
85
+ }
6
86
 
7
87
  /**
8
88
  * Prefetch Manager (Runtime)
@@ -15,7 +95,14 @@ declare class PrefetchManager {
15
95
  private strategy;
16
96
  private observer;
17
97
  private debug;
18
- constructor(strategy?: PrefetchStrategy, debug?: boolean);
98
+ private currentLocation;
99
+ /**
100
+ * Constructor
101
+ * @param strategy Prefetch strategy: 'auto', 'hover', 'visible', 'idle', or 'hybrid'
102
+ * @param debug Enable debug logging
103
+ * @param initialLocation Optional initial location (pathname)
104
+ */
105
+ constructor(strategy?: PrefetchStrategy, debug?: boolean, initialLocation?: string);
19
106
  /**
20
107
  * Initialize the prefetch manager
21
108
  * Loads configuration and sets up observers
@@ -30,6 +117,27 @@ declare class PrefetchManager {
30
117
  * matchRoutePattern('/admin/settings', '/admin/*') → true
31
118
  */
32
119
  private matchRoutePattern;
120
+ /**
121
+ * Update current location and trigger prefetch
122
+ * Call this whenever the application's location changes
123
+ *
124
+ * @param pathname The current pathname (can be from location.pathname, useLocation().pathname, etc.)
125
+ * @example
126
+ * ```tsx
127
+ * // In React Router DOM
128
+ * const location = useLocation();
129
+ * useEffect(() => {
130
+ * manager.onLocationChange(location.pathname);
131
+ * }, [location.pathname]);
132
+ *
133
+ * // In TanStack Router
134
+ * const location = useLocation();
135
+ * useEffect(() => {
136
+ * manager.onLocationChange(location.pathname);
137
+ * }, [location.pathname]);
138
+ * ```
139
+ */
140
+ onLocationChange(pathname: string): void;
33
141
  /**
34
142
  * Prefetch routes based on current route
35
143
  */
@@ -105,117 +213,6 @@ declare class PrefetchManager {
105
213
  logPrefetchSummary(): void;
106
214
  }
107
215
 
108
- /**
109
- * React Hook for Smart Prefetch
110
- * Automatically prefetches routes based on navigation
111
- */
112
-
113
- /**
114
- * React hook to enable smart prefetching
115
- * Call this once in your root App component
116
- *
117
- * @example
118
- * ```tsx
119
- * function App() {
120
- * usePrefetch();
121
- * return <Routes>...</Routes>;
122
- * }
123
- * ```
124
- */
125
- declare function usePrefetch(): PrefetchManager | null;
126
- /**
127
- * Get the prefetch manager instance
128
- * Useful for manual prefetch control
129
- */
130
- declare function getPrefetchManager(): PrefetchManager | null;
131
-
132
- /**
133
- * TanStack Router Hook for Smart Prefetch
134
- * Automatically prefetches routes based on navigation
135
- *
136
- * @example
137
- * ```tsx
138
- * import { usePrefetchTanStack } from 'vite-plugin-smart-prefetch/react';
139
- *
140
- * export function RootLayout() {
141
- * usePrefetchTanStack();
142
- * return <Outlet />;
143
- * }
144
- * ```
145
- */
146
-
147
- /**
148
- * React hook to enable smart prefetching with TanStack Router
149
- * Call this once in your root layout component
150
- *
151
- * Supports dynamic routes with patterns:
152
- * - Static routes: /dashboard, /orders
153
- * - Dynamic routes: /orders/:id, /users/$userId
154
- *
155
- * @example
156
- * ```tsx
157
- * function RootLayout() {
158
- * usePrefetchTanStack();
159
- * return <Outlet />;
160
- * }
161
- * ```
162
- */
163
- declare function usePrefetchTanStack(): PrefetchManager | null;
164
- /**
165
- * Get the prefetch manager instance
166
- * Useful for manual prefetch control
167
- *
168
- * @example
169
- * ```tsx
170
- * const manager = getPrefetchManager();
171
- * manager?.setSegment('premium');
172
- * ```
173
- */
174
- declare function getPrefetchManagerTanStack(): PrefetchManager | null;
175
- /**
176
- * Advanced: Use route pattern matching directly
177
- * Useful if you need to manually check pattern matching logic
178
- *
179
- * @example
180
- * ```tsx
181
- * const matchesPattern = matchPrefetchPattern('/orders/123', '/orders/:id');
182
- * ```
183
- */
184
- declare function matchPrefetchPattern(pathname: string, pattern: string): boolean;
185
-
186
- /**
187
- * Enhanced Link Component with Prefetch Support
188
- * Extends React Router Link with smart prefetching
189
- */
190
-
191
- interface PrefetchLinkProps extends Omit<LinkProps, 'prefetch'> {
192
- /**
193
- * Prefetch behavior
194
- * - 'hover': Prefetch on mouse enter
195
- * - 'visible': Prefetch when link becomes visible
196
- * - 'manual': No automatic prefetch (use onClick)
197
- * - undefined: Use default strategy from config
198
- */
199
- prefetch?: 'hover' | 'visible' | 'manual';
200
- }
201
- /**
202
- * Enhanced Link component with prefetching
203
- *
204
- * @example
205
- * ```tsx
206
- * // Prefetch on hover
207
- * <PrefetchLink to="/orders" prefetch="hover">
208
- * View Orders
209
- * </PrefetchLink>
210
- *
211
- * // Prefetch when visible
212
- * <PrefetchLink to="/dispatch-order" prefetch="visible">
213
- * Create Dispatch
214
- * </PrefetchLink>
215
- * ```
216
- */
217
- declare const PrefetchLink: React.ForwardRefExoticComponent<PrefetchLinkProps & React.RefAttributes<HTMLAnchorElement>>;
218
-
219
216
  interface PrefetchDebugPanelProps {
220
217
  manager: PrefetchManager | null;
221
218
  position?: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';
@@ -223,4 +220,4 @@ interface PrefetchDebugPanelProps {
223
220
  }
224
221
  declare function PrefetchDebugPanel({ manager, position, defaultOpen, }: PrefetchDebugPanelProps): react_jsx_runtime.JSX.Element | null;
225
222
 
226
- export { PrefetchDebugPanel, type PrefetchDebugPanelProps, PrefetchLink, type PrefetchLinkProps, getPrefetchManager, getPrefetchManagerTanStack, matchPrefetchPattern, usePrefetch, usePrefetchTanStack };
223
+ export { type PrefetchConfig, PrefetchDebugPanel, type PrefetchDebugPanelProps, PrefetchManager, type PrefetchStrategy, type PrefetchTarget };