Introduction to Next.js 13 and its news
12/12/2023
10 min min
Bartosz Lewandowski
Introduction to Next.js 13 and its news
12/12/2023
10 min min
Bartosz Lewandowski
Table of Contents
- What is Next.js?
- New Features in Next.js 13
- Why is the new version of Next.js a revolution?
- The Future of Next.js
- Summary
I invite you to discover Next.js 13 – the new version of the popular framework that changes the rules of the game in web development. It's a true revolution for developers and web technology enthusiasts. Learn how new features, such as an improved routing system and the introduction of Turbopack, translate into unprecedented performance and capabilities. Discover the secrets of this innovative platform that opens new horizons in the world of web development. Are you ready for this exciting journey through the latest trends in e-commerce design?
What is Next.js?
Next.js is a framework using the React library, which has gained popularity due to its versatility and performance. The latest version uses the app router, bringing breakthrough changes that make it even more attractive for e-commerce creators. It enables the creation of fast, responsive, and interactive user interfaces. Fast page loading and smooth interaction are essential for providing a good customer experience and potentially higher conversion rates. But what exactly makes it so unique and why do programmers worldwide eagerly reach for it?
Learn more about the basics of Next.js in our guide Setting up a Project with Next.js - A Beginner's Guide.
Development of Next.js – from its beginnings to today
The development of Next.js is a fascinating journey in the world of web technologies, beginning in 2016. This framework gained recognition by enabling easy creation of applications using server-side rendering (SSR - Server-Side Rendering). This method significantly improved the speed and performance of applications, which is crucial in today's dynamic internet world. Moreover, applications created with Next.js are characterized by excellent SEO optimization, allowing them to rank better in search results.
It is worth noting that Next.js has always emphasized simplicity and flexibility, allowing developers to create both static and dynamic websites with easily accessible tools. Over the years, this framework has continuously evolved, introducing new features and integrations that allowed for the creation of even more advanced and complex web applications.
A key moment in the evolution of Next.js was the introduction of features such as Static Site Generation (SSG) and Automatic Code Splitting, which significantly improved application performance. Another important feature of Next.js is its integration with various technologies and tools, such as React, Webpack, and Babel, making it even more flexible and accessible to a broader range of programmers.
Why is Next.js the choice of many developers?
Next.js has gained popularity among developers due to several significant advantages that make it a choice for many diverse web projects.
Firstly, the versatility of Next.js is one of its greatest assets. This framework allows developers to create both static websites (SSG - Static Site Generation) and dynamic server-rendered applications (SSR - Server-Side Rendering). This flexibility makes Next.js suitable for a wide range of projects – from simple blogs and portfolio sites to complex e-commerce platforms and corporate applications.
Secondly, Next.js is based on React, one of the most popular libraries for creating user interfaces. This means that developers who are already familiar with React can easily transition to Next.js, leveraging their existing experience and knowledge. Moreover, integration with React allows for the creation of interactive and responsive user interfaces using components, which significantly speeds up the application development process.
Another advantage of Next.js is its built-in performance and SEO optimizations. Thanks to automatic code splitting and image optimization, applications created in Next.js load faster, resulting in a better user experience and higher search engine rankings.
Next.js also offers support for server-side rendering and static site generation, which are crucial for SEO and performance improvement. SSR allows for faster page loading, which is especially important for mobile users and those with slower internet connections. On the other hand, SSG enables page generation during the application build process, ensuring lightning-fast loading times.
Furthermore, Next.js is continuously developed and supported by an active community and the company Vercel, ensuring regular updates, new features, and support. The availability of rich educational resources, documentation, and ready-to-use examples also greatly facilitates learning and implementing this framework.
New features in Next.js 13
Next.js 13 introduces a range of new features and improvements that significantly impact the way applications are created. But what exactly does the new version have to offer, and what are its greatest assets?
New app/ Directory
The app directory in Next.js 13 introduces a new method for organizing files and directing functionality in the application. This is a significant improvement over previous versions of Next.js, which primarily relied on the pages directory for routing. Here are some of the key files you can place in the app directory and their uses:
- page.tsx: This file is responsible for rendering a specific page in the application. Its name and location in the app directory determine the URL path it is assigned to. For example, app/about/page.tsx corresponds to the URL yourdomain.com/about. In this file, you define the main content of the page.
- layout.tsx: The layout file defines a common layout for a group of pages or the entire application. You can create nested layouts, where each subdirectory in app can have its own layout.tsx, encompassing both that page and all its subpages. Layouts can retain their state during transitions between pages.
- error.tsx: This file is used for error handling in the application. You can use it to define a custom error page that will be displayed when the application encounters an unexpected error.
- loading.tsx: The loading.tsx file acts as a <Suspense> component for a given route section. It defines what should be displayed while the actual page content is loading, which is especially useful for asynchronous operations.
- not-found.tsx: This is a special file used to define a custom page for a 404 - Not Found error. It allows you to create a more personalized page for users trying to access a non-existent path in your application.
- template.tsx: It serves a role similar to the layout file, wrapping each child layout or page. The difference is that unlike layouts, which are persistent across different routes and retain their state, template.tsx creates a new instance for each child element during navigation. This means that with each transition to a new page, template.tsx is refreshed, creating a new instance of its interior without retaining the previous state. This approach is useful when you want each page or component to have its own unique context and state that is not preserved between navigations.
Turbopack - the successor to Webpack
Next.js 13 is built on Turbopack, a native compiler written in Rust, which is the successor to Webpack. Here are some key points about Turbopack:
- Performance: One of the main goals of Turbopack is to increase the performance of the application build process. It is significantly faster than Webpack, which greatly reduces the time needed for code compilation and packaging, especially in large projects.
- Startup Time Optimization: Turbopack has optimized startup time, which is particularly beneficial during application development when developers frequently start and recompile the project.
- Faster Updates: In addition to fast startup time, Turbopack also offers much faster updates during development. This means that changes in the code are reflected more quickly in the application, improving the overall developer workflow.
- Alpha Technology: It should be noted that Turbopack was introduced as an alpha version, which means it is still under development and refinement. Nonetheless, it already shows promising results in terms of performance improvement.
- Change in Build Process: The introduction of Turbopack marks a change in how Next.js handles the application build process. It offers new possibilities and potentially changes the way developers work on projects in Next.js.
Improved rendering in Next.js 13
The new way of writing Server Components in Next.js 13 brings several significant changes and improvements that affect how applications are created and rendered:
- Server-side Rendering: The core idea of Server Components is that they are rendered on the server side, meaning the JavaScript code for these components is not sent to the browser. This reduces the size of the bundle sent to the client, leading to faster page loading.
- Automatic Differentiation: In Next.js 13, the system automatically differentiates between Client Components and Server Components. Components in the app folder are by default treated as Server Components.
- Use of "use client" Directive: If a component contains client-side logic, such as useState or useEffect hooks, it must be marked with a special "use client" directive at the beginning of the file. This indicates that this particular component will be treated as a Client Component.
- Data Sending Optimization: Thanks to server-side rendering, data is processed on the server, reducing the amount of processing required on the client side. This significantly improves performance, especially on devices with limited computing power.
- Streaming and Suspense: Next.js 13 uses streaming and Suspense features to optimize component loading. Components can be loaded gradually, allowing for faster content display and better loading state management.
- Reduction of Client Code: Since many components are now rendered on the server side, the amount of JavaScript code needed for execution on the client side is significantly reduced. This reduces the browser's load and improves overall performance. We elaborate on this topic in the post Rendering in Next.js - server and client components in a nutshell.
Faster data fetching
Next.js 13 introduces several important changes regarding data fetching. Here are the main points to know:
- Extended Fetch on the Server: Next.js extends the native Fetch API interface, allowing you to configure caching and revalidation behavior for each fetch request on the server. You can use fetch with async/await in server components, route handlers, and server actions.
- Data Caching: By default, Next.js automatically caches the values returned by fetch in the Data Cache on the server. This means that data can be fetched at build time or request time and then reused for each data request.
- Data Revalidation: Revalidation is the process of clearing the Data Cache and fetching the latest data. It is useful when data changes, and you want to ensure you are displaying the most up-to-date information. Revalidation can be time-based (automatically revalidating data after a specified time) or on-demand (manually revalidating data based on an event).
- On-Demand Revalidation: You can revalidate data on demand using paths (revalidatePath) or cache tags (revalidateTag). Next.js offers a cache tagging system for fetch requests, allowing you to revalidate all cache entries associated with a given tag. You can read more about data fetching in our dedicated post Data Fetching in Next.js - A Beginner's Guide.
Server Actions
Server Actions in Next.js 13 are asynchronous functions executed on the server, used in both server and client components for handling form submissions and data mutations. They can be defined in two ways:
- Directly in the Server Component: By adding the "use server" directive at the beginning of an asynchronous function.
- In a separate file: To enable reuse, Server Actions can be defined in a separate file by adding the "use server" directive at the beginning of the file. These actions can then be imported into both server and client components.
Applications of Server Actions include:
- Form handling: They can be invoked using the action attribute in the <form> element. Server components allow form submission even when JavaScript is not loaded or disabled. In client components, forms invoking Server Actions queue submissions until the client loads.
- Data mutation and revalidation: Server Actions allow for data modification and revalidation.
- Error handling: It is possible to easily manage both success and error messages.
- Updates with useOptimistic: They allow updating the user interface before a Server Action completes, which can provide a smoother user experience.
Additional Features:
- Setting and reading cookies: Server Actions enable cookie management during the action's execution.
- Advanced form customization: The ability to customize the behavior, layout, and appearance of forms according to application requirements, including advanced CSS styling.
- Handling complex data structures: Server Actions are not limited to simple forms; they can effectively handle complex data structures, including objects and arrays.
Server Actions are an integral part of Next.js architecture and work with its caching and data revalidation system. As a result, when an action is invoked, Next.js can return both the updated user interface and new data.
Why is the new version of Next.js a revolution?
Next.js 13 is considered revolutionary due to a range of innovations that significantly impact the way web applications are created. First and foremost, it introduces a new architecture known as the "app directory," which simplifies project structure and allows for more intuitive management of components and paths. This represents a major shift compared to the traditional page-based approach.
Server Components
Next.js 13 introduces significant improvements in handling Server Components, marking an important step in the further development of the framework. This feature allows React components to render on the server side, which has a substantial impact on performance and user experience.
Server Components in Next.js 13 enable the separation of heavier, more resource-intensive components from those rendered on the client side. In practice, this means that certain parts of the application can be processed on the server, reducing the load on the client's browser. This directly impacts page load speed, which is especially beneficial for users with slower internet connections or less powerful devices.
Using Server Components in Next.js 13 also allows for optimizing network bandwidth usage. Since part of the code is executed on the server, less data needs to be sent to the client's browser. This translates to faster page load times and reduced data usage, which is important, especially in the context of mobile internet users.
Additionally, Server Components allow for better separation of business logic from the user interface. These components can contain logic related to database access, authorization, or computations, which traditionally would require the use of additional APIs or server functions. As a result, developers can create more efficient and secure applications, reducing the risk of security issues on the client side.
Image Optimization
Image optimization in Next.js 13 is one of the key improvements, having a significant impact on the performance of web applications, especially in the context of mobile devices. In today's world, where loading speed and optimization are essential for providing a good user experience and search engine ranking, these changes are particularly important.
Automatic image optimization in Next.js 13 involves intelligent scaling, compressing, and converting images to formats that are more efficient in terms of size without losing quality. This significantly reduces page load time since images often constitute a significant portion of the data loaded by an application.
Additionally, Next.js 13 introduces support for modern image formats, such as WebP, which offer better compression while maintaining high image quality compared to traditional formats like JPG or PNG. Using these formats can significantly reduce the size of image files, which is crucial for mobile users and those with slower internet connections.
Next.js 13 also implements lazy loading of images, meaning that images are loaded only when needed (for example, when the user scrolls down and approaches a given image). This approach prevents all images on the page from loading at once, which can significantly burden the browser and slow down page load time.
Performance and SEO
In Next.js 13, the focus on performance and SEO is particularly important as the framework continues to introduce and refine various optimization techniques that are crucial for modern web applications.
Lazy Loading: The introduction and refinement of lazy loading is one of the important aspects of Next.js 13. This technique allows for loading resources, such as images and scripts, only when they are needed, typically when the user scrolls down and approaches a given element. This significantly reduces the initial page load time, which is especially important for mobile users and those with slower internet connections.
Prefetching: Another important feature is prefetching, which involves loading resources in advance that may be needed in the future. As a result, when the user clicks a link or navigates to another part of the page, most resources are already loaded, significantly speeding up load times and improving the overall user experience.
SEO Optimization: Next.js 13 also continues to focus on search engine optimization (SEO). Thanks to techniques such as SSR (Server-Side Rendering) and SSG (Static Site Generation), pages are better indexed by search engines. This, combined with faster load times and better content accessibility, contributes to better search rankings.
Overall Performance Improvement: A comprehensive approach to performance, including image optimization, script and resource management, and effective cache usage, significantly improves the speed and responsiveness of applications built with Next.js 13. These improvements have a direct impact on user experience, which is crucial in a world where expectations for speed and smoothness of web pages are increasingly high.
The future of Next.js
Next.js continues to evolve, and its creators, the company Vercel, are constantly working on introducing new features and improvements. So, what are the plans for the future, and in what direction will this framework develop?
Planned innovations and improvements
The creators of Next.js are announcing further improvements in both performance and flexibility of the framework. We can expect new tools and features that will make it even easier to create advanced web applications.
Vision for the future of Next.js
The vision for the future of Next.js is not only technological development but also support for the developer community. The framework's creators plan to work closely with developers to adapt Next.js to the ever-changing market needs and trends in web application development. Want to learn more about optimization? Check out our blog post Optimizations in Next.js - How to Speed Up Your Site?.
Summary
Next.js 13 introduces many significant changes and improvements that open up new possibilities for developers. From an improved routing system to server actions – these changes positively impact the application development process, their performance, and SEO. Next.js continues to evolve, and its future looks very promising.
Frequently Asked Questions
- How does Next.js 13 differ from previous versions? Next.js 13 introduces an improved routing system, data fetching, rendering, and tools that differ from those available in previous versions.
- How do the new features in Next.js 13 affect application performance? Improvements in Next.js 13, such as better path management and rendering optimizations, translate to faster loading and better search engine positioning.
- What are the development plans for Next.js? Further improvements in performance and flexibility of the framework are planned, as well as collaboration with the developer community.
- For which projects is Next.js best suited? Next.js is an ideal choice for many different projects - from simple websites to complex online stores.
Bartosz Lewandowski
CEO