Skip to content

Commit

Permalink
fix(#37981): handle legacy link behavior with number type children (#…
Browse files Browse the repository at this point in the history
…38013)

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Fixes #37981.

When handling the legacy link behavior on the current version of Next.js, it only wraps single string child in `<a>` tag. The PR fixes the issue by also wrap single number child in `<a>` tag, too.
  • Loading branch information
SukkaW authored Jun 25, 2022
1 parent a148d14 commit c85c410
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/next/client/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@ const Link = React.forwardRef<HTMLAnchorElement, LinkPropsReal>(

children = childrenProp

if (legacyBehavior && typeof children === 'string') {
if (
legacyBehavior &&
(typeof children === 'string' || typeof children === 'number')
) {
children = <a>{children}</a>
}

Expand Down
10 changes: 10 additions & 0 deletions test/integration/client-navigation/pages/link-number-child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import Link from 'next/link'

export default function Page() {
return (
<div>
Hello World. <Link href="/about">{1000}</Link>
</div>
)
}
6 changes: 6 additions & 0 deletions test/integration/client-navigation/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ describe('Client Navigation', () => {
)
})

it('should not throw error when one number type child is provided', async () => {
const browser = await webdriver(context.appPort, '/link-number-child')
expect(await hasRedbox(browser)).toBe(false)
if (browser) await browser.close()
})

it('should navigate back after reload', async () => {
const browser = await webdriver(context.appPort, '/nav')
await browser.elementByCss('#about-link').click()
Expand Down

0 comments on commit c85c410

Please sign in to comment.