|
| 1 | +# AGENTS.md |
| 2 | + |
| 3 | +## BigCommerce Catalyst Codebase Overview |
| 4 | + |
| 5 | +This document provides guidance for LLMs working with the BigCommerce Catalyst codebase, focusing on the Next.js application architecture, data fetching patterns, and key design principles. |
| 6 | + |
| 7 | +## Repository Structure |
| 8 | + |
| 9 | +The main Next.js application is located in the `/core` directory, which contains the complete e-commerce storefront implementation. Other packages exist outside of `/core` but are not the primary focus for most development work. |
| 10 | + |
| 11 | +## Middleware Architecture |
| 12 | + |
| 13 | +The application uses a composed middleware stack that significantly alters the default Next.js routing behavior. The middleware composition includes authentication, internationalization, analytics, channel handling, and most importantly, custom routing. |
| 14 | + |
| 15 | +### Custom Routing with `with-routes` Middleware |
| 16 | + |
| 17 | +The `with-routes` middleware is the most critical component that overrides Next.js's default path-based routing. Instead of relying on file-based routing, this middleware: |
| 18 | + |
| 19 | +1. **Queries the BigCommerce GraphQL API** to resolve incoming URL paths to specific entity types (products, categories, brands, blog posts, pages) |
| 20 | + |
| 21 | +2. **Rewrites requests** to internal Next.js routes based on the resolved entity type |
| 22 | + |
| 23 | +3. **Handles redirects** automatically based on BigCommerce's redirect configuration |
| 24 | + |
| 25 | +This means that URLs like `/my-product-name` can resolve to `/en/product/123` internally, providing flexible URL structure while maintaining SEO-friendly paths. |
| 26 | + |
| 27 | +## Data Fetching and Partial Prerendering (PPR) |
| 28 | + |
| 29 | +### PPR Configuration |
| 30 | + |
| 31 | +The application uses Next.js Partial Prerendering with incremental adoption. This allows static parts of pages to be prerendered while dynamic content streams in. |
| 32 | + |
| 33 | +### Streamable Pattern |
| 34 | + |
| 35 | +A custom `Streamable` utility provides suspense-friendly data fetching that works seamlessly with PPR. This pattern: |
| 36 | + |
| 37 | +- Wraps promises in a lazy-loading container |
| 38 | +- Provides stable promise instances for identical inputs |
| 39 | +- Integrates with React's `use()` hook and Suspense boundaries |
| 40 | + |
| 41 | +### Data Fetching Best Practices |
| 42 | + |
| 43 | +1. **Use React's `cache()` function** for server-side data fetching to ensure deduplication |
| 44 | + |
| 45 | +2. **Implement proper cache strategies** based on whether user authentication is present |
| 46 | + |
| 47 | +3. **Leverage Streamable for progressive enhancement** where static content loads immediately and dynamic content streams in |
| 48 | + |
| 49 | +## GraphQL API Client |
| 50 | + |
| 51 | +### Centralized Client Configuration |
| 52 | + |
| 53 | +All interactions with the BigCommerce API should use the centralized GraphQL client. This client provides: |
| 54 | + |
| 55 | +- Automatic channel ID resolution based on locale |
| 56 | +- Proper authentication token handling |
| 57 | +- Request/response logging in development |
| 58 | +- Error handling with automatic auth redirects |
| 59 | +- IP address forwarding for personalization |
| 60 | + |
| 61 | +### Usage Pattern |
| 62 | + |
| 63 | +Always import and use the configured client rather than making direct API calls. The client handles all the necessary headers, authentication, and channel context automatically. |
| 64 | + |
| 65 | +## UI Design System (Vibes) |
| 66 | + |
| 67 | +### Architecture Overview |
| 68 | + |
| 69 | +The UI design system is completely separated into the `vibes/soul` directory structure, providing clean separation between business logic and presentation. |
| 70 | + |
| 71 | +### Component Hierarchy |
| 72 | + |
| 73 | +1. **Primitives** (`vibes/soul/primitives/`) - Basic reusable UI components like buttons, cards, forms |
| 74 | + |
| 75 | +2. **Sections** (`vibes/soul/sections/`) - Page-level components that compose primitives into complete page sections |
| 76 | + |
| 77 | +3. **Library** (`vibes/soul/lib/`) - Utility functions and patterns like the Streamable implementation |
| 78 | + |
| 79 | +### Import Patterns |
| 80 | + |
| 81 | +Components should be imported from the vibes design system using the `@/vibes/soul/` alias, maintaining clear separation between business logic in `/components` and design system components in `/vibes`. |
| 82 | + |
| 83 | +## Key Architectural Principles |
| 84 | + |
| 85 | +1. **Routing Flexibility**: Unlike typical Next.js applications, URLs are resolved dynamically via GraphQL rather than file structure |
| 86 | +2. **Progressive Enhancement**: Static content loads immediately with dynamic content streaming via PPR and Streamable |
| 87 | +3. **Design System Separation**: Complete separation between business components and design system components |
| 88 | +4. **Centralized API Access**: All BigCommerce API interactions go through the configured GraphQL client |
| 89 | +5. **Middleware-First**: Critical functionality like routing, auth, and internationalization handled at the middleware layer |
| 90 | + |
| 91 | +## Notes |
| 92 | + |
| 93 | +This codebase differs significantly from typical Next.js applications due to the custom routing middleware and e-commerce-specific patterns. The `with-routes` middleware essentially turns Next.js into a headless CMS router, where content structure is determined by the BigCommerce backend rather than the filesystem. Understanding this fundamental difference is crucial for working effectively with the codebase. |
| 94 | + |
| 95 | +The Streamable pattern and PPR integration provide excellent user experience through progressive loading, but require understanding of React's newer concurrent features like the `use()` hook and Suspense boundaries. |
0 commit comments