|
| 1 | +# @faustjs/nextjs Template Hierarchy Example |
| 2 | + |
| 3 | +This example demonstrates how to use the `@faustjs/nextjs` package to automatically discover WordPress templates in a Next.js project using the WordPress template hierarchy. |
| 4 | + |
| 5 | +## What This Example Shows |
| 6 | + |
| 7 | +- **Template Discovery**: Automatically finds WordPress template files in your `wp-templates` directory |
| 8 | +- **Template Registry**: Uses dynamic imports to organize and load templates efficiently |
| 9 | +- **Template Hierarchy**: Follows WordPress template hierarchy rules for organizing templates |
| 10 | +- **Template Resolution**: SSR catch-all route that dynamically resolves WordPress URLs to templates |
| 11 | + |
| 12 | +## Project Structure |
| 13 | + |
| 14 | +``` |
| 15 | +src/ |
| 16 | +├── components/ |
| 17 | +│ └── WordPressLayout.js # Shared layout for all templates |
| 18 | +├── pages/ |
| 19 | +│ ├── _app.js # Next.js app wrapper |
| 20 | +│ ├── _document.js # Next.js document structure |
| 21 | +│ ├── index.js # Demo page showing discovered templates |
| 22 | +│ ├── 404.js # Custom 404 page |
| 23 | +│ └── [...uri].js # Catch-all route implementing template resolution |
| 24 | +├── styles/ |
| 25 | +│ └── globals.css # Global styles |
| 26 | +└── wp-templates/ |
| 27 | + ├── index.js # Template registry with dynamic imports |
| 28 | + ├── page.js # Page template |
| 29 | + ├── single.js # Single post template |
| 30 | + ├── archive.js # Archive template |
| 31 | + └── index-template.js # Index/fallback template |
| 32 | +``` |
| 33 | + |
| 34 | +## How It Works |
| 35 | + |
| 36 | +1. **Template Files**: Place your WordPress templates in `src/wp-templates/` |
| 37 | +2. **Template Registry**: The `index.js` file exports all templates with dynamic imports |
| 38 | +3. **Next.js Integration**: Uses Next.js dynamic imports for optimal loading |
| 39 | +4. **SSR Resolution**: The `[...uri].js` route dynamically fetches content and resolves templates |
| 40 | +5. **Shared Layout**: All templates use `WordPressLayout.js` for consistent styling |
| 41 | +6. **Path Aliases**: Uses `@/` alias for clean imports (configured in `jsconfig.json` and `next.config.js`) |
| 42 | + |
| 43 | +## Available Templates |
| 44 | + |
| 45 | +The following templates are currently registered and available: |
| 46 | + |
| 47 | +- **single.js** - Displays individual blog posts |
| 48 | +- **page.js** - Displays individual WordPress pages |
| 49 | +- **archive.js** - Displays category, tag, and other archive pages |
| 50 | +- **index-template.js** - Fallback template for any content without a specific template |
| 51 | + |
| 52 | +## Running the Example |
| 53 | + |
| 54 | +```bash |
| 55 | +# Install dependencies |
| 56 | +pnpm install |
| 57 | + |
| 58 | +# Copy environment configuration |
| 59 | +cp .env.example .env.local |
| 60 | + |
| 61 | +# Edit .env.local to set your WordPress URL |
| 62 | +# WORDPRESS_URL=https://your-wordpress-site.com |
| 63 | + |
| 64 | +# Start the development server |
| 65 | +pnpm dev |
| 66 | +``` |
| 67 | + |
| 68 | +Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. |
| 69 | + |
| 70 | +## Template Discovery |
| 71 | + |
| 72 | +The example shows discovered templates from the `wp-templates` directory on the home page, displaying: |
| 73 | + |
| 74 | +- Template name and type |
| 75 | +- File path information |
| 76 | +- Template descriptions |
| 77 | + |
| 78 | +The templates are loaded dynamically using Next.js dynamic imports for optimal performance. |
| 79 | + |
| 80 | +## Live Template Router Demo |
| 81 | + |
| 82 | +Try these example URLs to see the template hierarchy in action: |
| 83 | + |
| 84 | +- `/about` → Resolves to `page.js` template |
| 85 | +- `/hello-world` → Resolves to `single.js` template |
| 86 | +- `/category/uncategorized` → Resolves to `archive.js` template |
| 87 | +- `/tag/fun` → Resolves to `archive.js` template |
| 88 | + |
| 89 | +## Environment Variables |
| 90 | + |
| 91 | +Create a `.env.local` file with: |
| 92 | + |
| 93 | +```bash |
| 94 | +WORDPRESS_URL=https://your-wordpress-site.com |
| 95 | +``` |
| 96 | + |
| 97 | +Replace with your actual WordPress site URL. |
| 98 | + |
| 99 | +## Learn More |
| 100 | + |
| 101 | +- [Next.js Documentation](https://nextjs.org/docs) - Learn about Next.js features and API |
| 102 | +- [@faustjs/nextjs Documentation](../../packages/nextjs/README.md) - Learn about the template hierarchy system |
| 103 | +- [@faustjs/template-hierarchy Documentation](../../packages/template-hierarchy/README.md) - Understand the template hierarchy implementation |
| 104 | + |
| 105 | +## Dependencies |
| 106 | + |
| 107 | +This example uses: |
| 108 | + |
| 109 | +- **@faustjs/nextjs** - Next.js integration for Faust.js |
| 110 | +- **@faustjs/template-hierarchy** - Template hierarchy system |
| 111 | +- **Next.js 15** - React framework |
| 112 | +- **GraphQL** - Query language for APIs |
| 113 | +- **React 18** - UI library |
0 commit comments