Next.js is a popular framework for building React applications. With the release of Next.js 14, there are some new features and improvements that can make your life as a developer easier. In this tutorial, I will share 5 tips and tricks to help you get the most out of Next.js 14.
- Use the new image component for optimized image loading
One of the most exciting features of Next.js 14 is the new image component. This component allows you to easily optimize images for the web by automatically generating several sizes and formats of an image and serving the appropriate one based on the device’s screen size and resolution.
To use the new image component, simply import it from ‘next/image’ and pass the src and alt props. Next.js will take care of the rest:
import Image from 'next/image';
function MyComponent() {
return (
<Image src="/my-image.jpg" alt="My Image" />
);
}
By using the new image component, you can ensure that your images are optimized for the web and load quickly on all devices.
- Enable the experimental feature flag for faster incremental static regeneration
Next.js 14 introduces experimental features that can improve performance and developer experience. One such feature is the experimental feature flag for faster incremental static regeneration.
To enable this feature flag, add the following line to your next.config.js file:
module.exports = {
experimental: {
faster: true,
},
};
By enabling the experimental feature flag for faster incremental static regeneration, you can take advantage of performance improvements that reduce build times and improve the developer experience.
- Use the new built-in middleware feature for custom server logic
Next.js 14 introduces a new built-in middleware feature that allows you to define custom server logic without the need for an external server. This can be useful for implementing custom authentication or API routes.
To use the new built-in middleware feature, create a ‘middleware’ directory in your pages folder and add a JavaScript file with the following structure:
export default function middleware(req, res, next) {
// Custom server logic here
}
By using the new built-in middleware feature, you can simplify your codebase and add custom server logic without the need for an external server.
- Take advantage of the new script and link components for enhanced performance
Next.js 14 introduces new script and link components that allow you to defer loading of scripts and stylesheets until they are needed. This can improve performance by reducing the initial load time of your application.
To use the new script and link components, import them from ‘next/script’ and ‘next/link’ and pass the appropriate props:
import Script from 'next/script';
import Link from 'next/link';
function MyComponent() {
return (
<>
<Script src="/my-script.js" />
<Link href="/my-stylesheet.css" />
</>
);
}
By using the new script and link components, you can optimize the loading of scripts and stylesheets in your application for enhanced performance.
- Leverage the new static site generation capabilities for faster page loading
Next.js 14 includes improved static site generation capabilities that can significantly improve page loading times. By pre-rendering pages at build time, you can serve static HTML files that load quickly and provide a better user experience.
To take advantage of the new static site generation capabilities, enable the ‘static’ option in your next.config.js file:
module.exports = {
target: 'experimental-serverless-trace',
};
By enabling static site generation in Next.js 14, you can improve the performance of your application and provide a faster and more seamless user experience.
In conclusion, Next.js 14 offers several new features and improvements that can make your life as a developer easier. By using the tips and tricks outlined in this tutorial, you can take advantage of the latest capabilities of Next.js 14 and build high-performance React applications with ease.
Helpful. Thank you very much.
THank you so much
I came for the 5 tips and stayed for the blue balls.
To anyone experiencing hydration errors using next themes , the trick is to put it under the body not above, took me a minute.
npm run dev ed
Are you related to Jeff at CraftComputing? The resemblance is uncanny.
where is ur first channel?
You got nice hints, it was some plugin? I
as I know next js is using server by default so we don't need to type 'use server'
no need to import images from public folder, if the image is under the /public folder directly you can just type the name of the image and its extension.
src="img.jpg"
if it's under subfolder in public let's assume it's /media folder
src="media/img.jpg"
you need import only if the image isn't in public holder.
—
Thanks for the amazing tutorial.
i was confused about this as well, but you don't necessarily need to mark a component async for it to be a server component. It will be a server component by default unless marked client. Marking it async is only needed if you're gonna use await within it.
Great video but your hat was missing! But I'm still struggling with the next js font
what would i do for tokens? would i handle that in the route handler when i need the cookies?
Suppose we have a table in a Next JS page and the table has a filter (client) component with interactivity. When we hit Apply, how do we pass the filter values from the client child to the server parent component. (The parent fetches the data and renders the table). I can see many suggestions to pass the data as params or queryParams using revalidate or nav methods of next. That works fine for a simple values like ID or search params. For example, if it is not just an ID but an object (advanced filter similar to the one in Amazon), should we just pass the entire object via the query params and make the URL bloated with so many values or are there any alternate way to communicate from client child (filter) to server parent component (where data is fetched) ??
It's safe to include public API endpoints with NEXT_PUBLIC if they are meant to be publicly accessible and don't require any form of authentication or sensitive data.
Hi Ed, legend video as always!
At 15:28, what's the extension that shows you the import size? Thank you for the tips in this video.
Thanks baby reindeer
sent from iphon
waddup pewds
cool