这是无头的WordPress! ? -https://nextjswp.com
这是一个裸露的next.js应用程序,它通过wpgraphql从WordPress获取数据,并使用tailwindcss对其进行了样式。
请将其视为下一个无头WordPress项目的起点。
另外,这真的非常快!
git clone [email protected]:gregrickaby/nextjs-wordpress.gitnpm i.env.local文件cp .env.example .env.local在.env.local中自定义URL以匹配您的WordPress设置:
# WordPress GraphQL API URL. No trailing slash.
NEXT_PUBLIC_WORDPRESS_GRAPHQL_URL="https://blog.nextjswp.com/graphql"
# WordPress REST API URL. No trailing slash.
NEXT_PUBLIC_WORDPRESS_REST_API_URL="https://blog.nextjswp.com/wp-json/wp/v2"
# Optional. JWT auth refresh token.
#NEXTJS_AUTH_REFRESH_TOKEN=""
# Preview Secret. Must match the constant in wp-config.php.
NEXTJS_PREVIEW_SECRET="preview"
# Revalidation Secret. Must match the constant in wp-config.php.
NEXTJS_REVALIDATION_SECRET="revalidate"next.config.js在next.config.js中更新URL以匹配您的WordPress网站:
/** @type {import('next').NextConfig} */
const nextConfig = {
images : {
remotePatterns : [
{
protocol : 'https' ,
hostname : '*.nextjswp.**' // <-- Change to your WordPress site
}
]
}
}
module . exports = nextConfig/lib/config.ts更新内容以匹配您的WordPress网站:
const config = {
siteName : 'Next.js WordPress' ,
siteDescription : "It's headless WordPress!" ,
siteUrl : 'https://nextjswp.com'
}您需要具有以下插件的本地或公共WordPress网站:
安装上述所有插件后,您需要在wp-config.php文件中添加一些常数:
// The URL of your Next.js frontend. Include the trailing slash.
define ( ' NEXTJS_FRONTEND_URL ' , ' https://nextjswp.com/ ' );
// Optional. JWT auth refresh token.
//define( 'GRAPHQL_JWT_AUTH_SECRET_KEY', '' );
// Any random string. This must match the .env variable in the Next.js frontend.
define ( ' NEXTJS_PREVIEW_SECRET ' , ' preview ' );
// Any random string. This must match the .env variable in the Next.js frontend.
define ( ' NEXTJS_REVALIDATION_SECRET ' , ' revalidate ' );为了查询草稿帖子的预览,您需要使用WordPress进行身份验证。以下是一个一次性步骤:
wp-config.php文件,然后将字符串粘贴到GRAPHQL_JWT_AUTH_SECRET_KEY contents中。 (请确保通过删除//符号来散布常数。) // Optional. JWT auth refresh token.
define ( ' GRAPHQL_JWT_AUTH_SECRET_KEY ' , ' some-random-string-generated-by-wp-salt ' );your_username和your_password ) mutation Login {
login (
input : {
clientMutationId : " uniqueId "
password : " your_password "
username : " your_username "
}
) {
refreshToken
}
}refreshToken返回的更新.env.local文件,然后将refreshToken粘贴到NEXTJS_AUTH_REFRESH_TOKEN变量中。 (请确保通过删除#符号来散布变量。) # Optional. JWT auth refresh token.
NEXTJS_AUTH_REFRESH_TOKEN="refresh-token-generated-by-grapqh-query"现在,您应该能够通过单击WordPress管理员中的“预览”按钮来预览下一个。
npm run dev开发服务器启动后,您可以查看前端:http:// localhost:3000
GraphQL是有效的,因为我们可以在单个请求中查询多个端点。如果我们要使用WordPress REST-API,则需要对每个端点进行多次往返。
我们可以在GraphIQL(或您喜欢的REST客户端)中构建查询,然后让JSON.stringify()格式化它。因为这是所有标准的JavaScript,所以我们甚至可以将变量传递给我们的查询 - 不需要第三方套餐!
这是获取单个帖子(基于slug)的查询,特色图像,作者元,类别,标签,SEO和帖子注释:
import { Post } from '@/lib/types'
/**
* Fetch a single post by slug.
*/
export async function getPostBySlug ( slug : string ) {
// Define our query.
const query = `
query GetPost($slug: ID!) {
post(id: $slug, idType: SLUG) {
databaseId
content(format: RENDERED)
title(format: RENDERED)
featuredImage {
node {
altText
mediaDetails {
sizes(include: MEDIUM) {
height
width
sourceUrl
}
}
}
}
author {
node {
avatar {
url
}
name
}
}
date
tags {
nodes {
databaseId
name
}
}
categories {
nodes {
databaseId
name
}
}
seo {
metaDesc
title
}
comments(first: 30, where: {order: ASC}) {
nodes {
content(format: RENDERED)
databaseId
date
status
author {
node {
avatar {
url
}
email
name
url
}
}
}
}
}
}
`
// Define our variables.
const variables = {
slug : slug
}
// Fetch the data using a reusable fetch function. Next.js
// automatically memoizes and caches these requests.
const response = await fetchGraphQL ( query , variables )
// Return the post.
return response . data . post as Post
}此存储库不使用第三方GraphQl软件包,因为Next.js会在我们的自定义fetch函数中自动记忆fetch()请求。这意味着,如果我们两次获取相同的数据,则Next.js只会向WordPress提出一个请求。
如果您希望使用第三方GraphQl软件包,只需将自定义
fetchGraphQL()函数与您选择的软件包交换。
请记住,将所有环境变量从.env.local添加到Vercel或Netlify上的生产环境。
RSS feed和站点地图可用:
预览可用:
欢迎捐款!请参阅贡献指南以获取更多信息。