So yeah, let's get started then since we're a few minutes past the hour. So thank you, first and foremost, for being here, for tuning in for this workshop. I'm really excited about it.
The title of this workshop, as you're well aware already, is Build a Headless WordPress app with Next.js and WP GraphQL. So what we'll do is, I have a few slides to go over. I'll share my slides, introduce myself, and then go through a few topics, such as what is Headless WordPress, why might you want to consider it.
And after we get through those slides, then we'll dive into the live coding workshop part you actually clone down a Next.js application, get that up and running on your local machine, and then we'll see how we can get that hooked up to a WordPress backend so that we're pulling data from that WordPress backend via GraphQL, then using it to render the pages of our Next.js app. And we'll walk through a few different types of pages and see how each one is built. So I hope you really enjoy it and get a lot out of it.
All right. So let's build a headless WordPress app with Next.js and WP GraphQL. All right. So to start off, I'll introduce myself. So my name is Kellan Mace. I work at a company called WP Engine, which is one of the most popular hosting platforms on the web for WordPress sites, specifically. And it's relevant to this audience and this talk is the fact that WP Engine recently, we launched this new hosting platform called Atlas, which is geared toward headless WordPress WordPress sites even.
So you can, with a single account and a single hosting provider, you can host both the WordPress backend as well as your front-end JavaScript application all in the same spot, which is pretty cool. So check out Atlas if that sounds interesting to you. My Twitter handle is here as well, if you wanna get in touch with me after the conference. DMs are open, so please if you're diving into this stuff and have some questions, please reach out and let me know here. So with that we'll dive in.
I know I start with just a kind of disambiguating or calling, drawing attention to the differences between traditional WordPress and then Headless, and why you might wanna go one way versus the other, as I said. So let's do that.
Oh yeah, before we do that, just one slide here, and that is clone the app repo. I wanted to put this right at the top of the slide deck here, just because it might take a minute depending on your internet connection. But for you to clone down this repository, and then you can follow the steps in the readme there. They'll tell you to CD inside of the directory of this project, and then run npm install to get the dependencies installed, and so on. So if you do that now, that'll save you a minute once you get to the live coding portion later. Then you won't have to wait for the dependencies to be installed and so on. So you can go ahead and grab that that link in the slide on this slide.
All right, so with that, let's talk about traditional or monolithic WordPress. So as many of you on the call probably already know WordPress has been around for a while. So we're world's most popular content management system. At this point the statistic is pretty mind blowing. It's like WordPress has achieved about 43% market share on the internet. That's the percentage of sites online that are running that which is just just mind boggling to think about.
In traditional WordPress, WordPress has a lot of jobs, it's responsible for providing the admin interface that your content creators log into, when they create and edit and manage their content, it's responsible for saving the data to the database. And then when requests come in from, you know, website visitors WordPress is also responsible for pulling the data out of the database, and then templating it out using its theme API, as HTML, and finally responding to that request. So it wears quite a few hats and does a lot of jobs. And in many cases on the web, this is a great setup and works really well.
But more and more people are choosing to adopt a headless or decoupled architecture. So we'll talk about that next year. So that looks something like this, where you have WordPress is still used. So you can see it on the left of this slide here. But it's used really only to provide that nice admin experience for your content creators and to store the data. But it doesn't handle any rendering, it doesn't return, you know, any HTML to the site visitor. Instead, you have a front-end application that does that and that's Next.js in our case on the workshop is what we'll use for that. So you have Next.js and it handles you know, querying the data from WordPress and using that to render out HTML pages that site visitors are served. And in between here, you have to have some kind of API layer. So you need some way for your javascript front end and your WordPress back end to communicate with one another.
One way to do that is to use the REST API that's just built into native WordPress. I would argue though that a better choice these days is WPGraphQL. So that's the logo that you see here at the bottom. WPGraphQL is a free open source plugin for WordPress that turns any WordPress site into a GraphQL API, which is very powerful. So then you get all the benefits of GraphQL. In REST API architecture, if you wanted to query for blog posts and users and categories and something else, you might have to hit multiple REST API endpoints just to aggregate, you know, just to get all the data that you need, which can be time-consuming and cause a performance hit. But with WPGraphQL, you have the benefits of GraphQL where you can enter your WordPress data graph at any point and then from there, query however you want. You can say, I want the first 10 blog posts, and for each of those authors, I want their name and other posts they've written. And for each of those, I want the categories and kind of compose these nested queries and get all of that data back in a single network request, which is very powerful. And it has other benefits, too, but that's I would say one of the main ones. This is what kind of a decoupled architecture looks like. And this is the thing that we'll actually be building today.
The next question after you've, you know, heard me talk about traditional versus decoupled is, okay, but why? Why would I, you know, choose one versus the other? What are the benefits there, right? So let's cover that next real quick. So some of the benefits of going headless or for a decoupled architecture would be these. So your content creators get to keep their CMS. If you talk to many like marketing teams at many companies, they really love and know and, you know, rely on WordPress, and they want to keep using that as their preferred CMS. So they get to with a headless setup. Performance and scalability, you know, frameworks like Next.js make it very easy to get very fine-grained and say, I want this route to be fully static. This other route to be server-side rendered. This other route, I would do some client-side rendering. You get your very fine-grained control over, you know, performance and how each of your pages is built, and it can scale very well as well. Third one is platform agnostic backend. This is kind of an interesting one to think about. So with a headless or decoupled approach, your WordPress backend, it just serves up pure JSON data. So that means you could have a web client, like a Next.js app that pulls that JSON and renders it for the web client. But if you wanted to, you could also build an iOS client, an Android client, a desktop client, and all of these apps could source their data from the same GraphQL endpoint, you know, the same JSON data that the web client uses. Other platforms could use and consume as well. So that makes it pretty powerful if you have, you know, multiple platforms that you need to support. Next one is easy to pull data from multiple sources. This would be like at build time when your static pages of your site are being built, let's say, if you wanna pull some data from WordPress, some data from Salesforce, data from the YouTube API, some data from Contentful or whatever else. It makes it...modern frameworks like NOCS make it very easy to do that, to source data from all these, you know, sources, and then stitch it all together into your HTML pages. Next one is increased security. I've seen site architectures where they'll do something like they'll have a JavaScript frontend that serves the website to their site visitors, call it the WordPress backend, where their content creators manage content. And then that WordPress backend, they'll lock it down so that only certain IP addresses are allowed to even connect to that WordPress install and log in to manage the content. Anyone else on the entire internet who attempted to, you know, WordPress admin to try to get in would be disallowed. Since they don't have, you know, one of those allowed IP addresses, for example. In traditional WordPress, you couldn't do that because everyone needs to be able to access, you know, that web address where WordPress lives because it's doing all the rendering, right? Your site visitors need to visit that. When you have a decoder approach, that's no longer the case. You can do some tricks where you lock down access to the WordPress admin while letting your site visitors see the front-end JavaScript app here. And the last one on this list is improved developer experience.
Comments