This Next.js app replicates the WordPress Template Hierarchy, but in JavaScript

Overview

Next.js WordPress Template Hierarchy Routing

This Next.js app replicates the WordPress Template Hierarchy, but in JavaScript!

Requirements

WordPress Plugins:

  • WPGraphQL: Transforms WordPress data into a GraphQL schema.
  • FaustWP: Used to decipher which templates a nodeByUri query returns.

Getting Started

Install dependencies:

npm install

To set the WordPress GraphQL endpoint (Otherwise, the default will be used):

export NEXT_PUBLIC_GRAPHQL_ENDPOINT=https://your-wp-instance.com/graphql

Run the dev server:

npm run dev

Now, navigate to any route (i.e. http://localhost:3000/sample-page), and in your server's stdout, you can see the possible templates to create! For example, "single" is a possible template for posts. In that case you would create pages/single.js, and all posts will be routed to that page. Additionally, you can also get more granular. For example, if a post is named "Hello World" with a slug of "hello-world", you could create pages/single-sample-page.js and the post with that slug will be routed to that page.

Check out the WordPress template hierarchy for more info.

How it works

This example project works by first disabling the native file based routing in Next.js, and then using a Next.js custom server to handle the routing ourselves. From there, we make a nodeByUri GraphQL request based on the server's Request url to get some basic info about the route: the id, slug, templates, etc. From there, we determine what templates are available from the pages directory, and render the most specific template available.

Caveats

  1. Currently, this approach does not support Static Site Generation (SSG). The render() method used in the a Next custom server will not properly pass the query argument to getStaticProps, which is needed to properly pass the seedQuery
  2. A Next.js custom server is needed for this to work. This means you can not deploy to Vercel, Netlify. However, you can leverage a provider like WP Engine Atlas, which operates as a node server, not serverless functions like Vercel, Netlify, etc.
You might also like...

An experimental plugin to preview and insert block patterns in WordPress.

An experimental plugin to preview and insert block patterns in WordPress.

Block Pattern Explorer The Block Pattern Explorer is an experimental WordPress plugin based heavily on the work currently being done in Gutenberg. The

Oct 19, 2022

Custom Gutenburg blocks to add custom functionalities to your WordPress site. Brought to you by Arif Khan with 3

Custom Gutenburg blocks to add custom functionalities to your WordPress site. Brought to you by Arif Khan with <3

Gutpress(WordPress Gutenburg Block Plugin) Custom Gutenburg blocks to add custom functionalities to your WordPress site. Brought to you by Arif Khan w

Nov 23, 2022

A Simplized Wordpress Blog Theme Design & Developed from 2broear.com by 2BROEAR Released & openSourced in 2022.

A Simplized Wordpress Blog Theme Design & Developed from 2broear.com by 2BROEAR Released & openSourced in 2022.

2BLOG THEME A Simplized Wordpress Blog Theme Design & Developed from 2broear.com by 2BROEAR Released & openSourced in 2022. Preview Site :演示站点 (演示并不代表

Dec 8, 2022

Theme Redone is a custom WordPress theme starter/framework with its own Gutenberg blocks solution and a CLI that speeds up the block creation process.

Theme Redone is a custom WordPress theme starter/framework with its own Gutenberg blocks solution and a CLI that speeds up the block creation process.

Theme Redone The Framework for Developing Custom WordPress Themes with its own Gutenberg Blocks creation solution. Theme Redone is a custom WordPress

Dec 30, 2022

A fullstack next.js template with all the fun stuff like next auth, mongodb, prisma, chakra ui

A fullstack next.js template with all the fun stuff like next auth, mongodb, prisma, chakra ui

Welcome to FullStack Next.js template 👋 A fullstack next.js template with all the fun stuff like next auth, mongodb, prisma, chakra ui ✨ Demo Tech Ne

Oct 16, 2022

To Do list is a small but useful project to make list app , you can add tasks delete tasks and modify tasks, the project built using HTML, CSS, JavaScript

Project Name The To-Do-List app description this is a project in the second week of the second module in microverse. its a useful to do list that save

Jul 25, 2022

Kaol Stack - Prisma, Expo, Next, TRPC, Solito, Tailwind - A monorepo template for a truly universal app

Kaol Stack - Prisma, Expo, Next, TRPC, Solito, Tailwind - A monorepo template for a truly universal app

Kaol Stack 🔦 About A monorepo with Prisma, Next.js, Expo, tRPC, Authentication and Solito setup and configured to work together. With this setup you

Dec 21, 2022

This is a Next.js project bootstrapped with create-next-app.

Getting Started First, run the development server: npm run dev # or yarn dev Open http://localhost:3000 with your browser to see the result. You can s

Dec 5, 2022

A little robot that tells some jokes! I can't guarantee, but it is a funny app

A little robot that tells some jokes! I can't guarantee, but it is a funny app

A little robot that tells some jokes! I can't guarantee, but it is a funny app. It works calling a joke API to get a random joke and then pass the joke to a text to speech API. All of that using Javascript, HTML and CSS. I hope you enjoy it.

Jul 28, 2022
Owner
Blake Wilson
Blake Wilson
A Virtual Interactive Keyboard which replicates every key you press and a Text-Area in which everything is written and can be copied to the clipboard with a click of a button.

A Virtual Interactive Keyboard which replicates every key you press and a Text-Area in which everything is written and can be copied to the clipboard with a click of a button.

Devang Joshi 1 Mar 1, 2021
Download Notion pages as markdown and image files, preserving hierarchy and enabling workflow properties. Works with Docusaurus.

notion-pull notion-pull lets you use Notion as your editor for markdown-based static site generators like Docusaurus. Using Notion instead of raw mark

SIL LSDev 46 Jan 7, 2023
💻 A simple Create Next App template to start your projects with Next.js, TypeScript, ESLint, Prettier and other tools.

⚡ Next Typescript Template ⚡ A simple Create Next App template to start your projects with Next.js, TypeScript, ESLint, Prettier and other tools. Quic

João Gabriel 13 Nov 23, 2022
Small (fragile) script for migrating comments from dev.to posts to Wordpress format (WXR/XML)

dev-to-wxr Small (fragile) script for migrating comments from dev.to posts to Wordpress format (WXR/XML). Useful for importing in tools like disqus. U

Fahad Hossain 2 Jan 29, 2022
A dashboard for managing orders and inventory for a wordpress e-commerce site which has woo commerce plugin installed

WordPressWooCommerceDashboard - A dashboard for managing orders and inventory for a wordpress e-commerce site which has woo commerce plugin installed. This program provides shipping tracking for Delhivery.

Vikrama Reddy 1 Jan 3, 2022
Personal Blog - a project developed with Angular for the front-end interface and Wordpress for the back-end API served with Docker containers

PersonalBlog This project was generated with Angular CLI version 13.0.1. Front-end Interface Development server Run ng serve or ng serve --configurati

null 9 Oct 5, 2022
Integrate Tailwind with the @wordpress/create-block script.

Tailwind Blocks Example plugin demonstrating how to integrate Tailwind with the @wordpress/create-block script. The plugin was scaffolded using @wordp

David Gwyer 25 Nov 13, 2022
Turn any dynamic website (especially wordpress) into a fast, secure, stable static site

Static site publisher Turn any dynamic website (especially wordpress) into a fast, secure, stable static site Reduced complexity - no need to run simp

Alex Ivkin 7 Apr 6, 2022
WordPress Gutenberg plugin to display the attributes for the currently selected block in the Document sidebar.

Block X-ray Attributes Stable Tag: 1.2.0 Requires at least: 5.5 Tested up to: 5.9 Requires PHP: 7.2 License: GPL v2 or later Tags: block attributes, g

Sal Ferrarello 38 Mar 18, 2022