Skip to content

Commit

Permalink
Added better detection for absolute urls in link component (#5390)
Browse files Browse the repository at this point in the history
Co-authored-by: Luke Walker <49527534+lkwr@users.noreply.github.com>
  • Loading branch information
brophdawg11 and lkwr authored Feb 8, 2023
1 parent bb6c5df commit c8515ab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-insects-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/react": patch
---

Added better detection for absolute urls in `<Link>`/`<NavLink>` components ([#5286](https://github.com/remix-run/remix/pull/5286))
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@
- lili21
- lionotm
- liranm
- lkwr
- lordofthecactus
- lpsinger
- lswest
Expand Down
10 changes: 4 additions & 6 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,16 @@ function usePrefetchBehavior(
];
}

const ABSOLUTE_URL_REGEX = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;

/**
* A special kind of `<Link>` that knows whether or not it is "active".
*
* @see https://remix.run/components/nav-link
*/
let NavLink = React.forwardRef<HTMLAnchorElement, RemixNavLinkProps>(
({ to, prefetch = "none", ...props }, forwardedRef) => {
let isAbsolute =
typeof to === "string" &&
(/^[a-z+]+:\/\//i.test(to) || to.startsWith("//"));
let isAbsolute = typeof to === "string" && ABSOLUTE_URL_REGEX.test(to);

let href = useHref(to);
let [shouldPrefetch, prefetchHandlers] = usePrefetchBehavior(
Expand Down Expand Up @@ -325,9 +325,7 @@ export { NavLink };
*/
let Link = React.forwardRef<HTMLAnchorElement, RemixLinkProps>(
({ to, prefetch = "none", ...props }, forwardedRef) => {
let isAbsolute =
typeof to === "string" &&
(/^[a-z+]+:\/\//i.test(to) || to.startsWith("//"));
let isAbsolute = typeof to === "string" && ABSOLUTE_URL_REGEX.test(to);

let href = useHref(to);
let [shouldPrefetch, prefetchHandlers] = usePrefetchBehavior(
Expand Down

0 comments on commit c8515ab

Please sign in to comment.