Summary
With Vite taking over the JavaScript world, we must admit, the Next.js developer experience isn't the best in comparison with Astro's or Vue's. Reloads are slower, startups are slower... Even though Turbopack might change that completely, the stable version is still some time away. That's why we were rejoiced to learn the new version starts the local server 22% faster, Fast Refresh is 29% faster, and memory usage is 40% smaller, when using next start.
One user even reported 87-92% faster compilation (!).
The image component we know and use got a bit of an improvement. You may now take advantage of image optimization without using the <Image /> component by using the new, experimental, unstable_getImgProps() function.
The new functionality will be welcome with open hands by web developers, because thanks to the new, experimental, functionality, websites should be much faster.
One of the key features that make Next.js applications incredibly fast is its advanced caching mechanisms. Let's dive into how Next.js caching works and how it can automatically boost your app's performance.
Caching in Next.js is designed to enhance your application's performance and reduce operational costs by storing rendering work and data requests. This means that instead of fetching data or rendering pages from scratch every time a user visits, Next.js can retrieve the stored version, making the process much faster.
By default, Next.js tries to cache as much as possible. This means:
For instance, when a user visits a statically rendered route for the first time, the route is fetched and stored. On subsequent visits, instead of rendering the route again, Next.js serves the cached version, making the page load incredibly fast.
We had to wait for a while, but the new app router is now stable. That means that:
...are now deemed stable enough for the wider use. Now that we know that, we encourage companies of all sizes, who already are using Next.js, to give the the new router a shot. If your company does not already use to at least consider giving it a try. The server components are a massive change, that could enable you to build faster apps.
Be careful, however. If, for any reason you need to include Client components, then the JavaScript bundle can be bigger than it used to be with the pages router.
Sometimes, it's impossible to not opt into the client component. If you so much as need to use "useState", react to something, etc, then you must use "use client." Then you have to wrap your head around how to fetch data from the parent component, except you can't pass functions from the parent to the child, if the parent is a server client, and the child is a client component.
Server Actions are here to make client components better. You will be able to call server code from client components. How?
The possibilities are endless with this one, although some are concerned.
Properly setting the metatags for SEO was challenging if you used the new, experimental way to build apps. You had to create special files (head.js) which was rather cumbersome. The new approach revolves around exporting objects (for static metadata) or functions (for dynamic metadata).
That is a major quality of life improvement for developers working on projects of all sizes.
One missing piece that the new way of writing Next apps was missing was API routes. They allowed you to use Vercel's framework as a truly full-stack framework. You kept intensive tasks running on the server-side, keeping only the necessary bits on the client.
Of course, you could create API routes in the old pages folder, but that was not an ideal solution. Sure it worked, but the new router not supporting one of the most important features was just not ideal to say the very least.
The Next.js team delivered in the update.
Not only are the API routes now available, but they even updated them.
Now, you may only respond to one HTTP method, easier than before. Furthermore, the "Route Handlers" are based on what we all, web developers, know: the standard Web APIs such as Request and Response.
Here's how you would set cookies:
Overall it's a big improvement. Here's how you would check for a POST request before:
A big quality of life improvement.
You now get statically typed links in the app directory. What does it mean? Imagine you have a route such as "/marketing-pricing." You use a link in your app, but instead of correctly typing the link, you write "/marketing-price." You deploy to production, aaaaand... Google complains to you that you have a broken link. Oops.
What happens now is that you would actually get an error, thus saving you from making this silly mistake.
Maybe it's not a big deal, however it's a great addition to the framework.
Heads up: if you are not already using TypeScript, you will have to start using it to benefit from this.
Turbopack, the newest bundler, and successor to webpack did not start off too strong. Too many things were missing. That's why, since the last release, the Next.js team added these features:
Additionally, it is now in the Beta status.
They say the most difficult aspect of programming is caching... It's perhaps good, then, that you get an easy caching mechanism out of the box, that's easy to understand, and make use of. Most importantly, the new mechanism controls the caching rules on the side of the app, which differes a bit from how caching usually works. Usually it's the APIs that dictate for how long you may cache.
On the 25th of October, people all around the world tuned in to watch Next.js conf – the conference where developers found out what the developers of the most famous React framework prepared for them. All who turned up for the stream or the in-person event witnessed the presentation of the next major version of the framework – Next.js 13. The view was quite something – the new version is much different in many ways than its predecessor. It changed so much in so many ways – the company itself claimed the update will be the biggest change to Next.js ever. Was it? We watched the full conference for you, but if you want to watch it yourself first, you may find the video below. Otherwise, read on.
Lastly, Vercel, called the new version “dynamic without limits.” This perhaps refers to the removed limitations connected to the app sizes. Previously, you had to choose carefully what you enabled your users to do, and what not to do, as new possibilities often meant more and more data users had to pull. Now, that worry is not at the back of developers' head anymore: websites will remain fast unless you make them slow, and heavy on purpose.
Reinventing React was the topic of recent months. The topic? Fetching data and removing the tons of JavaScript we ship to users around the world. For one, it made users’ experience much worse. Moreover, it had an adverse effect on Earth – all data sent across the internet = emitted carbon (perhaps reducing emissions is just a side-effect, though it’s sure a nice outcome). Another problem was the problem of waterfall fetching. In short, instead of fetching data in parallel, browsers fetched data in sequence (one after another).
The first React framework (or the first mainstream framework) to solve the problem of both waterfalls, and too much client-side JavaScript (both at the same time) was Remix. It became an internet hit. The creation of the React Router team was web-standards first, and enabled developers to create React websites that were lighter and rendered quicker on top of that. Another selling point was the unique approach to fetching data, but that’s another story, for another article.
Remix’s approach was picked up by the Next.js team, and in May 2022, they have announced requested feedback on their new proposal: “Layouts”. The change was called the biggest one to date, and it sure was. A big portion of why developers all around the globe waited
The groundbreaking features that Next 13 brings are entirely opt-in. We are not sure for just how much longer the legacy, and the new approach will be supported at the same time, though at the time of writing they are.
Putting your code in the pages folder is the old way of writing full-stack apps you know and are used to. Nothing changes about the core process here, and if you want to stick to the old ways, then you may skip this section, and move to the “Turbopack” one.
If you are ready for the new (and slightly untested), put your code in the app directory, and read on. Vercel promised a dramatic reduction in client-side JavaScript... and they delivered! Previously, a simple “Hello, World” app would require sending over 70kB of data just to have a div. Now, if you don’t need interactivity, the only thing we get is HTML, and CSS. No JS WHATSOEVER. Of course, if you need interactivity, JS will appear, and we are not talking about the full React + Next package.
The worst part of the web experience is waiting for something to load while having nothing to look at. One talk during the presentation of React 18 already mentioned that and touched on the importance of introducing loading states with Suspense.
Next.js 13 delivers an improved version of the mechanism. You may now create a function called “Loading” that users will see until data fetching for your page is complete.
The upgraded Layouts solve the problems of all Next releases up to date. Previously, creating reusable foundations for pages was slightly problematic. One had to use a quick and a somewhat improvised way to go about things, but not anymore. We may now create a file called “layout.tsx” that components will use to build a user interface.
Server Components enable massive savings in the bundle size. They are a bit of a shakeup in the front-end world as well. React used to be a client-side library only. As we wrote before, that changed recently, with the addition of this new type of a building block. Next.js now uses them, and uses them by default to help you make faster apps.
The new components might make GraphQL and other libraries even more popular. How so? GraphQL is heavy. @apollo/client is ~42kb by itself (minified; version 3.7.1). If you now keep everything on the back-end, then no single byte is included in the client bundle. For smaller teams there is a benefit as well. You may now even call Prisma directly inside your components (even though you don’t really want to do that).
There are a few drawbacks, however.
Many third-party components will have to be wrapped in a component that will tell Next that it’s a client component. It’s a bit of additional work; busy work that nobody likes. You are not able to add any interactive elements, too. To do that, you must use Client Components.
Furthermore, calling fetch from Client Components is not supported currently. As the docs say:
fetch is currently not supported in Client Components. Although it is possible to fetch data in Client Components by, for example, using a third-party library such as SWR, you might run into performance issues when doing so.
Traditionally, users had to wait for all the page to generate before they received any of it. Not anymore. Server will now send the UI to the client in small bits, as they get created. This means larger chunks will not block the lighter ones. Of course, this feature is currently supported only in the app directory, and it does not seem as if it might change.
People with fast Wi-Fi or with access to a good internet connection aren’t going to benefit from this new addition as much as the ones with slower connections. There are more people like this than you would have thought. It’s great that we may now improve the experience for them – they may be customers you could have had if your webpage loaded faster.
Turbopack is the successor to Webpack. Vercel, having hired Webpack’s creator, had the possibility to write a faster, better tool than Webpack. It’s much faster, that’s certain, though it’s not all great. For one, you now have to author plugin using Rust, instead of JavaScript. The subset of JavaScript developers having working knowledge of the systems language is much, much smaller, than the set of JS devs.
That’s a trade-off, though. We now have a tool that’s lightning fast and will heavily decrease build times for large apps. The change is aimed at enterprises. For small teams, the change is nice, but won’t change much, realistically. Enterprises will now build their apps in a much shorter time and having to hire Rust developers is not a big deal for them.
Furthermore, developers’ favorite, Tailwind CSS, does not really work with Turbopack now. It was a big misstep from the bundler’s creators, though we don’t think it won’t be remedied soon.
Turbopack is blazingly fast. We are not sure whether the trade-off of faster vs losing many existing plugins is worth it in the end. It also remains a question why the development team behind the framework did not choose to use Vite. Surely, some of the existing Webpack plugins will be made compatible with Turbopack, though Vite has a sea of them already, AND it’s blazingly fast as well. A bundler monopoly is not something anybody wants, on the other hand.
There are new packages coming your way, that will make your images & fonts load faster, as well as some improvements to how links work in Next.js 13. They work the same in the app and the pages directory.
The new helper is designed to make pictures load even faster. This release brings us some improvements. Firstly, it now helps you to speed up the Largest Contenful Paint (LCP). LCP is the largest element one sees on a page. If it’s an image, then Next.js will tell you to add a special priority attribute to the picture, so the framework can prioritize the loading of said block.
From other noteworthy additions, now the alt attribute is required, which means you will have to add it to all your images. Quite frankly, you should have done that already, so that is a non-issue.
Fonts are some of the heaviest things to load on a site. Vercel teamed up with the Chrome team to deliver the best experience. The library allows you to load fonts from Google (downloaded at build time) or load local fonts, from your server. In case you worry loading fonts from the company from Mountain View will expose your users, worry not. Users don’t connect to the servers of the search engine company at all.
The solution for enabling client-side navigation has changed a bit. As the documention says:
The <Link> Component no longer requires manually adding an <a> tag as a child. This behavior was added as an experimental option in version 12.2 and is now the default. In Next.js 13, <Link> always renders <a> and allows you to forward props to the underlying tag.
Setting a dynamic Open Graph (OG)/Twitter image used to be a challenging task. There was a repository on GitHub, courtesy of Vercel, where the task was made significantly easier for you. All you had to do was clone the code, make changes, deploy the service, and, in a few hours, you were done. Now, you may be done in less than an hour.
The way it works is you create a React component, which is then transformed into an image. Sounds simple, and it is. There are some limitations as to what CSS properties are supported, though don’t worry about that one bit. The possibilities are still wide open for you to create stunning OG images
We will start with the one breaking change everybody is happy to see. INTERNET EXPLORER ISN’T SUPPORTED ANYMORE 🎉.
Further, what you need to keep in mind is:
To sum up, the benefits of Next.js 13 are worth the upgrade. Furthermore, the additional packages, such as @vercel/og or @next/font are worth adopting along with the new app directory. Of course, you don’t need to migrate all pages right away. You may upgrade incrementally, one page after another. This was always Vercel’s philosophy with the framework. This is perhaps the reason why the biggest companies feel safe using Next.js in production. If you want to have your app migrated to Next.js 13, want a new site running on the most popular React framework, or want to have a chat about building your web app, feel free to contact us here.