Published in March 20, 2025
Performance is crucial for any web application, especially for an app built with Next.js. A slow website leads to high bounce rates, poor user experience, and lower SEO rankings. In this blog, I'll walk you through how I optimized my Next.js app to load 10x faster and how you can achieve the same results.
Before optimization, my app suffered from the following issues:
vaScript bundles** causing long load times.
After diagnosing these problems, I applied several optimizations that drastically improved performance.
Next.js automatically pre-renders pages whenever possible. To take full advantage of this:
export default function Home({ data }: { data: any }) {
return <div>{data.title}</div>;
}
export async function getStaticProps() {
const res = await fetch('https://api.example.com/data');
const data = await res.json();
return {
props: { data },
revalidate: 10, // ISR (Incremental Static Regeneration)
};
}
getStaticProps
ensures the page is built at compile time.Images often account for 50% of a website’s total weight. Next.js has built-in image optimization:
import Image from 'next/image';
<Image src="/example.jpg" alt="Example" width={500} height={300} quality={75} />
To avoid shipping unnecessary JavaScript, code split large components:
import dynamic from 'next/dynamic';
const HeavyComponent = dynamic(() => import('../components/HeavyComponent'), {
ssr: false,
});
This ensures the heavy component loads only when needed, reducing the initial bundle size.
Next.js App Router supports React Server Components, reducing client-side JS:
export default async function Page() {
const data = await fetchData();
return <div>{data}</div>;
}
Expensive API calls should be cached using React Query, SWR, or Redis:
import useSWR from 'swr';
const fetcher = (url: string) => fetch(url).then((res) => res.json());
export default function Page() {
const { data, error } = useSWR('/api/data', fetcher, {
revalidateOnFocus: false,
});
if (error) return <div>Error loading data</div>;
if (!data) return <div>Loading...</div>;
return <div>{data.title}</div>;
}
useSWR
caches and refetches only when necessary.To defer non-essential scripts, use:
<script src="/heavy-script.js" async />
OR load third-party libraries only when needed:
const HeavyLib = dynamic(() => import('heavy-lib'), { ssr: false });
Using Tailwind CSS and PurgeCSS, we removed unused styles:
"purge": ["./pages/**/*.tsx", "./components/**/*.tsx"]
Minimize JS size by:
Instead of relying on a slow backend, use Next.js Edge Functions:
export default function middleware(req) {
return new Response('Hello from Edge!');
}
Metric | Before Optimization | After Optimization |
---|---|---|
First Contentful Paint (FCP) | 3.5s | 1.2s |
Largest Contentful Paint (LCP) | 5.2s | 1.5s |
Time to Interactive (TTI) | 6.8s | 1.9s |
JavaScript Payload | 750KB | 150KB |
Image Load Time | 4s | 800ms |
By implementing these eight optimizations, I achieved a 10x faster Next.js app.
If your Next.js app is slow, start with these steps:
✅ Enable Static Generation (SSG) & ISR
✅ Optimize Images with Next.js <Image />
✅ Reduce JS bundle size using dynamic imports
✅ Use React Server Components for heavy data fetching
✅ Implement Caching & Memoization (SWR, React Query)
✅ Minimize Render-blocking Scripts & Unused Code
✅ Deploy APIs with Edge Functions for speed
By applying these techniques, you’ll see massive performance improvements. Try them out and watch your app load at lightning speed! ⚡
#naymhossen #webdevelopment #nextjs #developer #coding #programming #faster #nextjs15