Skip to content

Commit 6e38912

Browse files
authored
Merge pull request #359 from wpengine/next-template-hierarchy
docs: Next Pages template hierarchy
2 parents fe3fe2f + 0444319 commit 6e38912

29 files changed

+1525
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"phpVersion": "8.3",
3+
"plugins": [
4+
"https://github.com/wp-graphql/wp-graphql/releases/latest/download/wp-graphql.zip"
5+
],
6+
"themes": ["https://downloads.wordpress.org/theme/nude.1.2.zip"],
7+
"config": {
8+
"WP_DEBUG": true,
9+
"SCRIPT_DEBUG": false,
10+
"GRAPHQL_DEBUG": true,
11+
"WP_DEBUG_LOG": true,
12+
"WP_DEBUG_DISPLAY": false,
13+
"SAVEQUERIES": false
14+
},
15+
"mappings": {
16+
"db": "./wp-env/db",
17+
"wp-content/uploads": "./wp-env/uploads",
18+
".htaccess": "./wp-env/setup/.htaccess"
19+
},
20+
"lifecycleScripts": {
21+
"afterStart": "wp-env run cli -- wp theme activate nude && wp-env run cli -- wp theme delete --all && wp-env run cli -- wp rewrite structure '/%postname%/' && wp-env run cli -- wp rewrite flush"
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Astro Template HIerarchy and Data fetching w/URQL Example
2+
3+
In this example we show how to implement the WP Template Hierarchy in Astro for use with a Headless WordPress backend using WPGraphQL. We use URQL for all routing and fetching page content.
4+
5+
## Getting Started
6+
7+
> [!IMPORTANT]
8+
> Docker Desktop needs to be installed to run WordPress locally.
9+
10+
1. Run `npm run example:setup` to install dependencies and configure the local WP server.
11+
2. Run `npm run example:start` to start the WP server and Astro development server.
12+
13+
> [!NOTE]
14+
> When you kill the long running process this will not shutdown the local WP instance, only Astro. You must run `npm run example:stop` to kill the local WP server.
15+
16+
## Trouble Shooting
17+
18+
To reset the WP server and re-run setup you can run `npm run example:prune` and confirm "Yes" at any prompts.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.*
7+
.yarn/*
8+
!.yarn/patches
9+
!.yarn/plugins
10+
!.yarn/releases
11+
!.yarn/versions
12+
13+
# testing
14+
/coverage
15+
16+
# next.js
17+
/.next/
18+
/out/
19+
20+
# production
21+
/build
22+
23+
# misc
24+
.DS_Store
25+
*.pem
26+
27+
# debug
28+
npm-debug.log*
29+
yarn-debug.log*
30+
yarn-error.log*
31+
.pnpm-debug.log*
32+
33+
# env files (can opt-in for committing if needed)
34+
.env*
35+
36+
# vercel
37+
.vercel
38+
39+
# typescript
40+
*.tsbuildinfo
41+
next-env.d.ts
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/pages/api-reference/create-next-app).
2+
3+
## Getting Started
4+
5+
First, run the development server:
6+
7+
```bash
8+
npm run dev
9+
# or
10+
yarn dev
11+
# or
12+
pnpm dev
13+
# or
14+
bun dev
15+
```
16+
17+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
18+
19+
You can start editing the page by modifying `pages/index.js`. The page auto-updates as you edit the file.
20+
21+
[API routes](https://nextjs.org/docs/pages/building-your-application/routing/api-routes) can be accessed on [http://localhost:3000/api/hello](http://localhost:3000/api/hello). This endpoint can be edited in `pages/api/hello.js`.
22+
23+
The `pages/api` directory is mapped to `/api/*`. Files in this directory are treated as [API routes](https://nextjs.org/docs/pages/building-your-application/routing/api-routes) instead of React pages.
24+
25+
This project uses [`next/font`](https://nextjs.org/docs/pages/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
26+
27+
## Learn More
28+
29+
To learn more about Next.js, take a look at the following resources:
30+
31+
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
32+
- [Learn Next.js](https://nextjs.org/learn-pages-router) - an interactive Next.js tutorial.
33+
34+
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
35+
36+
## Deploy on Vercel
37+
38+
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
39+
40+
Check out our [Next.js deployment documentation](https://nextjs.org/docs/pages/building-your-application/deploying) for more details.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"paths": {
4+
"@/*": ["./src/*"]
5+
}
6+
}
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
reactStrictMode: true,
4+
};
5+
6+
export default nextConfig;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "example-app",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "next dev",
7+
"build": "next build",
8+
"start": "next start",
9+
"lint": "next lint"
10+
},
11+
"dependencies": {
12+
"next": "^15.3.4",
13+
"react": "^19.0.0",
14+
"react-dom": "^19.0.0",
15+
"urql": "^4.2.2"
16+
}
17+
}
Binary file not shown.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useRouteData } from "@/lib/context";
2+
import TemplateHierarchyInfo from "@/components/TemplateHierarchyInfo";
3+
4+
export default function Layout({ children }) {
5+
const { templateData, uri } = useRouteData();
6+
7+
return (
8+
<div className="layout">
9+
<TemplateHierarchyInfo template={templateData} uri={uri} />
10+
<header>
11+
<h1>Template Hierarchy Example</h1>
12+
<nav>
13+
<ul>
14+
<li>
15+
<a href="/">Home</a>
16+
</li>
17+
<li>
18+
<a href="/hello-world">Sample Post</a>
19+
</li>
20+
<li>
21+
<a href="/sample-page">Sample Page</a>
22+
</li>
23+
</ul>
24+
</nav>
25+
</header>
26+
<main>{children}</main>
27+
</div>
28+
);
29+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { gql } from "urql";
2+
import { useRouteData } from "@/lib/context";
3+
4+
export default function RecentPosts() {
5+
const { graphqlData } = useRouteData();
6+
7+
const posts = graphqlData?.RecentPosts?.data?.posts?.nodes || [];
8+
9+
if (graphqlData?.RecentPosts?.error) {
10+
console.error("Error fetching RecentPosts:", graphqlData.RecentPosts.error);
11+
return <div>Error loading recent posts.</div>;
12+
}
13+
14+
return (
15+
<div className="recent-posts">
16+
<h2>Recent Posts</h2>
17+
<ul>
18+
{posts.map((post) => (
19+
<li key={post.id}>
20+
<a href={post.uri}>{post.title}</a>
21+
</li>
22+
))}
23+
</ul>
24+
</div>
25+
);
26+
}
27+
28+
RecentPosts.query = {
29+
query: gql`
30+
query RecentPosts {
31+
posts(first: 5) {
32+
nodes {
33+
id
34+
title
35+
uri
36+
}
37+
}
38+
}
39+
`,
40+
};

0 commit comments

Comments
 (0)