vite-plugin-smart-prefetch 0.4.0 → 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,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,43 +15,32 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/frameworks/react/index.ts
31
21
  var index_exports = {};
32
22
  __export(index_exports, {
33
23
  PrefetchDebugPanel: () => PrefetchDebugPanel,
34
- PrefetchLink: () => PrefetchLink,
35
- getPrefetchManager: () => getPrefetchManager,
36
- getPrefetchManagerTanStack: () => getPrefetchManagerTanStack,
37
- matchPrefetchPattern: () => matchPrefetchPattern,
38
- usePrefetch: () => usePrefetch,
39
- usePrefetchTanStack: () => usePrefetchTanStack
24
+ PrefetchManager: () => PrefetchManager
40
25
  });
41
26
  module.exports = __toCommonJS(index_exports);
42
27
 
43
- // src/frameworks/react/usePrefetch.ts
44
- var import_react = require("react");
45
- var import_react_router_dom = require("react-router-dom");
46
-
47
28
  // src/runtime/prefetch-manager.ts
48
29
  var PrefetchManager = class {
49
- constructor(strategy = "hybrid", debug = false) {
30
+ /**
31
+ * Constructor
32
+ * @param strategy Prefetch strategy: 'auto', 'hover', 'visible', 'idle', or 'hybrid'
33
+ * @param debug Enable debug logging
34
+ * @param initialLocation Optional initial location (pathname)
35
+ */
36
+ constructor(strategy = "hybrid", debug = false, initialLocation) {
50
37
  this.config = null;
51
38
  this.prefetched = /* @__PURE__ */ new Set();
52
39
  this.observer = null;
53
- this.currentSegment = null;
54
- this.fallbackToDefault = true;
40
+ this.currentLocation = null;
55
41
  this.strategy = strategy;
56
42
  this.debug = debug;
43
+ this.currentLocation = initialLocation || null;
57
44
  }
58
45
  /**
59
46
  * Initialize the prefetch manager
@@ -115,25 +102,6 @@ var PrefetchManager = class {
115
102
  }
116
103
  }
117
104
  }
118
- /**
119
- * Set the user segment for segment-based prefetch rules
120
- * @param segment - The user's segment (e.g., 'premium', 'free', 'admin')
121
- * @param fallbackToDefault - Use default rules if segment rules unavailable
122
- */
123
- setSegment(segment, fallbackToDefault = true) {
124
- this.currentSegment = segment;
125
- this.fallbackToDefault = fallbackToDefault;
126
- if (this.debug) {
127
- console.log(`\u{1F504} Segment updated: ${segment || "(none)"}`);
128
- console.log(` Fallback to default: ${fallbackToDefault}`);
129
- }
130
- }
131
- /**
132
- * Get the current segment
133
- */
134
- getSegment() {
135
- return this.currentSegment;
136
- }
137
105
  /**
138
106
  * Match pathname against a route pattern
139
107
  * Supports both React Router (:id) and TanStack Router ($id) syntax
@@ -149,6 +117,30 @@ var PrefetchManager = class {
149
117
  );
150
118
  return patternRegex.test(pathname);
151
119
  }
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) {
141
+ this.currentLocation = pathname;
142
+ this.prefetch(pathname);
143
+ }
152
144
  /**
153
145
  * Prefetch routes based on current route
154
146
  */
@@ -204,23 +196,12 @@ var PrefetchManager = class {
204
196
  }
205
197
  return;
206
198
  }
207
- let prefetchTargets = routeConfig.prefetch;
208
- let ruleSource = "default";
209
- if (this.currentSegment && routeConfig.segments?.[this.currentSegment]) {
210
- prefetchTargets = routeConfig.segments[this.currentSegment];
211
- ruleSource = `segment (${this.currentSegment})`;
212
- } else if (this.currentSegment && !routeConfig.segments?.[this.currentSegment] && !this.fallbackToDefault) {
213
- if (this.debug) {
214
- console.warn(`\u26A0\uFE0F No prefetch rules for segment "${this.currentSegment}" and fallback disabled`);
215
- }
216
- return;
217
- }
199
+ const prefetchTargets = routeConfig.prefetch;
218
200
  console.log(`\u2705 Found prefetch rule for: ${matchedRoute}`);
219
201
  console.log(` Total targets to prefetch: ${prefetchTargets.length}`);
220
- console.log(` Using: ${ruleSource} rules`);
221
202
  console.log(` Strategy: ${this.strategy}`);
222
203
  if (this.debug) {
223
- console.groupCollapsed(`\u{1F4CA} Prefetch Rules for: ${matchedRoute} (${ruleSource})`);
204
+ console.groupCollapsed(`\u{1F4CA} Prefetch Rules for: ${matchedRoute}`);
224
205
  prefetchTargets.forEach((target, index) => {
225
206
  console.log(` ${index + 1}. ${target.route} (${target.priority} priority, ${(target.probability * 100).toFixed(1)}%)`);
226
207
  });
@@ -585,135 +566,18 @@ var PrefetchManager = class {
585
566
  }
586
567
  };
587
568
 
588
- // src/frameworks/react/usePrefetch.ts
589
- var managerInstance = null;
590
- function usePrefetch() {
591
- const location = (0, import_react_router_dom.useLocation)();
592
- const initialized = (0, import_react.useRef)(false);
593
- (0, import_react.useEffect)(() => {
594
- if (!initialized.current) {
595
- const config = window.__SMART_PREFETCH__ || {};
596
- const strategy = config.strategy || "hybrid";
597
- const debug = config.debug || false;
598
- managerInstance = new PrefetchManager(strategy, debug);
599
- managerInstance.init();
600
- window.__FARMART_PREFETCH_MANAGER__ = managerInstance;
601
- initialized.current = true;
602
- if (debug) {
603
- console.log("\u2705 usePrefetch initialized");
604
- console.log("\u2705 Prefetch manager exposed as window.__FARMART_PREFETCH_MANAGER__");
605
- }
606
- }
607
- }, []);
608
- (0, import_react.useEffect)(() => {
609
- if (managerInstance) {
610
- managerInstance.prefetch(location.pathname);
611
- }
612
- }, [location.pathname]);
613
- return managerInstance;
614
- }
615
- function getPrefetchManager() {
616
- return managerInstance;
617
- }
618
-
619
- // src/frameworks/react/usePrefetchTanStack.ts
620
- var import_react2 = require("react");
621
- var import_react_router = require("@tanstack/react-router");
622
- var managerInstance2 = null;
623
- function usePrefetchTanStack() {
624
- const location = (0, import_react_router.useLocation)();
625
- const initialized = (0, import_react2.useRef)(false);
626
- (0, import_react2.useEffect)(() => {
627
- if (!initialized.current) {
628
- const config = window.__SMART_PREFETCH__ || {};
629
- const strategy = config.strategy || "hybrid";
630
- const debug = config.debug || false;
631
- managerInstance2 = new PrefetchManager(strategy, debug);
632
- managerInstance2.init();
633
- window.__FARMART_PREFETCH_MANAGER__ = managerInstance2;
634
- initialized.current = true;
635
- if (debug) {
636
- console.log("\u2705 usePrefetchTanStack initialized");
637
- console.log("\u2705 Prefetch manager exposed as window.__FARMART_PREFETCH_MANAGER__");
638
- }
639
- }
640
- }, []);
641
- (0, import_react2.useEffect)(() => {
642
- if (managerInstance2) {
643
- const pathname = location.pathname.split("?")[0].split("#")[0];
644
- managerInstance2.prefetch(pathname);
645
- }
646
- }, [location.pathname]);
647
- return managerInstance2;
648
- }
649
- function getPrefetchManagerTanStack() {
650
- return managerInstance2;
651
- }
652
- function matchPrefetchPattern(pathname, pattern) {
653
- const patternRegex = new RegExp(
654
- "^" + pattern.replace(/:[^/]+/g, "[^/]+").replace(/\$[^/]+/g, "[^/]+").replace(/\*/g, ".*") + // Match wildcard
655
- "$"
656
- );
657
- return patternRegex.test(pathname);
658
- }
659
-
660
- // src/frameworks/react/PrefetchLink.tsx
661
- var import_react3 = __toESM(require("react"), 1);
662
- var import_react_router_dom2 = require("react-router-dom");
663
- var import_jsx_runtime = require("react/jsx-runtime");
664
- var PrefetchLink = import_react3.default.forwardRef(
665
- ({ prefetch, to, children, onMouseEnter, ...props }, forwardedRef) => {
666
- const linkRef = (0, import_react3.useRef)(null);
667
- const manager = getPrefetchManager();
668
- const route = typeof to === "string" ? to : to.pathname || "";
669
- (0, import_react3.useEffect)(() => {
670
- if (prefetch === "visible" && linkRef.current && manager) {
671
- manager.observeLink(linkRef.current, route);
672
- }
673
- }, [prefetch, route, manager]);
674
- const handleMouseEnter = (e) => {
675
- if (prefetch === "hover" && manager) {
676
- manager.prefetchRoute(route);
677
- }
678
- if (onMouseEnter) {
679
- onMouseEnter(e);
680
- }
681
- };
682
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
683
- import_react_router_dom2.Link,
684
- {
685
- ref: (node) => {
686
- if (node) {
687
- linkRef.current = node;
688
- if (typeof forwardedRef === "function") {
689
- forwardedRef(node);
690
- } else if (forwardedRef) {
691
- forwardedRef.current = node;
692
- }
693
- }
694
- },
695
- to,
696
- onMouseEnter: handleMouseEnter,
697
- ...props,
698
- children
699
- }
700
- );
701
- }
702
- );
703
- PrefetchLink.displayName = "PrefetchLink";
704
-
705
569
  // src/frameworks/react/PrefetchDebugPanel.tsx
