|
| 1 | +# Example: Multisite Next.js App Router using the Fetch API |
| 2 | + |
| 3 | +# Table of Contents |
| 4 | +- [Overview](#overview) |
| 5 | + - [Prerequisites](#prerequisites) |
| 6 | + - [Project Structure](#project-structure) |
| 7 | + - [Features](#features) |
| 8 | + - [Screenshots](#screenshots) |
| 9 | +- [Running the Example with wp-env](#running-the-example-with-wp-env) |
| 10 | + - [Prerequisites](#prerequisites-1) |
| 11 | + - [Setup Repository and Packages](#setup-repository-and-packages) |
| 12 | + - [Build and Start the Application](#build-and-start-the-application) |
| 13 | + - [Command Reference](#command-reference) |
| 14 | + - [Database Access](#database-access) |
| 15 | + - [Adding More Sites to the Multisite Examples](#adding-more-sites-to-the-multisite-examples) |
| 16 | + |
| 17 | + |
| 18 | +# Overview |
| 19 | + |
| 20 | +An example mulitsite headless WordPress application using Next.js App Router and the fetch API to fetch data from WordPress using WPGraphQL. It showcases different data fetching strategies, state management techniques, and modern web development patterns in a real-world application context. This also contains a full example using wp-env and sample data. |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +- Node.js (v18+ recommended) |
| 25 | +- [pnpm](https://pnpm.io/) |
| 26 | +- [Docker](https://www.docker.com/) (if you plan on running the example see details below) |
| 27 | + |
| 28 | + |
| 29 | +## Project Structure |
| 30 | + |
| 31 | +``` |
| 32 | +/ |
| 33 | +├── example-app/ # Next.js App Headless Example Application |
| 34 | +│ ├── app/ # Application-specific pages and routes |
| 35 | +│ ├── components/ # Reusable UI components |
| 36 | +│ ├── lib/ # Utility functions and libraries and WP GraphQL Fragments |
| 37 | +| |
| 38 | +├── wp-env/ # WordPress local environment setup |
| 39 | +│ ├── wp-env.json |
| 40 | +│ ├── db/ # Example database export to be imported for the example setup |
| 41 | +│ ├── setup/ # .htaccess file to fix a CORS issue |
| 42 | +| |── log/ # Log file for wp-content/debug.log |
| 43 | +| |-- screenshots/ # Example screenshots for this README |
| 44 | +│ ├── uploads.zip # Zipped wp-content/uploads directory for the example application |
| 45 | +│ ├── uploads/ # Unzipped wp-content/uploads directory when the example application is run. |
| 46 | +├── package.json # Scripts for running the example application |
| 47 | +``` |
| 48 | + |
| 49 | +## Features |
| 50 | + |
| 51 | +- **Covers various rendering patterns of Next.js** |
| 52 | + |
| 53 | + - Server-Side Rendering (SSR) for dynamic pages |
| 54 | + - Static Site Generation (SSG) for static pages |
| 55 | + - Client-Side data fetching (CSR) for blog settings |
| 56 | + - Hybrid data fetching, combining SSR and CSR |
| 57 | + |
| 58 | +- **Multisite Features** |
| 59 | + |
| 60 | + - Dynamic site management in next.config.mjs |
| 61 | + - Fetch data based off site key defined in next.config.mjs |
| 62 | + - Homepage dynamically fetches and displays data from multiple WordPress sites in the multisite network. |
| 63 | + |
| 64 | + - **Catch-All Template** |
| 65 | + |
| 66 | + - The catch-all template fetches data from all WordPress sites in the multisite network. |
| 67 | + - It loops through each site until it finds the relevant data to display. |
| 68 | + - Can be extended on a per site basis for querying different fragments |
| 69 | + |
| 70 | +- **Blog features** |
| 71 | + |
| 72 | + - Listing posts with pagination |
| 73 | + - Fetching posts and pages using nodeByUri of WPGraphQL |
| 74 | + - Fetching static pages at build time |
| 75 | + - Commenting posts |
| 76 | + - Header with dynamic blog title |
| 77 | + - Featured image |
| 78 | + - Includes category and tag blog post listings |
| 79 | + |
| 80 | +- **Other Template Features** |
| 81 | + - Page template |
| 82 | + - CPT template with listings for a CPT events (can be adapted for other CPT) |
| 83 | + - Single CPT page with ACF custom meta fields |
| 84 | + |
| 85 | +## Screenshots |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +# Running the example with wp-env |
| 98 | + |
| 99 | +## Prerequisites |
| 100 | + |
| 101 | +**Note** Please make sure you have all prerequisites installed as mentioned above and Docker running (`docker ps`) |
| 102 | + |
| 103 | +## Setup Repository and Packages |
| 104 | + |
| 105 | +- Clone the repo `git clone https://github.com/wpengine/hwptoolkit.git` |
| 106 | +- Install packages `cd hwptoolkit && pnpm install` |
| 107 | +- Setup a .env file under `examples/next/client-multisite-app-router-fetch-data/example-app` with |
| 108 | + |
| 109 | +``` |
| 110 | +NEXT_PUBLIC_WORDPRESS_URL=http://localhost:8888 |
| 111 | +NEXT_PUBLIC_MOVIE_WORDPRESS_URL=http://localhost:8888/movies |
| 112 | +``` |
| 113 | + |
| 114 | +e.g. |
| 115 | + |
| 116 | +```bash |
| 117 | +echo "NEXT_PUBLIC_WORDPRESS_URL=http://localhost:8888" > examples/next/client-multisite-app-router-fetch-data/example-app/.env |
| 118 | +echo "NEXT_PUBLIC_MOVIE_WORDPRESS_URL=http://localhost:8888/movies" >> examples/next/client-multisite-app-router-fetch-data/example-app/.env |
| 119 | +``` |
| 120 | + |
| 121 | +> [!IMPORTANT] |
| 122 | +> The site configuration is based off values set in [example-app/next.config.mjs](example-app/next.config.mjs) |
| 123 | +> If you want to add more sites please feel free to add more sites to your .env file and update the configuration in the next.config.mjs |
| 124 | +
|
| 125 | + |
| 126 | +## Build and start the application |
| 127 | + |
| 128 | +- `cd examples/next/client-multisite-app-router-fetch-data` |
| 129 | +- Then run `pnpm example:build` will build and start your application.) |
| 130 | +- This does the following: |
| 131 | + - Unzips `wp-env/uploads.zip` to `wp-env/uploads` which is mapped to the wp-content/uploads directory for the Docker container. |
| 132 | + - Starts up [wp-env](https://developer.wordpress.org/block-editor/getting-started/devenv/get-started-with-wp-env/) |
| 133 | + - Imports the database from [wp-env/db/database.sql](wp-env/db/database.sql) |
| 134 | + - Sets the mulitisite wp-config.php variables |
| 135 | + - Install Next.js dependencies for `example-app` |
| 136 | + - Runs the Next.js dev script |
| 137 | + |
| 138 | +Congratulations, WordPress should now be fully set up. |
| 139 | + |
| 140 | +| Frontend | Admin | |
| 141 | +|----------|------------------------------| |
| 142 | +| [http://localhost:3000/](http://localhost:3000/) | [http://localhost:8888/wp-admin/](http://localhost:8888/wp-admin/) | |
| 143 | + |
| 144 | +The login details for the admin is username "admin" and password "password" |
| 145 | + |
| 146 | + |
| 147 | +> [!IMPORTANT] |
| 148 | +There are issues with setting variables for multisite in .wp-env.json before converting the site to multisite. For this reason we set the variables after the sample database (which is multisite) is imported. |
| 149 | +**You also need to update the port in wp:multisite:set:variables if you ever change 8888** |
| 150 | + |
| 151 | +## Command Reference |
| 152 | + |
| 153 | +| Command | Description | |
| 154 | +|-----------------------------|-----------------------------------------------------------------------------| |
| 155 | +| `example:build` | Prepares the environment by unzipping images, starting WordPress, importing the database, and starting the application. | |
| 156 | +| `example:dev` | Runs the Next.js development server. | |
| 157 | +| `example:dev:install` | Installs the required Next.js packages. | |
| 158 | +| `example:start` | Starts WordPress, sets the multisite variables, and starts the Next.js development server. | |
| 159 | +| `example:stop` | Stops the WordPress environment. | |
| 160 | +| `example:prune` | Rebuilds and restarts the application by destroying and recreating the WordPress environment. | |
| 161 | +| `wp:start` | Starts the WordPress environment. | |
| 162 | +| `wp:stop` | Stops the WordPress environment. | |
| 163 | +| `wp:multisite:set:variables`| Sets the variables for multisite in `wp-config.php`. | |
| 164 | +| `wp:destroy` | Completely removes the WordPress environment. | |
| 165 | +| `wp:db:query` | Executes a database query within the WordPress environment. | |
| 166 | +| `wp:db:export` | Exports the WordPress database to `wp-env/db/database.sql`. | |
| 167 | +| `wp:db:import` | Imports the WordPress database from `wp-env/db/database.sql`. | |
| 168 | +| `wp:images:unzip` | Extracts the WordPress uploads directory. | |
| 169 | +| `wp:images:zip` | Compresses the WordPress uploads directory. | |
| 170 | + |
| 171 | + |
| 172 | +>**Note** You can run `pnpm wp-env` and use any other wp-env command. You can also see <https://www.npmjs.com/package/@wordpress/env> for more details on how to use or configure `wp-env`. |
| 173 | +
|
| 174 | +### Database access |
| 175 | + |
| 176 | +If you need database access add the following to your wp-env `"phpmyadminPort": 11111,` (where port 11111 is not allocated). |
| 177 | + |
| 178 | +You can check if a port is free by running `lsof -i :11111` |
| 179 | + |
| 180 | +### Adding more sites to the multisite examples |
| 181 | + |
| 182 | +If you want to add more sites you need to do the followint |
| 183 | + |
| 184 | +1. Add the variable to your .env next.js setup e.g. `NEXT_PUBLIC_SPORT_WORDPRESS_URL=http://localhost:8888/sport` |
| 185 | +2. Update your next.config.mjs with a site key and this value e.g. |
| 186 | +```js |
| 187 | + WORDPRESS_SITES: JSON.stringify({ |
| 188 | + main: process.env.NEXT_PUBLIC_WORDPRESS_URL + "/graphql", |
| 189 | + movie_site: process.env.NEXT_PUBLIC_MOVIE_WORDPRESS_URL + "/graphql", |
| 190 | + sport_site: process.env.NEXT_PUBLIC_SPORT_WORDPRESS_URL + "/graphql", |
| 191 | + }), |
| 192 | +``` |
| 193 | + |
| 194 | +You then use the site key e.g. `sport_site` when fetching data with a template or the `fetchGraphQL` function |
| 195 | + |
| 196 | +e.g. |
| 197 | + |
| 198 | +``` |
| 199 | +# example-app/src/app/sport-news/page.js |
| 200 | +import { SportsListingsQuery } from "@/lib/queries/SportsListingsQuery"; |
| 201 | +import { CustomPostTypeTemplate } from "@/components/cpt/CustomPostTypeTemplate"; |
| 202 | +
|
| 203 | +export default async function SportsListingsPage(params) { |
| 204 | + return CustomPostTypeTemplate(SportsListingsQuery, { |
| 205 | + params: params, |
| 206 | + customPostType: "sport", # Update CPT |
| 207 | + siteKey: "sport_site", # This is your site key |
| 208 | + title: "Sport News", |
| 209 | + cacheExpiry: 3600, |
| 210 | + }); |
| 211 | +} |
| 212 | +
|
| 213 | +``` |
| 214 | + |
| 215 | +**Note** |
| 216 | + |
| 217 | +1. You need to add the query |
| 218 | +2. You also need to update the Custom Post Type Template to include your listings template |
0 commit comments