Skip to content

Commit

Permalink
Make sure bad links in router methods resolve in production (vercel#1…
Browse files Browse the repository at this point in the history
…5135)

edge-case I introduced in vercel#14827
  • Loading branch information
Janpot committed Jul 13, 2020
1 parent dfee552 commit cd79c82
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/next-server/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ export default class Router implements BaseRouter {

const parsed = tryParseRelativeUrl(url)

if (!parsed) return
if (!parsed) return resolve(false)

let { pathname, searchParams } = parsed
const query = searchParamsToUrlQuery(searchParams)
Expand Down
26 changes: 26 additions & 0 deletions test/integration/invalid-href/pages/third.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useRouter } from 'next/router'
import { useState } from 'react'

const invalidLink = 'https://vercel.com/'

export default () => {
const { query, ...router } = useRouter()
const [isDone, setIsDone] = useState(false)
const { method = 'push' } = query

function click(e) {
router[method](invalidLink).then(
() => setIsDone(true),
() => setIsDone(true)
)
}

return (
<>
<button id="click-me" onClick={click}>
Click me
</button>
{isDone ? <div id="is-done">Done</div> : null}
</>
)
}
12 changes: 12 additions & 0 deletions test/integration/invalid-href/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,17 @@ describe('Invalid hrefs', () => {
)
).toBeTruthy()
})

it('makes sure that router push with bad links resolve', async () => {
const browser = await webdriver(appPort, '/third')
await browser.elementByCss('#click-me').click()
await browser.waitForElementByCss('#is-done')
})

it('makes sure that router replace with bad links resolve', async () => {
const browser = await webdriver(appPort, '/third?method=replace')
await browser.elementByCss('#click-me').click()
await browser.waitForElementByCss('#is-done')
})
})
})

0 comments on commit cd79c82

Please sign in to comment.