Tools of the trade
Out of the box, Next.js comes with several tools that help us optimise and increase the performance of our sites.
Here are three of our favourite ones.
Next/Image
First and probably simplest, the Next/Image component replaces the humble <img /> element, and processes your media assets, ensuring you always serve the best possible image to the user.
It automatically optimises images, converts them into modern formats (WebP or AVIF), and generates appropriate sizes for different responsive levels. It also removes any layout shift, giving your site a massive speed boost.
It’s a must-have for any Next.js site. [Get Next/Image]
Next/Script
For us, the biggest speed bumps on websites are the third party scripts.
Picture a team of proud developers who have just launched a perfectly performing site, only to have Google Tag Manager or a cookie banner swoop in to ruin their day.
A sad story, I’m sure we can all agree.
We know these scripts are essential for marketing departments and user experience testing. We just wish there were an easier way to add them onto a site.
Most of the time, clients add these scripts directly to the site in Google Tag Manager, and don’t try to optimise the load process, so the scripts start running the moment a user lands on a page, increasing initial loading time and making the overall site seem sluggish.
Luckily, Next/Script helps alleviate a lot of these issues.
You can add it to a specific route or globally to the _app file to ensure the file loads on every page. Next.js ensures the script is only loaded once, and it even comes with a handy strategy prop that lets you decide when to load the script.