Skip to content

confrontend/bookstore-ssr-nextjs-react

Repository files navigation

Bookstore App with Next.js 14: A Full-Stack React Solution

Introduction

Welcome to our Bookstore application, built with the power and flexibility of Next.js 14, the latest version of the acclaimed React framework designed for building full-stack web applications. Our journey into crafting this bookstore is not just about creating an application but about leveraging the best of modern web technologies to provide a seamless, interactive, and fast user experience.

Choice of Technology

Our decision to utilize Next.js 14 for developing the Bookstore application is grounded in the framework's robust feature set, designed to address common web development challenges efficiently. Here are some key reasons behind our choice:

  • Efficient Routing & SSR: Utilizes a file-system-based router with support for layouts and dynamic rendering, ensuring speedy page loads.
  • Simplified Data Fetching: Easy async/await usage for dynamic content updates, enhancing the user experience.
  • Flexible Styling Options: Supports CSS Modules, Tailwind CSS, and CSS-in-JS for creative, responsive designs.
  • Built-in Performance Optimizations: Includes automatic optimization of images, fonts, and scripts to boost Core Web Vitals.
  • Robust TypeScript Support: Offers improved type checking and compilation, leading to more reliable and bug-free code.
  • Enhanced SEO & Sharing: Features a Metadata API and customizable <head> elements, improving search engine ranking and social media visibility.

State Management

Leveraging Redux for State Management in a Next.js Application

In our Next.js application, we utilized Redux to manage the application state on the client side while still maintaining the benefits of server-side rendering (SSR) for improved SEO and initial load performance. This approach ensures our application's dynamic functionality integrates seamlessly with Next.js's SSR capabilities.

Approach

  1. Server-Side Data Fetching: On the server, we pre-load essential data needed for the initial render. This step is crucial for SSR to send a fully rendered page to the client, enhancing SEO and the user's initial load experience.

    export default async function Home() {
      // Initially load books from server. This preserves the benefits of SSR.
      const books = await getBooks();
    
      return (
        <main className="container mx-auto py-8 px-4">
          <StoreProvider>
            ...
            <BookList books={books} />
          </StoreProvider>
        </main>
      );
    }
  2. Client-Side State Initialization: Once the data is fetched server-side, we pass it to our client components. The BookList component receives the initial list of books as props and dispatches an action to populate the Redux store on the client side. This step syncs the server-side state with the client-side Redux store.

    export const BookList = ({ books }: { books: Book[] }) => {
      const store = useStore<RootState>();
      const initialized = useRef(false);
    
      if (!initialized.current) {
        // Sync server-side state with client-side state.
        store.dispatch(addBooks(books));
        initialized.current = true;
      }
    
      const clientBooks = useSelector((state: RootState) => state.books.list);
      // Render books...
    }
  3. State Management for Dynamic Features: Dynamic features, such as adding or editing books, leverage the Redux store for state management. Modals for adding and editing books are triggered via UI state in the Redux store, and the forms within these modals dispatch actions to update the Redux store based on user interactions. This allows for a seamless user experience with immediate updates reflected in the UI.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published