diff --git a/packages/next/src/shared/lib/router/utils/resolve-href.ts b/packages/next/src/shared/lib/router/utils/resolve-href.ts index 0a64be83501c8..b4c33ae353da8 100644 --- a/packages/next/src/shared/lib/router/utils/resolve-href.ts +++ b/packages/next/src/shared/lib/router/utils/resolve-href.ts @@ -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 diff --git a/test/e2e/repeated-forward-slashes-error/pages/my/path/[name].js b/test/e2e/repeated-forward-slashes-error/pages/my/path/[name].js new file mode 100644 index 0000000000000..06e03757b674f --- /dev/null +++ b/test/e2e/repeated-forward-slashes-error/pages/my/path/[name].js @@ -0,0 +1,5 @@ +import Link from 'next/link' + +export default function Page() { + return Hello +} diff --git a/test/e2e/repeated-forward-slashes-error/repeated-forward-slashes-error.test.ts b/test/e2e/repeated-forward-slashes-error/repeated-forward-slashes-error.test.ts new file mode 100644 index 0000000000000..f1982fff07727 --- /dev/null +++ b/test/e2e/repeated-forward-slashes-error/repeated-forward-slashes-error.test.ts @@ -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." + ) + }) + } +) diff --git a/test/integration/repeated-slashes/test/index.test.js b/test/integration/repeated-slashes/test/index.test.js index 347550441580a..5c6823038f5bb 100644 --- a/test/integration/repeated-slashes/test/index.test.js +++ b/test/integration/repeated-slashes/test/index.test.js @@ -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}'`)) ) } })