Skip to content

Commit

Permalink
chore: update absolute URL check
Browse files Browse the repository at this point in the history
Signed-off-by: Logan McAnsh <[email protected]>
  • Loading branch information
mcansh committed Jan 17, 2023
1 parent 99d2559 commit c9cb93f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ErrorResponse,
Navigation,
} from "@remix-run/router";
import { createPath } from "@remix-run/router";
import type {
LinkProps,
NavigationType,
Expand Down Expand Up @@ -315,33 +316,35 @@ let NavLink = React.forwardRef<HTMLAnchorElement, RemixNavLinkProps>(
);
NavLink.displayName = "NavLink";
export { NavLink };

/**
* This component renders an anchor tag and is the primary way the user will
* navigate around your website.
*
* @see https://remix.run/api/remix#link
*/
let Link = React.forwardRef<HTMLAnchorElement, RemixLinkProps>(
({ to: inputTo, prefetch = "none", children, ...props }, forwardedRef) => {
let to = inputTo.toString();
let isExternal = /^[a-z]+:/.test(to);
let href = useHref(to);
({ to, prefetch = "none", children, ...props }, forwardedRef) => {
let location = typeof to === "string" ? to : createPath(to);
let isAbsolute =
/^[a-z+]+:\/\//i.test(location) || location.startsWith("//");

let href = useHref(location);
let [shouldPrefetch, prefetchHandlers] = usePrefetchBehavior(
prefetch,
props
);

if (isExternal) {
if (isAbsolute) {
return (
<>
<a ref={forwardedRef} href={to} {...props} {...prefetchHandlers}>
<a ref={forwardedRef} href={href} {...props} {...prefetchHandlers}>
{children}
</a>
{shouldPrefetch ? (
<>
<link rel="prerender" href={to} />
<link rel="dns-prefetch" href={to} />
<link rel="prerender" href={href} />
<link rel="dns-prefetch" href={href} />
</>
) : null}
</>
Expand Down

0 comments on commit c9cb93f

Please sign in to comment.