Skip to content

Commit

Permalink
Add workaround for vercel/next.js#53347
Browse files Browse the repository at this point in the history
  • Loading branch information
michel-kraemer committed Aug 13, 2023
1 parent bccfb18 commit 2384705
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/components/DocsLink.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Link from "next/link"
import Link from "./LinkFix"
import { AnchorHTMLAttributes } from "react"
import { Index } from "./docs/Toc"

Expand Down
2 changes: 1 addition & 1 deletion src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Container from "./Container"
import Link from "next/link"
import Link from "./LinkFix"
import { Balancer } from "react-wrap-balancer"

const now = new Date().getFullYear()
Expand Down
46 changes: 46 additions & 0 deletions src/components/LinkFix.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import NextLink from "next/link"
import { createElement, forwardRef } from "react"

// This is a workaround for https://github.com/vercel/next.js/issues/53347
// We render a normal <a> tag for external links and a <Link> for internal ones

// TODO remove this once https://github.com/vercel/next.js/issues/53347 is fixed

const Link: typeof NextLink = forwardRef((props, ref) => {
let href = props.href?.toString()
if (
href !== undefined &&
((href.match(/https?:\/\//) &&
!href.startsWith("https://steep-wms.github.io/") &&
!href.startsWith("http://localhost")) ||
href.startsWith("mailto:"))
) {
let forbiddenProps = [
"as",
"replace",
"scroll",
"shallow",
"passHref",
"prefetch",
"locale",
"legacyBehavior",
"onMouseEnter",
"onTouchStart",
]

let keys = Object.keys(props)
for (let fp of forbiddenProps) {
if (keys.includes(fp)) {
throw new Error(
`Propery '${fp}' is not supported for external link '${href}'`,
)
}
}

return createElement("a", { ...props, "data-external": "external", ref })
}

return <NextLink {...props} ref={ref} />
})

export default Link
2 changes: 1 addition & 1 deletion src/components/NavBar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client"

import Link from "next/link"
import Link from "./LinkFix"
import QuickSearch from "./search/QuickSearch"
import { Spin as Hamburger } from "hamburger-react"
import { useEffect, useRef, useState } from "react"
Expand Down
2 changes: 1 addition & 1 deletion src/components/about/Profile.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client"

import { Balancer } from "react-wrap-balancer"
import Link from "next/link"
import Link from "@/components/LinkFix"
import { Tooltip } from "@/components/Tooltip"
import SimpleIcon from "@/components/SimpleIcon"
import { siGithub } from "simple-icons"
Expand Down
2 changes: 1 addition & 1 deletion src/components/docs/SidebarLeft.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Link from "next/link"
import Link from "@/components/LinkFix"
import { Toc } from "@/components/docs/Toc"
import Sidebar from "./Sidebar"
import { useEffect, useRef } from "react"
Expand Down
2 changes: 1 addition & 1 deletion src/components/docs/SidebarRight.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Link from "next/link"
import Link from "@/components/LinkFix"
import Sidebar from "./Sidebar"
import { Index, Section, Subsection } from "@/components/docs/Toc"
import { ExternalLink } from "lucide-react"
Expand Down
2 changes: 1 addition & 1 deletion src/components/get-started/DownloadButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Button } from "../Button"
import { Download } from "lucide-react"
import SimpleIcon from "../SimpleIcon"
import { siDocker, siGithub } from "simple-icons"
import Link from "next/link"
import Link from "../LinkFix"
import pkg from "../../package.json"

const DownloadButtons = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/home/ExampleWorkflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ChevronRight } from "lucide-react"
import { useAnimate, stagger } from "framer-motion"
import Container from "../Container"
import useInViewEx from "../hooks/useInViewEx"
import Link from "next/link"
import Link from "../LinkFix"

const ExampleWorkflow = () => {
const [scope, animate] = useAnimate()
Expand Down
2 changes: 1 addition & 1 deletion src/components/home/HeroWorkflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
useMotionValue,
useReducedMotion,
} from "framer-motion"
import Link from "next/link"
import Link from "../LinkFix"

const HeroWorkflow = () => {
const [scope, animate] = useAnimate()
Expand Down
2 changes: 1 addition & 1 deletion src/components/search/SearchResultListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChevronLeft } from "lucide-react"
import Link from "next/link"
import Link from "../LinkFix"
import clsx from "clsx"
import type { SearchResult } from "./SearchResult"
import { Index } from "../docs/Toc"
Expand Down
2 changes: 1 addition & 1 deletion src/mdx-components.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import Balancer from "react-wrap-balancer"
import { createElement } from "react"
import Link from "next/link"
import Link from "./components/LinkFix"
import CodeContainer, { CodeContainerProps } from "./components/CodeContainer"
import DocsLink from "./components/DocsLink"

Expand Down

0 comments on commit 2384705

Please sign in to comment.