Skip to content

Commit c9cb93f

Browse files
committed
chore: update absolute URL check
Signed-off-by: Logan McAnsh <logan@mcan.sh>
1 parent 99d2559 commit c9cb93f

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

packages/remix-react/components.tsx

+11-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
ErrorResponse,
1010
Navigation,
1111
} from "@remix-run/router";
12+
import { createPath } from "@remix-run/router";
1213
import type {
1314
LinkProps,
1415
NavigationType,
@@ -315,33 +316,35 @@ let NavLink = React.forwardRef<HTMLAnchorElement, RemixNavLinkProps>(
315316
);
316317
NavLink.displayName = "NavLink";
317318
export { NavLink };
319+
318320
/**
319321
* This component renders an anchor tag and is the primary way the user will
320322
* navigate around your website.
321323
*
322324
* @see https://remix.run/api/remix#link
323325
*/
324326
let Link = React.forwardRef<HTMLAnchorElement, RemixLinkProps>(
325-
({ to: inputTo, prefetch = "none", children, ...props }, forwardedRef) => {
326-
let to = inputTo.toString();
327-
let isExternal = /^[a-z]+:/.test(to);
328-
let href = useHref(to);
327+
({ to, prefetch = "none", children, ...props }, forwardedRef) => {
328+
let location = typeof to === "string" ? to : createPath(to);
329+
let isAbsolute =
330+
/^[a-z+]+:\/\//i.test(location) || location.startsWith("//");
329331

332+
let href = useHref(location);
330333
let [shouldPrefetch, prefetchHandlers] = usePrefetchBehavior(
331334
prefetch,
332335
props
333336
);
334337

335-
if (isExternal) {
338+
if (isAbsolute) {
336339
return (
337340
<>
338-
<a ref={forwardedRef} href={to} {...props} {...prefetchHandlers}>
341+
<a ref={forwardedRef} href={href} {...props} {...prefetchHandlers}>
339342
{children}
340343
</a>
341344
{shouldPrefetch ? (
342345
<>
343-
<link rel="prerender" href={to} />
344-
<link rel="dns-prefetch" href={to} />
346+
<link rel="prerender" href={href} />
347+
<link rel="dns-prefetch" href={href} />
345348
</>
346349
) : null}
347350
</>

0 commit comments

Comments
 (0)