|
| 1 | +# FaustJS SvelteKit Template Hierarchy Example |
| 2 | + |
| 3 | +This example demonstrates how to use FaustJS with SvelteKit to create a headless WordPress application with automatic template hierarchy support. |
| 4 | + |
| 5 | +## What is Template Hierarchy? |
| 6 | + |
| 7 | +WordPress template hierarchy determines which template file is used to display different types of content (posts, pages, archives, etc.). This example shows how to implement similar functionality in a SvelteKit application using FaustJS. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- ✅ Automatic template selection based on WordPress content type |
| 12 | +- ✅ Support for custom post types and archives |
| 13 | +- ✅ WordPress-style template hierarchy (single.svelte, archive.svelte, index.svelte) |
| 14 | +- ✅ GraphQL data fetching with URQL |
| 15 | +- ✅ Server-side rendering (SSR) |
| 16 | + |
| 17 | +## Getting Started |
| 18 | + |
| 19 | +### Prerequisites |
| 20 | + |
| 21 | +- Node.js v16.0.0 or newer |
| 22 | +- A WordPress site with the [FaustJS plugin](https://wordpress.org/plugins/faustwp/) installed |
| 23 | +- WPGraphQL plugin installed on your WordPress site |
| 24 | + |
| 25 | +### Installation |
| 26 | + |
| 27 | +1. Clone this repository or copy this example |
| 28 | +2. Install dependencies: |
| 29 | + |
| 30 | +```bash |
| 31 | +npm install |
| 32 | +``` |
| 33 | + |
| 34 | +3. Configure your WordPress URL in the `.env` file: |
| 35 | + |
| 36 | +```bash |
| 37 | +WORDPRESS_URL=https://your-wordpress-site.com |
| 38 | +``` |
| 39 | + |
| 40 | +### Development |
| 41 | + |
| 42 | +Start the development server: |
| 43 | + |
| 44 | +```bash |
| 45 | +npm run dev |
| 46 | + |
| 47 | +# or start the server and open the app in a new browser tab |
| 48 | +npm run dev -- --open |
| 49 | +``` |
| 50 | + |
| 51 | +The application will automatically: |
| 52 | + |
| 53 | +- Fetch content from your WordPress site |
| 54 | +- Determine the appropriate template based on the URL |
| 55 | +- Render the content using the matching Svelte template |
| 56 | + |
| 57 | +### Template Structure |
| 58 | + |
| 59 | +Templates are located in `src/wp-templates/`: |
| 60 | + |
| 61 | +- `index.svelte` - Default template (homepage, fallback) |
| 62 | +- `single.svelte` - Single post/page template |
| 63 | +- `archive.svelte` - Archive pages (categories, tags, custom post types) |
| 64 | + |
| 65 | +### Building |
| 66 | + |
| 67 | +To create a production version of your app: |
| 68 | + |
| 69 | +```bash |
| 70 | +npm run build |
| 71 | +``` |
| 72 | + |
| 73 | +You can preview the production build with `npm run preview`. |
| 74 | + |
| 75 | +## How It Works |
| 76 | + |
| 77 | +1. The `[...uri]/+page.server.js` route catches all URLs |
| 78 | +2. Uses `uriToTemplate()` from `@faustjs/sveltekit` to: |
| 79 | + - Query WordPress for content at the given URI |
| 80 | + - Determine the appropriate template type |
| 81 | + - Fetch the necessary data |
| 82 | +3. Renders the content using the matching Svelte template |
| 83 | + |
| 84 | +## Learn More |
| 85 | + |
| 86 | +- [FaustJS Documentation](https://faustjs.org/docs/) |
| 87 | +- [SvelteKit Documentation](https://kit.svelte.dev/docs) |
| 88 | +- [WordPress Template Hierarchy](https://developer.wordpress.org/themes/basics/template-hierarchy/) |
0 commit comments