How I Made My Website Load in 0.364 Seconds
Table of Contents
Introduction
In this tutorial, we will walk through the process of optimizing a Next.js website for performance based on the principles discussed in the video "How I Made My Website Load in 0.364 Seconds." We will cover key metrics for web performance, implement best practices for building a high-performance web application, and explore how to leverage Next.js features to enhance load times and user experience.
Chapter 1: Understanding Web Performance
- Importance of Performance: Web performance is crucial as it directly affects user experience and conversion rates. A delay of just 100 milliseconds can significantly impact sales for businesses like Amazon.
- Key Metrics: Familiarize yourself with the Core Web Vitals:
- Largest Contentful Paint (LCP): Measures loading performance. Aim for an LCP of 2.5 seconds or less.
- First Contentful Paint (FCP): Measures when the first piece of content is rendered. Aim for 1.8 seconds or less.
- First Input Delay (FID): Measures responsiveness. Aim for 100 milliseconds or less.
- Cumulative Layout Shift (CLS): Measures visual stability. Aim for a score of 0.1 or less.
Chapter 2: Setting Up a Next.js Application
-
Create a New Project:
- Create a new folder for your project and open it in Visual Studio Code.
- Use the terminal to run:
npx create-next-app@latest --ts
- Answer the setup prompts as needed.
-
Install Dependencies:
- Install additional packages like Tailwind CSS and Shad CN UI:
npx shadcn-ui@latest init
- Install additional packages like Tailwind CSS and Shad CN UI:
-
Start the Development Server:
- Run the following command to start your local server:
npm run dev
- Run the following command to start your local server:
Chapter 3: Building the Resource Page
-
Create a Resource Page:
- Set up the folder structure in your application for the home page and loading states.
- Use the
app
directory to create a new page that will serve as your resource listing.
-
Implement Navigation:
- Create a simple navbar and footer for better UX.
- Use semantic HTML tags for better accessibility.
-
Develop the Search Functionality:
- Create a search input component that captures user input and implements debouncing to minimize API calls.
- Use the
useEffect
hook to trigger search updates.
-
Implement Filters:
- Create filter buttons to allow users to narrow their resource search by category.
- Use state management to keep track of which filters are active.
Chapter 4: Fetching Data with Sanity
-
Set Up Sanity:
- Install the Sanity CLI and create a new project using:
npm install -g @sanity/cli sanity init
- Install the Sanity CLI and create a new project using:
-
Create Schemas:
- Define schemas for your resources and resource playlists in the Sanity studio to structure your content.
-
Fetch Resources:
- Create functions to fetch resources from Sanity using GROQ queries.
- Use
getStaticProps
orgetServerSideProps
to fetch data on the server side.
Chapter 5: Optimizing Performance
-
Implement Image Optimization:
- Use Next.js's built-in
Image
component for automatic image optimization. - Ensure images are served in the correct format and size.
- Use Next.js's built-in
-
Use Server-Side Rendering:
- Leverage Next.js’s capabilities to render pages on the server, reducing the amount of JavaScript sent to the client.
-
Conduct Performance Tests:
- Use tools like Google Lighthouse and GTmetrix to analyze and improve your website's performance.
- Aim for scores of 90+ in performance metrics.
Chapter 6: Deployment
-
Prepare for Deployment:
- Ensure your code is free of errors and warnings. Fix any TypeScript issues that arise.
- Push your code to GitHub.
-
Deploy on Vercel:
- Sign up for Vercel and import your GitHub repository.
- Configure environment variables as needed and click deploy.
-
Check Your Live Site:
- After deployment, test your live site to ensure all functionalities work as expected.
Conclusion
By following these steps, you can create a high-performance Next.js application optimized for speed and user experience. Regularly monitor your website’s performance metrics and make adjustments as necessary to maintain an optimal user experience. Consider exploring additional features and improvements in your Next.js projects to keep up with the latest web development trends.