706
- var import_react4 = require("react");
707
- var import_jsx_runtime2 = require("react/jsx-runtime");
570
+ var import_react = require("react");
571
+ var import_jsx_runtime = require("react/jsx-runtime");
708
572
  function PrefetchDebugPanel({
709
573
  manager,
710
574
  position = "bottom-right",
711
575
  defaultOpen = false
712
576
  }) {
713
- const [isOpen, setIsOpen] = (0, import_react4.useState)(defaultOpen);
714
- const [stats, setStats] = (0, import_react4.useState)(null);
715
- const [links, setLinks] = (0, import_react4.useState)([]);
716
- (0, import_react4.useEffect)(() => {
577
+ const [isOpen, setIsOpen] = (0, import_react.useState)(defaultOpen);
578
+ const [stats, setStats] = (0, import_react.useState)(null);
579
+ const [links, setLinks] = (0, import_react.useState)([]);
580
+ (0, import_react.useEffect)(() => {
717
581
  if (!manager) return;
718
582
  const updateStats = () => {
719
583
  setStats(manager.getStats());
@@ -793,46 +657,46 @@ function PrefetchDebugPanel({
793
657
  marginBottom: "8px",
794
658
  fontSize: "11px"
795
659
  };
796
- return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: containerStyle, children: [
797
- isOpen && stats && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: panelStyle, children: [
798
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: headerStyle, children: "\u{1F680} Smart Prefetch Debug" }),
799
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: { marginBottom: "12px" }, children: [
800
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: statStyle, children: [
801
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: "Strategy:" }),
802
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("strong", { children: stats.strategy })
660
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: containerStyle, children: [
661
+ isOpen && stats && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: panelStyle, children: [
662
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: headerStyle, children: "\u{1F680} Smart Prefetch Debug" }),
663
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: { marginBottom: "12px" }, children: [
664
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: statStyle, children: [
665
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "Strategy:" }),
666
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("strong", { children: stats.strategy })
803
667
  ] }),
804
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: statStyle, children: [
805
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: "Config Loaded:" }),
806
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("strong", { children: stats.configLoaded ? "\u2705" : "\u274C" })
668
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: statStyle, children: [
669
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "Config Loaded:" }),
670
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("strong", { children: stats.configLoaded ? "\u2705" : "\u274C" })
807
671
  ] }),
