Skip to content

Commit

Permalink
Fix TypeError: Failed to construct 'URL': Invalid URL (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Apr 17, 2024
1 parent 7d587c7 commit 40463d5
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions components/text.js
Expand Up @@ -178,7 +178,12 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
// If [text](url) was parsed as <a> and text is not empty and not a link itself,
// we don't render it as an image since it was probably a conscious choice to include text.
const text = children[0]
const url = !href.startsWith('/') && new URL(href)
let url
try {
url = !href.startsWith('/') && new URL(href)
} catch {
// ignore invalid URLs
}
const internalURL = process.env.NEXT_PUBLIC_URL
if (!!text && !/^https?:\/\//.test(text)) {
if (props['data-footnote-ref'] || typeof props['data-footnote-backref'] !== 'undefined') {
Expand All @@ -191,7 +196,7 @@ export default memo(function Text ({ rel, imgproxyUrls, children, tab, itemId, o
</Link>
)
}
if (href.startsWith('/') || url.origin === internalURL) {
if (href.startsWith('/') || url?.origin === internalURL) {
return (
<Link
id={props.id}
Expand Down

0 comments on commit 40463d5

Please sign in to comment.