Skip to content

Commit

Permalink
Add page path to repeated slashes in href error (#46557)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
hanneslund authored Mar 1, 2023
1 parent abf8a42 commit b0b5cd8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/next/src/shared/lib/router/utils/resolve-href.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function resolveHref(

if ((urlParts[0] || '').match(/(\/\/|\\)/)) {
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)
urlAsString = (urlProtoMatch ? urlProtoMatch[0] : '') + normalizedUrl
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Link from 'next/link'

export default function Page() {
return <Link href="/hello//world">Hello</Link>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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."
)
})
}
)
2 changes: 1 addition & 1 deletion test/integration/repeated-slashes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ function runTests({ isDev = false, isExport = false, isPages404 = false }) {
browser.eval(
'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}'`))
)
}
})
Expand Down

0 comments on commit b0b5cd8

Please sign in to comment.