Unverified Commit 8b803d79 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update error when internal href and external as are used (#20658)

* Update error when internal href and external as are used

* Update to handle error outside invariant

* Update err.sh components
parent fad32560
...@@ -11,13 +11,15 @@ Note: this error will only show when the `next/link` component is clicked not wh ...@@ -11,13 +11,15 @@ Note: this error will only show when the `next/link` component is clicked not wh
```jsx ```jsx
import Link from 'next/link' import Link from 'next/link'
export default () => ( export default function Page(props) {
return (
<> <>
<Link href="/[post]" as="/post-1/comments"> <Link href="/[post]" as="/post-1/comments">
<a>Invalid link</a> <a>Invalid link</a>
</Link> </Link>
</> </>
) )
}
``` ```
**Compatible `href` and `as`** **Compatible `href` and `as`**
...@@ -25,13 +27,15 @@ export default () => ( ...@@ -25,13 +27,15 @@ export default () => (
```jsx ```jsx
import Link from 'next/link' import Link from 'next/link'
export default () => ( export default function Page(props) {
return (
<> <>
<Link href="/[post]" as="/post-1"> <Link href="/[post]" as="/post-1">
<a>Valid link</a> <a>Valid link</a>
</Link> </Link>
</> </>
) )
}
``` ```
#### Possible Ways to Fix It #### Possible Ways to Fix It
......
# Invalid relative `href` and external `as` values
#### Why This Error Occurred
Somewhere you are utilizing the `next/link` component, `Router#push`, or `Router#replace` with a relative route in your `href` that has an external `as` value. The `as` value must be relative also or only `href` should be used with an external URL.
Note: this error will only show when the `next/link` component is clicked not when only rendered.
**Incompatible `href` and `as`**
```jsx
import Link from 'next/link'
export default function Page(props) {
return (
<>
<Link href="/invalid" as="mailto:john@example.com">
<a>Invalid link</a>
</Link>
</>
)
}
```
**Compatible `href` and `as`**
```jsx
import Link from 'next/link'
export default function Page(props) {
return (
<>
<Link href="mailto:john@example.com">
<a>Invalid link</a>
</Link>
</>
)
}
```
#### Possible Ways to Fix It
Look for any usage of the `next/link` component, `Router#push`, or `Router#replace` and make sure that the provided `href` and `as` values are compatible
### Useful Links
- [Routing section in Documentation](https://nextjs.org/docs/routing/introduction)
...@@ -904,7 +904,7 @@ export default class Router implements BaseRouter { ...@@ -904,7 +904,7 @@ export default class Router implements BaseRouter {
// pages to allow building the data URL correctly // pages to allow building the data URL correctly
let resolvedAs = as let resolvedAs = as
if (process.env.__NEXT_HAS_REWRITES) { if (process.env.__NEXT_HAS_REWRITES && as.startsWith('/')) {
resolvedAs = resolveRewrites( resolvedAs = resolveRewrites(
addBasePath( addBasePath(
addLocale(delBasePath(parseRelativeUrl(as).pathname), this.locale) addLocale(delBasePath(parseRelativeUrl(as).pathname), this.locale)
...@@ -940,6 +940,19 @@ export default class Router implements BaseRouter { ...@@ -940,6 +940,19 @@ export default class Router implements BaseRouter {
} }
} }
} }
if (!isLocalURL(as)) {
if (process.env.NODE_ENV !== 'production') {
throw new Error(
`Invalid href: "${url}" and as: "${as}", received relative href and external as` +
`\nSee more info: https://err.sh/next.js/invalid-relative-url-external-as`
)
}
window.location.href = as
return false
}
resolvedAs = delLocale(delBasePath(resolvedAs), this.locale) resolvedAs = delLocale(delBasePath(resolvedAs), this.locale)
if (isDynamicRoute(route)) { if (isDynamicRoute(route)) {
......
...@@ -17,7 +17,7 @@ export function parseRelativeUrl(url: string, base?: string) { ...@@ -17,7 +17,7 @@ export function parseRelativeUrl(url: string, base?: string) {
resolvedBase resolvedBase
) )
if (origin !== globalBase.origin) { if (origin !== globalBase.origin) {
throw new Error('invariant: invalid relative URL') throw new Error(`invariant: invalid relative URL, router received ${url}`)
} }
return { return {
pathname, pathname,
......
...@@ -95,7 +95,7 @@ describe('Build Output', () => { ...@@ -95,7 +95,7 @@ describe('Build Output', () => {
expect(indexSize.endsWith('B')).toBe(true) expect(indexSize.endsWith('B')).toBe(true)
// should be no bigger than 62.2 kb // should be no bigger than 62.2 kb
expect(parseFloat(indexFirstLoad)).toBeCloseTo(62.2, 1) expect(parseFloat(indexFirstLoad)).toBeCloseTo(62.3, 1)
expect(indexFirstLoad.endsWith('kB')).toBe(true) expect(indexFirstLoad.endsWith('kB')).toBe(true)
expect(parseFloat(err404Size) - 3.7).toBeLessThanOrEqual(0) expect(parseFloat(err404Size) - 3.7).toBeLessThanOrEqual(0)
......
import Link from 'next/link'
export default function Page() {
return (
<Link href="/second" as="mailto:hello@example.com">
<a id="click-me">email</a>
</Link>
)
}
...@@ -102,6 +102,10 @@ describe('Invalid hrefs', () => { ...@@ -102,6 +102,10 @@ describe('Invalid hrefs', () => {
await noError('/second') await noError('/second')
}) })
it('does not show error when internal href is used with external as', async () => {
await noError('/invalid-relative', true)
})
it('shows error when dynamic route mismatch is used on Link', async () => { it('shows error when dynamic route mismatch is used on Link', async () => {
const browser = await webdriver(appPort, '/dynamic-route-mismatch') const browser = await webdriver(appPort, '/dynamic-route-mismatch')
try { try {
...@@ -166,6 +170,14 @@ describe('Invalid hrefs', () => { ...@@ -166,6 +170,14 @@ describe('Invalid hrefs', () => {
) )
}) })
it('shows error when internal href is used with external as', async () => {
await showsError(
'/invalid-relative',
/Invalid href: "\/second" and as: "mailto:hello@example\.com", received relative href and external as/,
true
)
})
it('does not throw error when dynamic route mismatch is used on Link and params are manually provided', async () => { it('does not throw error when dynamic route mismatch is used on Link and params are manually provided', async () => {
await noError('/dynamic-route-mismatch-manual', true) await noError('/dynamic-route-mismatch-manual', true)
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment