Commit graph

697 commits

Author SHA1 Message Date
Tim Neutkens
1b4e2ccd63 v12.1.7-canary.11 2022-05-23 13:35:21 +02:00
JJ Kasper
6cc2147386
v12.1.7-canary.10 2022-05-19 17:11:19 -05:00
JJ Kasper
50833d009d
v12.1.7-canary.9 2022-05-19 13:06:44 -05:00
JJ Kasper
5acf9db617
v12.1.7-canary.8 2022-05-18 20:35:50 -05:00
JJ Kasper
af86ca08e4
v12.1.7-canary.7 2022-05-17 11:01:35 -05:00
JJ Kasper
257eccb7fc
v12.1.7-canary.6 2022-05-13 10:25:27 -05:00
JJ Kasper
1ccf368f1a
v12.1.7-canary.5 2022-05-12 13:11:34 -05:00
JJ Kasper
334d42c441
v12.1.7-canary.4 2022-05-11 09:26:55 -05:00
Tim Neutkens
a1bb1c69ed v12.1.7-canary.3 2022-05-06 13:11:55 +02:00
Steven
cefb944ee5 v12.1.7-canary.2 2022-05-05 08:08:52 -04:00
JJ Kasper
87529e987c
v12.1.7-canary.1 2022-05-03 16:02:45 -05:00
Tim Neutkens
b9bf269991 v12.1.7-canary.0 2022-05-03 13:17:28 +02:00
JJ Kasper
b188fab336
v12.1.6 2022-05-02 14:46:56 -05:00
Tim Neutkens
0c23f5d1d2 v12.1.6-canary.17 2022-05-02 20:27:26 +02:00
Tim Neutkens
20e5fed1cf
Handle styled-jsx in newLinkBehavior codemod (#36628)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-05-02 19:07:14 +02:00
Tim Neutkens
ddba1aab1f v12.1.6-canary.16 2022-05-01 18:58:46 +02:00
JJ Kasper
c838b5f50d
v12.1.6-canary.15 2022-04-29 11:54:57 -05:00
JJ Kasper
244456936b
v12.1.6-canary.14 2022-04-28 13:34:45 -05:00
Tim Neutkens
0b0b5ca75c v12.1.6-canary.13 2022-04-28 11:33:17 +02:00
JJ Kasper
af1d7c94f6
v12.1.6-canary.12 2022-04-27 14:23:09 -05:00
Tim Neutkens
5e3225da0f v12.1.6-canary.11 2022-04-27 18:40:33 +02:00
Tim Neutkens
c232d3856a
Add string children case for newNextLinkBehavior codemod (#36516) 2022-04-27 18:39:56 +02:00
Tim Neutkens
5907e9d394 v12.1.6-canary.10 2022-04-27 17:34:18 +02:00
JJ Kasper
90863c70ea
v12.1.6-canary.9 2022-04-26 15:14:22 -05:00
Tim Neutkens
52816703be v12.1.6-canary.8 2022-04-26 14:15:28 +02:00
JJ Kasper
994f1823ba
v12.1.6-canary.7 2022-04-25 19:22:10 -05:00
Tim Neutkens
489e65ed98
Rework <Link> behavior (backwards compatible) (#36436)
Fixes https://github.com/vercel/next.js/discussions/32233

⚠️ If you're looking at this PR please read the complete description including the part about incremental adoption.

## TLDR:

Official support for `<Link href="/about">About</Link>` / `<Link href="/about"><CustomComponent /></Link>` / `<Link href="/about"><strong>About</strong></Link>` where `<Link>` always renders `<a>` without edge cases where it doesn’t render `<a>`. You'll no longer have to put an empty `<a>` in `<Link>` with this enabled.

## Full context

### Changes to `<Link>`

- Added an `legacyBehavior` prop that defaults to `true` to preserve the defaults we have today, this will allow to run a codemod on existing codebases to move them to the version where `legacyBehavior` becomes `false` by default
- When using the new behavior `<Link>` always renders an `<a>` instead of having `React.cloneElement` and passing props onto a child element
- When using the new behavior props that can be passed to `<a>` can be passed to `<Link>`. Previously you could do something like `<Link href="/somewhere"><a target="_blank">Download</a></Link>` but with `<Link>` rendering `<a>` it now allows these props to be set on link. E.g. `<Link href="/somewhere" target="_blank"></Link>` / `<Link href="/somewhere" className="link"></Link>`

### Incremental Adoption / Codemod

The main reason we haven't made these changes before is that it breaks pretty much all Next.js apps, which is why I've been hesitant to make this change in the past. I've spent a bunch of time figuring out what the right approach is to rolling this out and ended up with an approach that requires existing apps to run a codemod that automatically opts their `<Link>` usage into the old behavior in order to keep the app functioning.

This codemod will auto-fix the usage where possible. For example: 

- When you have `<Link href="/about"><a>About</a></Link>` it'll auto-fix to `<Link href="/about">About</Link>`
- When you have `<Link href="/about"><a onClick={() => console.log('clicked')}>About</a></Link>` it'll auto-fix to `<Link href="/about" onClick={() => console.log('clicked')}>About</Link>`
- For cases where auto-fixing can't be applied the `legacyBehavior` prop is added. When you have `<Link href="/about"><Component /></Link>` it'll transform to `<Link href="/about" legacyBehavior><Component /></Link>` so that your app keeps functioning using the old behavior for that particular link. It's then up to the dev to move that case out of the `legacyBehavior` prop.


**This default will be changed in Next.js 13, it does not affect existing apps in Next.js 12 unless opted in via `experimental.newLinkBehavior` and running the codemod.**

Some code samples of what changed:

```jsx
const CustomComponent = () => <strong>Hello</strong>

// Legacy behavior: `<a>` has to be nested otherwise it's excluded

// Renders: <a href="/about">About</a>. `<a>` has to be nested.
<Link href="/about">
  <a>About</a>  
</Link>

// Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included.
<Link href="/about">
  <strong>Hello</strong>
</Link>


// Renders: <strong onClick={nextLinkClickHandler}>Hello</strong>. No `<a>` is included.
<Link href="/about">
  <CustomComponent />
</Link>

// --------------------------------------------------
// New behavior: `<Link>` always renders `<a>`

// Renders: <a href="/about">About</a>. `<a>` no longer has to be nested.
<Link href="/about">
  About
</Link>

// Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included.
<Link href="/about">
  <strong>Hello</strong>
</Link>

// Renders: <a href="/about"><strong>Hello</strong></a>. `<a>` is included.
<Link href="/about">
  <CustomComponent />
</Link>
```

---


## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Errors have helpful link attached, see `contributing.md`


Co-authored-by: JJ Kasper <22380829+ijjk@users.noreply.github.com>
2022-04-25 22:01:30 +00:00
Steven
94faeec1dd v12.1.6-canary.6 2022-04-22 08:57:41 -04:00
JJ Kasper
b8f7c520da
v12.1.6-canary.5 2022-04-21 10:07:37 -05:00
Tim Neutkens
0e2fd9280a v12.1.6-canary.4 2022-04-19 14:44:21 +02:00
JJ Kasper
e80c48e009
v12.1.6-canary.3 2022-04-16 11:15:20 -05:00
JJ Kasper
9c7311b1a5
v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
JJ Kasper
6907519155
v12.1.6-canary.1 2022-04-14 10:23:58 -05:00
JJ Kasper
bc40c0b530
v12.1.6-canary.0 2022-04-13 12:45:39 -05:00
JJ Kasper
38d17bca0c
v12.1.5 2022-04-12 14:40:16 -05:00
JJ Kasper
9c613aaa66
v12.1.5-canary.7 2022-04-12 13:35:53 -05:00
JJ Kasper
a9d6d9f71a
v12.1.5-canary.6 2022-04-11 16:27:07 -05:00
JJ Kasper
da39e29c27
v12.1.5-canary.5 2022-04-11 12:00:35 -05:00
JJ Kasper
345f5cc351
v12.1.5-canary.4 2022-04-07 11:41:10 -05:00
JJ Kasper
211ce73c87
v12.1.5-canary.3 2022-04-06 13:34:56 -05:00
JJ Kasper
e146168c3b
v12.1.5-canary.2 2022-04-05 18:34:29 -05:00
JJ Kasper
4db8c49cc3
v12.1.5-canary.1 2022-04-04 10:29:32 -05:00
JJ Kasper
3069d4b770
v12.1.5-canary.0 2022-03-31 18:27:09 -05:00
Tim Neutkens
48a3222ccc v12.1.4 2022-03-31 08:58:14 +02:00
JJ Kasper
09ac22ff28
v12.1.4-canary.1 2022-03-30 21:21:00 -05:00
JJ Kasper
2f11413448
v12.1.4-canary.0 2022-03-30 17:28:11 -05:00
Tim Neutkens
7eee27f9ed v12.1.3 2022-03-30 21:49:00 +02:00
Tim Neutkens
bb918fd321 v12.1.3-canary.4 2022-03-30 20:52:45 +02:00
Tim Neutkens
2269f03af0 v12.1.3-canary.3 2022-03-30 18:34:13 +02:00
Tim Neutkens
eef557d4f8 v12.1.3-canary.2 2022-03-30 15:16:01 +02:00