Commit graph

680 commits

Author SHA1 Message Date
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
Tim Neutkens
d876667aef v12.1.3-canary.1 2022-03-30 14:31:26 +02:00
JJ Kasper
88a7e58406
v12.1.3-canary.0 2022-03-28 18:33:56 -05:00
JJ Kasper
50be7cc17d
v12.1.2 2022-03-28 13:56:02 -05:00
JJ Kasper
825425494a
v12.1.2-canary.1 2022-03-28 13:08:22 -05:00
JJ Kasper
086cf91ada
v12.1.2-canary.0 2022-03-26 22:32:33 -05:00
JJ Kasper
79a8651bc4
v12.1.1 2022-03-25 11:45:19 -05:00
JJ Kasper
a5540e8fda
v12.1.1-canary.18 2022-03-25 10:32:52 -05:00
CommanderRoot
db2567b01b
chore: replace deprecated String.prototype.substr() (#35421)
.substr() is deprecated so we replace it with .slice() which works similarily but isn't deprecated
Signed-off-by: Tobias Speicher <rootcommander@gmail.com>

Co-authored-by: Steven <steven@ceriously.com>
2022-03-24 17:49:38 -04:00
Steven
78831c3c84 v12.1.1-canary.17 2022-03-22 09:00:31 -04:00
JJ Kasper
92da36f1e0
v12.1.1-canary.16 2022-03-21 18:33:48 -05:00
Jiachi Liu
f62766f364 v12.1.1-canary.15 2022-03-18 11:25:16 +01:00
JJ Kasper
9129bb9a05
v12.1.1-canary.14 2022-03-17 12:51:26 -05:00
JJ Kasper
52e34cede8
v12.1.1-canary.13 2022-03-16 18:46:19 -05:00
JJ Kasper
4c6443ba1c
v12.1.1-canary.12 2022-03-16 11:23:01 -05:00
Steven
6611bfd31c v12.1.1-canary.11 2022-03-14 10:26:50 -04:00
JJ Kasper
d7deb852bd
v12.1.1-canary.10 2022-03-11 08:53:10 -06:00
Steven
ceb7bfb29d v12.1.1-canary.9 2022-03-10 08:22:40 -08:00