Skip to content

Conversation

@ahuseyn
Copy link
Member

@ahuseyn ahuseyn commented Mar 20, 2025

Example: Rendering WordPress Blocks in Next.js

This example demonstrates rendering WordPress Blocks with JSX in a Next.js project. The example includes 16 block components across various categories. Example includes a utility to convert flat blocks data from GraphQL (GQL) response into the hierarchical data structure. Passing this data into BlockRenderer component generates the WordPress content by matching the appropriate blocks and using a default block when block implementation is missing. Default block is also customizable component. Example also gives an option to provide custom HTML parser to render HTML content.

Features

  • 16 sample block component
  • Custom HTML parser option
  • Default block for fallback
  • Custom default block option
  • Utility to form inline CSS
  • Utility to convert flat GQL data into hierarchical data
  • GraphQL fragments for each block

Important notes

  • This example does not require a working WordPress instance, it uses fake request to get prefetched GQL response
  • If you have a working WordPress instance you can easily connect it to this example. For the details check the pages/index.js file.
  • You will need to have wp-graphql-content-blocks and WPGraphQL plugins installed if you want to connect it to a WordPress instance.

Project structure

/
├── public/
│   ├── post.json                       # Example GraphQL post data
├── pages/
│   ├── index.js                        # Home page to showcase all blocks
├── components/
│   ├── Caption.js                      # Component for figcaption and caption elements
│   ├── DefaultBlock.js                 # Default block to render if there's no corresponding block
│   ├── BlockRenderer.js                # Component to render to render blocks
│   ├── blocks/
│   │   ├── CoreAudio.js                # Audio block component
│   │   ├── CoreButton.js               # Button block component
│   │   └── ...                         # Other block components
├── utils/
│   ├── flatListToHierarchical.js       # Convert GraphQL data into hierarchical
│   ├── getInlineStyles.js              # Parse inline styles
├── package.json                        # Project dependencies and scripts

Installation and Setup

Prerequisites

  1. Node.js 18.18 or later
  2. npm or other package manager

Clone the repository

git clone https://github.com/wpengine/hwptoolkit.git
cd examples/next/render-blocks-pages-router

Install dependencies

npm install

Start the development server

npm run dev

@ahuseyn
Copy link
Member Author

ahuseyn commented Mar 20, 2025

@moonmeister do you think it's fine enough or am I missing something? If you think it satisfies the ticket, we can open it for review.

@ahuseyn ahuseyn marked this pull request as ready for review March 21, 2025 09:35
@ahuseyn ahuseyn requested a review from a team as a code owner March 21, 2025 09:35
theodesp
theodesp previously approved these changes Mar 21, 2025
Copy link
Member

@theodesp theodesp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

josephfusco
josephfusco previously approved these changes Mar 21, 2025
Copy link
Member

@josephfusco josephfusco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahuseyn Nice work! I left some minor suggestions.

@ahuseyn ahuseyn dismissed stale reviews from josephfusco and theodesp via 30ccc9a March 21, 2025 19:17
@ahuseyn ahuseyn removed the request for review from moonmeister March 21, 2025 19:19
@ahuseyn
Copy link
Member Author

ahuseyn commented Mar 21, 2025

Thanks @theodesp and @josephfusco. I've committed your suggestions.

@ahuseyn ahuseyn requested a review from josephfusco March 21, 2025 19:20
@colinmurphy
Copy link
Member

@ahuseyn PR looks awesome 🚀

I added 2 small suggestions in the README.

Also happy to help out with setting up wp-env if that is something you want to use 😄

colinmurphy
colinmurphy previously approved these changes Mar 21, 2025
@ahuseyn ahuseyn changed the title docs: Blocks rendering in Next.js (pages/mocked graphql calls) feat: Blocks rendering in Next.js (pages/mocked graphql calls) Mar 24, 2025
@ahuseyn ahuseyn requested a review from theodesp March 24, 2025 10:12
@ahuseyn
Copy link
Member Author

ahuseyn commented Mar 24, 2025

@ahuseyn PR looks awesome 🚀

I added 2 small suggestions in the README.

Also happy to help out with setting up wp-env if that is something you want to use 😄

Thanks @colinmurphy. For this example we've decided to go with mock graphql requests, but I'm planning to add your wp-env implementation to Apollo example and other future examples.
Note: I'm going to apply your suggestions as well.

Copy link
Member

@colinmurphy colinmurphy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀 🚀 🚀

@colinmurphy
Copy link
Member

@ahuseyn PR looks awesome 🚀
I added 2 small suggestions in the README.
Also happy to help out with setting up wp-env if that is something you want to use 😄

Thanks @colinmurphy. For this example we've decided to go with mock graphql requests, but I'm planning to add your wp-env implementation to Apollo example and other future examples. Note: I'm going to apply your suggestions as well.

No worries. Happy to help once we have the other PR merged aswell for WP ENV and we can workout next steps :)

@ahuseyn ahuseyn added this pull request to the merge queue Mar 24, 2025
Merged via the queue into main with commit 10f9127 Mar 24, 2025
1 check passed
@ahuseyn ahuseyn deleted the example-next-render-blocks branch March 24, 2025 11:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants