Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(ui): a pressed link stops prefetching after the page changes

Sidwebworks1f6a7981a8cb
diff --git a/packages/react/src/PrefetchLink.tsx b/packages/react/src/PrefetchLink.tsx
index ab2a86cea7..f1ffeb7801 100644
--- a/packages/react/src/PrefetchLink.tsx
+++ b/packages/react/src/PrefetchLink.tsx
@@ -6,7 +6,7 @@
import { forwardRef, useState } from "react";
import type { LinkProps } from "react-router";
-import { Link, PrefetchPageLinks, useHref } from "react-router";
+import { Link, PrefetchPageLinks, useHref, useLocation } from "react-router";
const ABSOLUTE_URL = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
@@ -31,8 +31,15 @@ export const PrefetchLink = forwardRef<
Omit<LinkProps, "prefetch">
>(({ onPointerDown, ...props }, ref) => {
const href = useHref(props.to, { relative: props.relative });
- // A new key remounts the prefetch tags, so each press prefetches again.
- const [presses, setPresses] = useState(0);
+ const location = useLocation();
+ // The tags live only on the page that was showing when the link was pressed.
+ // Left mounted, they prefetched the destination again after every later
+ // navigation, for routes that page did not have yet. A new count remounts
+ // them, so each press prefetches again.
+ const [pressed, setPressed] = useState<{
+ locationKey: string;
+ count: number;
+ } | null>(null);
const canPrefetch =
props.to !== "#" &&
!(typeof props.to === "string" && ABSOLUTE_URL.test(props.to));
@@ -52,10 +59,17 @@ export const PrefetchLink = forwardRef<
!event.ctrlKey &&
!event.shiftKey &&
!event.altKey;
- if (plain && canPrefetch) setPresses((n) => n + 1);
+ if (plain && canPrefetch) {
+ setPressed((last) => ({
+ locationKey: location.key,
+ count: (last?.count ?? 0) + 1
+ }));
+ }
}}
/>
- {presses > 0 && <PrefetchPageLinks key={presses} page={href} />}
+ {pressed?.locationKey === location.key && (
+ <PrefetchPageLinks key={pressed.count} page={href} />
+ )}
</>
);
});