Tag: Module 9: Building a Real-World Application

  • Module 9: Building a Real-World Application

    Lesson 1: Setting up the Project Structure

    Creating a well-organized project structure is essential for maintainable and scalable applications. Follow these best practices:

    •  Structure Overview:

    /components
    /pages
    /public
    /styles
    /utils

      • Explanation:
        • components/ : Reusable UI components.
        • pages/ : Route-based structure for the application.
        • public/ : Static assets such as images and fonts.
        • styles/ : Global and module-specific CSS files.
        • utils/ : Helper functions and utilities.

    Lesson 2: Using an External API

    Learn how to fetch and integrate data from an external API in a Next.js application.

      • Steps:
        1. Choosean API (e.g., OpenWeatherMap, GitHub API).
        2. Fetch data using getStaticProps or getServerSideProps :

    export async function getStaticProps() {
    const res = await fetch(‘https://api.example.com/data’); const data = await res.json();

    return {
    props: { data },
    };
    }

    export default function Page({ data }) { return <div>{JSON.stringify(data)}</div>;
    }

      • Best Practices:
        • Use environment variables for API keys.
        • Handle errors gracefully with try-catch blocks or fallback content.

    Lesson 3: Implementing Authentication

     Add user authentication to your application using libraries like next-auth or custom APIs.

      • Steps with ******** next-auth :
        1. Install next-auth :

          npm install next-auth

        2. Configure anAPI route:

          import NextAuth from ‘next-auth’;
          import Providers from ‘next-auth/providers’;

          export default NextAuth({ providers: [
          Providers.GitHub({
          clientId: process.env.GITHUB_CLIENT_ID, clientSecret: process.env.GITHUB_CLIENT_SECRET,
          }),
          ],
          });

        3. Protectpages using getSession :

          import { getSession } from ‘next-auth/client’;

          export async function getServerSideProps(context) { const session = await getSession(context);
          if (!session) { return {
          redirect: {
          destination: ‘/api/auth/signin’, permanent: false,
          },
          };
          }

          return {
          props: { session },
          };
          }

    Lesson 4: SEO Optimization

    Optimize your application for search engines using built-in Next.js features.

      • Steps:
        1. Add metadata with the <Head> component:

          import Head from ‘next/head’;

          export default function Page() { return (
          <>
          <Head>
          <title>My Page</title>
          <meta name=”description” content=”This is my page description.” />
          </Head>
          <div>Welcome to My Page</div>

          );
          }

        2. Optimize images with the <Image> component.
        3. Use dynamic rendering methods (SSR or ISR) for up-to-date
      • Best Practices:
        • Use descriptive URLs and headings.
        • Integrate schema markup for rich results.

    Additional Resources

     Example Projects