-
Notifications
You must be signed in to change notification settings - Fork 26.2k
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
Add warning for invalid href being passed to router #8231
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5f5996d
Add warning for bad href being passed to router
ijjk 97f86ee
Merge remote-tracking branch 'upstream/canary' into invalid-href-warning
ijjk 7dd5982
Merge remote-tracking branch 'upstream/canary' into invalid-href-warning
ijjk d6cbada
Apply suggestions from code review
ijjk fa43d18
Inline invalidHref for better code elimination
ijjk f511f4d
Merge branch 'canary' into invalid-href-warning
Timer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Invalid href passed to router | ||
|
||
#### Why This Error Occurred | ||
|
||
Next.js provides a router which can be utilized via a component imported via `next/link`, a wrapper `withRouter(Component)`, and now a hook `useRouter()`. | ||
When using any of these, it is expected they are only used for internal navigation, i.e. navigating between pages in the same Next.js application. | ||
|
||
Either you passed a non-internal `href` to a `next/link` component or you called `Router#push` or `Router#replace` with one. | ||
|
||
Invalid `href`s include external sites (https://google.com) and `mailto:` links. In the past, usage of these invalid `href`s could have gone unnoticed but since they can cause unexpected behavior. | ||
We now show a warning in development for them. | ||
|
||
#### Possible Ways to Fix It | ||
|
||
Look for any usage of `next/link` or `next/router` that is being passed a non-internal `href` and replace them with either an anchor tag (`<a>`) or `window.location.href = YOUR_HREF`. | ||
|
||
### Useful Links | ||
|
||
- [Routing section in Documentation](https://nextjs.org/docs#routing) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Link from 'next/link' | ||
import { useRouter } from 'next/router' | ||
|
||
const invalidLink = 'mailto:idk@idk.com' | ||
|
||
export default () => { | ||
const { query, ...router } = useRouter() | ||
const { method } = query | ||
|
||
return method ? ( | ||
<a | ||
id='click-me' | ||
onClick={e => { | ||
e.preventDefault() | ||
router[method](invalidLink) | ||
}} | ||
> | ||
invalid link :o | ||
</a> | ||
) : ( | ||
// this should throw an error on load since prefetch | ||
// receives the invalid href | ||
<Link href={invalidLink}> | ||
<a id='click-me'>invalid link :o</a> | ||
</Link> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// page used for loading and installing error catcher | ||
export default () => <p>Hi 👋</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import Link from 'next/link' | ||
import { useRouter } from 'next/router' | ||
|
||
const invalidLink = 'https://google.com/another' | ||
|
||
export default () => { | ||
const { query, ...router } = useRouter() | ||
const { method } = query | ||
|
||
return method ? ( | ||
<a | ||
id='click-me' | ||
onClick={e => { | ||
e.preventDefault() | ||
router[method](invalidLink) | ||
}} | ||
> | ||
invalid link :o | ||
</a> | ||
) : ( | ||
// this should throw an error on load since prefetch | ||
// receives the invalid href | ||
<Link href={invalidLink}> | ||
<a id='click-me'>invalid link :o</a> | ||
</Link> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* eslint-env jest */ | ||
/* global jasmine */ | ||
import { join } from 'path' | ||
import webdriver from 'next-webdriver' | ||
import { | ||
findPort, | ||
launchApp, | ||
killApp, | ||
nextStart, | ||
nextBuild, | ||
getReactErrorOverlayContent, | ||
waitFor | ||
} from 'next-test-utils' | ||
|
||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5 | ||
let app | ||
let appPort | ||
const appDir = join(__dirname, '..') | ||
|
||
const firstErrorRegex = /Invalid href passed to router: mailto:idk@idk.com.*invalid-href-passed/ | ||
const secondErrorRegex = /Invalid href passed to router: .*google\.com.*invalid-href-passed/ | ||
|
||
const showsError = async (pathname, regex, click = false) => { | ||
const browser = await webdriver(appPort, pathname) | ||
if (click) { | ||
await browser.elementByCss('a').click() | ||
} | ||
const errorContent = await getReactErrorOverlayContent(browser) | ||
expect(errorContent).toMatch(regex) | ||
await browser.close() | ||
} | ||
|
||
const noError = async (pathname, click = false) => { | ||
const browser = await webdriver(appPort, '/') | ||
await browser.eval(`(function() { | ||
window.caughtErrors = [] | ||
window.addEventListener('error', function (error) { | ||
window.caughtErrors.push(error.message || 1) | ||
}) | ||
window.addEventListener('unhandledrejection', function (error) { | ||
window.caughtErrors.push(error.message || 1) | ||
}) | ||
window.next.router.replace('${pathname}') | ||
})()`) | ||
await waitFor(250) | ||
if (click) { | ||
await browser.elementByCss('a').click() | ||
} | ||
const numErrors = await browser.eval(`window.caughtErrors.length`) | ||
expect(numErrors).toBe(0) | ||
await browser.close() | ||
} | ||
|
||
describe('Invalid hrefs', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
it('shows error when mailto: is used as href on Link', async () => { | ||
await showsError('/first', firstErrorRegex) | ||
}) | ||
|
||
it('shows error when mailto: is used as href on router.push', async () => { | ||
await showsError('/first?method=push', firstErrorRegex, true) | ||
}) | ||
|
||
it('shows error when mailto: is used as href on router.replace', async () => { | ||
await showsError('/first?method=replace', firstErrorRegex, true) | ||
}) | ||
|
||
it('shows error when https://google.com is used as href on Link', async () => { | ||
await showsError('/second', secondErrorRegex) | ||
}) | ||
|
||
it('shows error when http://google.com is used as href on router.push', async () => { | ||
await showsError('/second?method=push', secondErrorRegex, true) | ||
}) | ||
|
||
it('shows error when https://google.com is used as href on router.replace', async () => { | ||
await showsError('/second?method=replace', secondErrorRegex, true) | ||
}) | ||
}) | ||
|
||
describe('production mode', () => { | ||
beforeAll(async () => { | ||
await nextBuild(appDir) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
}) | ||
afterAll(() => killApp()) | ||
|
||
it('shows error when mailto: is used as href on Link', async () => { | ||
await noError('/first') | ||
}) | ||
|
||
it('shows error when mailto: is used as href on router.push', async () => { | ||
await noError('/first?method=push', true) | ||
}) | ||
|
||
it('shows error when mailto: is used as href on router.replace', async () => { | ||
await noError('/first?method=replace', true) | ||
}) | ||
|
||
it('shows error when https://google.com is used as href on Link', async () => { | ||
await noError('/second') | ||
}) | ||
|
||
it('shows error when http://google.com is used as href on router.push', async () => { | ||
await noError('/second?method=push', true) | ||
}) | ||
|
||
it('shows error when https://google.com is used as href on router.replace', async () => { | ||
await noError('/second?method=replace', true) | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this condition looks wrong, IMO should be
!pathname || !protocol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, just stumbled upon this today using
Router.push
and passing an absolute URL object to bothhref
, andas
.