Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: app-router prefetch crash when an invalid URL is passed to Link #66755

Merged
merged 4 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/next/src/client/components/app-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,17 @@ function Router({
) {
return
}
const url = new URL(addBasePath(href), window.location.href)

let url: URL
try {
url = new URL(addBasePath(href), window.location.href)
} catch (err) {
console.error(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should save the error, in dev mode if it's invalid url we should throw the error so users can discover earlier. And in production build we just swallow the error, not doing any prefetch and also error in console

`Cannot prefetch '${href}' because it cannot be converted to a URL.`
huozhi marked this conversation as resolved.
Show resolved Hide resolved
)
return
}

// External urls can't be prefetched in the same way.
if (isExternalURL(url)) {
return
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/app-dir/app-prefetch/app/invalid-url/delay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use client'

import { useEffect } from 'react'
import { useState } from 'react'

export function Delay({ duration = 500, children }) {
const [isVisible, setIsVisible] = useState(false)
useEffect(() => {
const timeout = setTimeout(() => setIsVisible(true), duration)
return () => clearTimeout(timeout)
}, [duration])

if (!isVisible) return null
return <>{children}</>
}
16 changes: 16 additions & 0 deletions test/e2e/app-dir/app-prefetch/app/invalid-url/from-link/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Link from 'next/link'
import { INVALID_URL } from '../invalid-url'
import { Delay } from '../delay'

export const dynamic = 'force-dynamic'

export default async function Page() {
return (
<>
<Link href={INVALID_URL}>invalid link</Link>
<Delay>
<h1>Hello, world!</h1>
</Delay>
</>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use client'
import { useEffect } from 'react'
import { INVALID_URL } from '../invalid-url'
import { Delay } from '../delay'
import { useRouter } from 'next/navigation'

export const dynamic = 'force-dynamic'

export default async function Page() {
const router = useRouter()
useEffect(() => {
router.prefetch(INVALID_URL)
}, [router])

return (
<Delay>
<h1>Hello, world!</h1>
</Delay>
)
}
8 changes: 8 additions & 0 deletions test/e2e/app-dir/app-prefetch/app/invalid-url/invalid-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// We need a URL that reliably fails `new URL(url, window.location)
// This only fails if `window.location` starts with `https://`:
//
// const invalidUrl = 'http:'
//
// because `new URL('http:', 'http://localhost:3000')` works fine.
// So better to pick something that's always invalid
export const INVALID_URL = '///'
16 changes: 16 additions & 0 deletions test/e2e/app-dir/app-prefetch/prefetching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,20 @@ describe('app dir - prefetching', () => {
})
})
})

describe('should not crash for invalid URLs', () => {
it.each([
{ title: 'passed to Link', path: '/invalid-url/from-link' },
{
title: 'passed to router.prefetch',
path: '/invalid-url/from-router-prefetch',
},
])('$title', async ({ path }) => {
const browser = await next.browser(path)

await check(() => browser.hasElementByCssSelector('h1'), true)

expect(await browser.elementByCss('h1').text()).toEqual('Hello, world!')
})
})
})
Loading