Unverified Commit b0b5cd8d authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Add page path to repeated slashes in href error (#46557)

Currently if you find this error in your logs it's difficult to know
where it occurred. This adds the page pathname to the error message.

Before

![image](https://user-images.githubusercontent.com/25056922/221892504-383c0d31-5cc4-4e32-b317-9ebc42696151.png)

After

![image](https://user-images.githubusercontent.com/25056922/221892431-1dd2c3e5-b6aa-46a2-8ca9-3be266fee76f.png)

Ref:
[slack](https://vercel.slack.com/archives/C03KAR5DCKC/p1677527238287849?thread_ts=1677521794.667229&cid=C03KAR5DCKC)


## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ]
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent abf8a423
...@@ -43,7 +43,7 @@ export function resolveHref( ...@@ -43,7 +43,7 @@ export function resolveHref(
if ((urlParts[0] || '').match(/(\/\/|\\)/)) { if ((urlParts[0] || '').match(/(\/\/|\\)/)) {
console.error( console.error(
`Invalid href passed to next/router: ${urlAsString}, repeated forward-slashes (//) or backslashes \\ are not valid in the href` `Invalid href '${urlAsString}' passed to next/router in page: '${router.pathname}'. Repeated forward-slashes (//) or backslashes \\ are not valid in the href.`
) )
const normalizedUrl = normalizeRepeatedSlashes(urlAsStringNoProto) const normalizedUrl = normalizeRepeatedSlashes(urlAsStringNoProto)
urlAsString = (urlProtoMatch ? urlProtoMatch[0] : '') + normalizedUrl urlAsString = (urlProtoMatch ? urlProtoMatch[0] : '') + normalizedUrl
......
import Link from 'next/link'
export default function Page() {
return <Link href="/hello//world">Hello</Link>
}
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
createNextDescribe(
'repeated-forward-slashes-error',
{
files: __dirname,
},
({ next }) => {
it('should log error when href has repeated forward-slashes', async () => {
await next.render$('/my/path/name')
await check(() => next.cliOutput, /Invalid href/)
expect(next.cliOutput).toContain(
"Invalid href '/hello//world' passed to next/router in page: '/my/path/[name]'. Repeated forward-slashes (//) or backslashes \\ are not valid in the href."
)
})
}
)
...@@ -303,7 +303,7 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) { ...@@ -303,7 +303,7 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) {
browser.eval( browser.eval(
'window.caughtErrors.map(err => typeof err !== "string" ? err.message : err).join(", ")' 'window.caughtErrors.map(err => typeof err !== "string" ? err.message : err).join(", ")'
), ),
new RegExp(escapeRegex(`Invalid href passed to next/router: ${href}`)) new RegExp(escapeRegex(`Invalid href '${href}'`))
) )
} }
}) })
......
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