808
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: statStyle, children: [
809
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: "Total Prefetched:" }),
810
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("strong", { children: stats.totalPrefetched })
672
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: statStyle, children: [
673
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "Total Prefetched:" }),
674
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("strong", { children: stats.totalPrefetched })
811
675
  ] }),
812
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: statStyle, children: [
813
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { children: "Link Tags in DOM:" }),
814
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("strong", { children: links.length })
676
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: statStyle, children: [
677
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: "Link Tags in DOM:" }),
678
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("strong", { children: links.length })
815
679
  ] })
816
680
  ] }),
817
- stats.prefetchedRoutes.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [
818
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { ...headerStyle, fontSize: "11px" }, children: "Prefetched Routes:" }),
819
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { fontSize: "11px", color: "#666", marginBottom: "8px" }, children: stats.prefetchedRoutes.join(", ") })
681
+ stats.prefetchedRoutes.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
682
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { ...headerStyle, fontSize: "11px" }, children: "Prefetched Routes:" }),
683
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { fontSize: "11px", color: "#666", marginBottom: "8px" }, children: stats.prefetchedRoutes.join(", ") })
820
684
  ] }),
821
- links.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [
822
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { ...headerStyle, fontSize: "11px" }, children: "Active Link Tags:" }),
823
- links.map((link, i) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: linkStyle, children: [
824
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("strong", { children: link.route }) }),
825
- /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { style: { color: "#666", marginTop: "4px" }, children: [
685
+ links.length > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
686
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { ...headerStyle, fontSize: "11px" }, children: "Active Link Tags:" }),
687
+ links.map((link, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: linkStyle, children: [
688
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("strong", { children: link.route }) }),
689
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: { color: "#666", marginTop: "4px" }, children: [
826
690
  "Priority: ",
827
691
  link.priority,
828
692
  " | Prob: ",
829
693
  (parseFloat(link.probability) * 100).toFixed(1),
830
694
  "%"
831
695
  ] }),
832
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { color: "#999", marginTop: "2px", wordBreak: "break-all" }, children: link.href.split("/").pop() })
696
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { style: { color: "#999", marginTop: "2px", wordBreak: "break-all" }, children: link.href.split("/").pop() })
833
697
  ] }, i))
834
698
  ] }),
835
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
699
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
836
700
  "button",
837
701
  {
838
702
  onClick: () => {
@@ -852,7 +716,7 @@ function PrefetchDebugPanel({
852
716
  }
853
717
  )
854
718
  ] }),
855
- /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
719
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
856
720
  "button",
857
721
  {
858
722
  style: buttonStyle,
@@ -866,11 +730,6 @@ function PrefetchDebugPanel({
866
730
  // Annotate the CommonJS export names for ESM import in node:
867
731
  0 && (module.exports = {
868
732
  PrefetchDebugPanel,
869
- PrefetchLink,
870
- getPrefetchManager,
871
- getPrefetchManagerTanStack,
872
- matchPrefetchPattern,
873
- usePrefetch,
874
- usePrefetchTanStack
733
+ PrefetchManager
875
734
  });
876
735
  //# sourceMappingURL=index.cjs.map