Next.js 14 is the latest version of the popular React framework, designed to improve the developer experience and enhance the performance of web applications. This release introduces several new features and improvements, making it the biggest release of Next.js to date.
Next.js 14 is primarily used for building server-side rendered and static web applications. It provides a robust set of features such as pre-rendering, server side rendering, static site generation, and much more. These features make it an excellent choice for developers building highly performant, SEO-friendly web applications.
1// Example of a simple Next.js component 2function HomePage() { 3 return <div>Welcome to Next.js!</div> 4} 5 6export default HomePage 7
One of the significant improvements in Next.js 14 is the turbocharged compiler. The Next.js team has been working tirelessly to improve local development performance in both the Pages and App Router. The Rust-based compiler is expected to stabilise soon, promising faster local server startup and code updates.
1// Running Next.js with turbo mode 2next dev --turbo 3
Next.js 14 introduces a simplified way of handling forms and mutations. In previous versions, developers had to create API routes to handle form submissions manually. Now, with the introduction of Server Actions, developers can define a function that runs securely on the server and can be called directly from React components.
1// Example of a Server Action in Next.js 14 2async function create(formData: FormData) { 3 'use server'; 4 const id = await createItem(formData); 5} 6
Server Actions are a new feature in Next.js 14 that simplifies the developer experience of authoring data mutations. They allow developers to define functions that run securely on the server and can be called directly from React components. This feature is built on web fundamentals like forms and the FormData Web API, making it familiar to developers using server-centric frameworks.
1// Example of using a Server Action in a form 2<form action={create}> 3 <input type="text" name="name" /> 4 <button type="submit">Submit</button> 5</form> 6
Server Actions are deeply integrated into the entire App Router model. They offer a variety of functionalities such as revalidating cached data, redirecting to different routes, setting and reading cookies, handling optimistic UI updates, and more. This deep integration allows for a more streamlined development process and improved performance of web applications.
1// Example of using Server Actions for revalidating cached data 2revalidatePath('/path/to/data') 3
Next.js 14 offers a preview of Partial Prerendering, a compiler optimization for dynamic content with a fast initial static response. This feature builds on a decade of research and development into server side rendering (SSR), static-site generation (SSG), and incremental static revalidation (ISR).
1// Example of a page with Partial Prerendering 2export default function Page() { 3 return ( 4 <main> 5 <header> 6 <h1>My Store</h1> 7 <Suspense fallback={<CartSkeleton />}> 8 <ShoppingCart /> 9 </Suspense> 10 </header> 11 <Banner /> 12 <Suspense fallback={<ProductListSkeleton />}> 13 <Recommendations /> 14 </Suspense> 15 <NewProducts /> 16 </main> 17 ); 18} 19
The motivation behind Partial Prerendering is to simplify the existing model without introducing new APIs for developers to learn. It aims to provide the speed and reliability of static rendering while also supporting fully dynamic, personalized responses. This feature requires no new APIs to learn, making it an attractive option for most developers.
1// Example of a Suspense boundary for Partial Prerendering 2<Suspense fallback={<ProductListSkeleton />}> 3 <Recommendations /> 4</Suspense> 5
Partial Prerendering in Nextjs 14 is built on React Suspense. It works by generating a static shell based on your Suspense boundaries. The fallback from React Suspense is prerendered, and the dynamic components are then replaced with the static shell when a request is made.
1// Example of a Suspense boundary with a fallback 2<Suspense fallback={<CartSkeleton />}> 3 <ShoppingCart /> 4</Suspense> 5
With Partial Prerendering enabled, a static HTML shell is immediately served when a request is made. This shell is based on your <Suspense />
boundaries and the fallback from React Suspense is prerendered. Dynamic components, like those reading cookies or showing a banner based on the user, are then streamed in as part of the same HTTP request as the static shell. This eliminates the need for extra network roundtrips.
1// Example of a static HTML shell 2<main> 3 <header> 4 <h1>My Store</h1> 5 <div class="cart-skeleton"> 6 <!-- Hole --> 7 </div> 8 </header> 9 <div class="banner" /> 10 <div class="product-list-skeleton"> 11 <!-- Hole --> 12 </div> 13 <section class="new-products" /> 14</main> 15
Partial prerendering is currently under active development. More updates and improvements are expected in the upcoming minor releases of Next.js. The goal is to enhance the developer experience further and improve the performance of web applications.
1// Stay tuned for more updates on Partial Prerendering 2console.log('Stay tuned for more updates on Partial Prerendering'); 3
Next.js 14 introduces several improvements to metadata. These improvements aim to provide a smoother user experience by preventing the page from flickering due to changes in theme color or layout shifts due to viewport changes. Only a few metadata options block, ensuring non-blocking metadata will not prevent a partially prerendered page from serving the static shell.
1// Example of using the new metadata options 2const metadata = { 3 viewport: 'width=device-width, initial-scale=1', 4 generateViewport: true 5}; 6
In Next.js 14, blocking and non-blocking metadata have been decoupled. Non-blocking metadata will not prevent a partially prerendered page from serving the static shell. This improvement enhances the user experience by ensuring the page doesn't flicker or shift layout due to metadata changes.
1// Example of non-blocking metadata 2const metadata = { 3 nonBlocking: true 4}; 5
Next.js 14 introduces new metadata options to replace the deprecated ones. The new options include viewport and generateViewport. All other metadata options remain the same. Developers can start adopting these new APIs today. The existing metadata options will continue to work until they are removed in a future major version.
1// Example of using the new viewport metadata option 2const metadata = { 3 viewport: 'width=device-width, initial-scale=1' 4}; 5
In conclusion, Next.js 14 brings many new features and improvements that enhance the developer experience and the performance of web applications. With features like Server Actions, Partial Prerendering, and improved metadata handling, Next.js 14 is a powerful tool for front end web developers.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.