Git Product home page Git Product logo

next-wordpress-starter's Introduction

Next.js WordPress Starter

All Contributors

Scaling WordPress with the power of Next.js and the static web!

⚑️ Quick Start

Deploy to Netlify Deploy with Vercel

Requirements

yarn create next-app -e https://github.com/colbyfayock/next-wordpress-starter
# or
npx create-next-app -e https://github.com/colbyfayock/next-wordpress-starter

Add an .env.local file to the root with the following:

WORDPRESS_GRAPHQL_ENDPOINT="http://wordpressite.com/graphql"

In some cases, the above may not work. Change it as follows:

WORDPRESS_GRAPHQL_ENDPOINT="http://yourhost.com/index.php?graphql

πŸš€ Getting Started

What is this and what does it include?

The goal of this project is to take WordPress as a headless CMS and use Next.js to create a static experience without any 3rd party services that can be deployed anywhere.

The hope is to build out as many features as we can to support what's typically expected from an out of the box theme on WordPress. Currently, those features include:

Additionally, the theme is expected to be SEO friendly and performant out of the box, including:

  • Unique page titles
  • Unique descriptions
  • Open Graph tags

You can also optionally enable Yoast SEO plugin support to supercharge your SEO! (See below)

Check out the Issues for what's on deck!

Want something a little more basic? Check out my other starter with an MVP setup to get up and running with WPGraphQL in WordPress: https://github.com/colbyfayock/next-wpgraphql-basic-starter

Requirements

Environment

This project makes use of WPGraphQL to query WordPress with GraphQL. In order to make that request to the appropriate endpoint, we need to set a environment variable to let Next.js know where to request the site information from.

Create a new file locally called .env.local and add the following:

WORDPRESS_GRAPHQL_ENDPOINT="[WPGraphQL Endpoint]"

Replace the contents of the variable with your WPGraphQL endpoint. By default, it should resemble [Your Host]/graphql.

Note: environment variables can optionally be statically configured in next.config.js

All Environment Variables

Name Required Default Description
WORDPRESS_GRAPHQL_ENDPOINT Yes - WordPress WPGraphQL endpoint (ex: host.com/graphl)
WORDPRESS_MENU_LOCATION_NAVIGATION No PRIMARY Configures header navigation Menu Location
WORDPRESS_PLUGIN_SEO No false Enables SEO plugin support (true, false)

Please note some themes do not have PRIMARY menu location.

Development

To start the project locally, run:

yarn dev
# or
npm run dev

The project should now be available at http://localhost:3000!

ESLint extension for Visual Studio Code

It is possible to take advantage of this extension to improve the development experience. To set up the ESLint extension in Visual Studio Code add a new folder to the root .vscode. Inside add a file settings.json with the following content:

{
  "editor.formatOnSave": false,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  }
}

With this file ESLint will automatically fix and validate syntax errors and format the code on save (based on Prettier configuration).

Deployment

Netlify

There are two options as to how you can deploy this project to Netlify:

Essential Next.js Plugin should be provided as an option when you're first importing a project based on this starter. If it's not, you can install this plugin using the Netlify Plugins directory. This will allow the project to take full advantage of all native Next.js features that Netlify supports with this plugin.

Exporting the project lets Next.js compile the project into static assets including HTML files. This allows you to deploy the project as a static site directly to Netlify just like any other site. You can do this by adding next export to the end of the build command inside package.json (ex: next build && next export).

Regardless of which option you choose, you can configure your environment variables either when creating your new site or by navigating to Site Settings > Build & Deploy > Environment and triggering a new deploy once added.

Deploy to Netlify

Vercel

Given Next.js is a Vercel-supported project, you can simply import the project as a new site and configure your environment variables by either adding them during import or by navigating to Settings > Environment Variables and triggering a new build once added.

Deploy with Vercel

πŸ›  Configuring Your Project

package.json

In order to avoid an additional configuration file, we take advantage of some built-in properties of package.json to configure parts of the website.

Name Required Description
homepage Yes Homepage or hostname used to construct full URLs (ex Open Graph)
  • homepage: Setting the homepage property will update instances where the full URL is required such as Open Graph tags

WordPress

This project aims to take advantage of as many built-in WordPress features by default like a typical WordPress theme. Those include:

Name Usage
Site Language lang attribute on the <html> tag
Site Title Homepage header, page metadata
Tagline Homepage subtitle

There is some specific WordPress configuration required to allow for the best use of this starter.

Images

This Starter doesn't currently provide any mechanisms for dealing with image content from WordPress. The images are linked to "as is", meaning if the image is uploaded via the WordPress interface, the image will be served from WordPress.

To serve the images statically, you have a few options.

Jetpack

By enabling the Image Accelerator from Jetpack, your images will automatically be served statically and cached via the wp.com CDN. This feature comes free with the basic installation of Jetpack, requiring only that you connect the WordPress site to the Jetpack service.

Jetpack CDN

πŸ”Œ Plugins

Yoast SEO

The Yoast SEO plugin is partially supported including most major features like metadata and open graph customization.

Requirements

  • Yoast SEO plugin
  • Add WPGraphQL SEO plugin

To enable the plugin, configure WORDPRESS_PLUGIN_SEO to be true either as an environment variable or within next.config.js.

πŸ₯Ύ Bootstrapped with Next.js WordPress Starter

Examples of websites that started off with Next.js WordPress Starter

πŸ’ Sponsors

WordPress hosting for the public-facing project provided by WP Engine.

WP Engine Logo

✨ Contributors

Thanks goes to these wonderful people (emoji key):

Colby Fayock
Colby Fayock

πŸ’» πŸ“–
Kevin Cunningham
Kevin Cunningham

πŸ’»
Guillermo Angulo
Guillermo Angulo

πŸ’»
Hein Snyman
Hein Snyman

πŸ’»
Grische
Grische

πŸ”§
Jatin Rathee
Jatin Rathee

πŸ’»
Dave
Dave

πŸ’»
Brad Garropy
Brad Garropy

πŸ’»
FΓ‘bio Sales
FΓ‘bio Sales

πŸ’»
Leonardo Losoviz
Leonardo Losoviz

πŸ’»
Avneesh Agarwal
Avneesh Agarwal

πŸ’»
Phattarapol L.
Phattarapol L.

πŸ’»
Peter Cruckshank
Peter Cruckshank

πŸ’»
Shane O'Grady
Shane O'Grady

πŸ’»
Nick Gaswirth
Nick Gaswirth

πŸ’»
alexandruvisan19
alexandruvisan19

πŸ’»
Ritik Chourasiya
Ritik Chourasiya

πŸ”§
Rick Knowlton
Rick Knowlton

πŸ’»
Jedidiah Amaraegbu
Jedidiah Amaraegbu

πŸ“–

This project follows the all-contributors specification. Contributions of any kind welcome!

next-wordpress-starter's People

Contributors

alexandruvisan19 avatar allcontributors[bot] avatar amjedidiah avatar avneesh0612 avatar bradgarropy avatar colbyfayock avatar dave-zengenti avatar depfu[bot] avatar doingandlearning avatar ffabiosales avatar gaswirth avatar grische avatar guilleangulo avatar heinsnyman avatar jatin-rathee avatar lanbau avatar leoloso avatar petercr avatar phattoz avatar rickknowlton avatar shaneog avatar theritikchoure avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

next-wordpress-starter's Issues

Add ability to navigate search results with keyboard arrow keys

When someone makes a search, we show a list of results that match that search

We should allow someone to navigate that list using their arrows keys to change the focus of which element is selected, in addition to the already supported-by-default tab key

The feature needs to not negatively impact accessibility

The search form and results are inside of the Nav.js component:
https://github.com/colbyfayock/next-wordpress-starter/blob/master/src/components/Nav/Nav.js

Pinned post support

Content editors should be able to pin a blog post using the WordPress editor

In WordPress this feature is called "Sticky"

I've gone ahead and made the post with slug i-love-you-buddy sticky. If you look in the JSON response, the post that's sticky has an attribute of sticky: true.

http://54.243.240.9/wp-json/wp/v2/posts

You can make multiple posts sticky.

We probably will want to find the posts that are sticky within the posts array, pull them out, and append them to the top of the posts list when rendering them to the page

We should additionally add some kind of icon to identify a sticky post. Maybe a pin icon? I'm open to suggestions. The project includes react-icons and currently makes use of Font Awesome. We should use an icon that matches that style.

Questions

  • Should the "sticky" posts show twice? Once at the top and once in it's natural location? How does Twitter handle pinned Tweets? What does a typical WP site do?

Page 404

Description

Currently there isn't a custom 404 page and when landing at a page that doesn't exist, the default message is "An unexpected error has occurred.". In Next.js creating a custom 404 page is as easy as creating a pages/404.js file , so I thought it would be a nice adition. Anytime trying to access a page that doesn't exist, it will fall to that page.

About the content it could be a simple message like Page Not Found and a link to go back to homepage? What do you think?

Safari Invalid Date

I noticed that in Safari, when going particularly to a Post page, the page fails to render due to an Invalid Date issue.

Looks like in the GraphQL query, the modified date doesn't return in a correct format

When querying for modified it comes as:

2021-01-24 02:34:18

Which is invalid when adding to a new Date() instance.

But what's needed is

2021-01-24T02:34:18

Which is available inside of modifiedGmt

is this a WPGraphQL issue? expected?

Better meta descriptions

I set up meta descriptions in to basic patterned values with the goal of getting something in there.

It would be great to write and customize these to look a bit better. They still need to be a pattern as the use case here is that the pages would change

Future work can include a tutorial about how to add ACF or Yoast to customize this from within WP

Please propose the written solutions before doing the work

WordPress Plugin support

Ideally the starter could easily support common plugins

How can we provide opt-in support to things like ACF without adding a bunch of code right into the starter? can we?

Sitemap

We should provide an automatically generated sitemap.xml file

Can we take advantage of the work we did with the search index?

Is there a plugin we can roll this into dynamically?

Site Footer

The footer should be updated to include important details such as:

  • Latest posts?
  • Link to all posts
  • Website info + copyright
  • Categories?
  • Pages?

What's typical in a "standard" WordPress site?

Menu support

WordPress allows the ability to manage menus within the editor to easily make changes to navigation and other menus.

The goal of this will be to seed this functionality into the project with a starting point of making it possible to change the Navigation as a Menu

If there is no Navigation menu created, the project should work as it is (or propose a different default configuration) so that not having a Navigation Menu wouldn't break the site

If there is a Navigation Menu, that menu should be used to fill out the global site nav

Debug option at plugins

Just an idea:

Currently the plugins have 4 lines of console log each if everything goes without any errors:

[SearchIndex] Compiling file public/wp-search.json
[SearchIndex] Successfully fetched posts from http://xxxxxxxxx/graphql
[SearchIndex] Created directory ./public
[SearchIndex] Successfully wrote file to public/wp-search.json

Making a total of 12 lines if all plugins are passed. I was thinking that maybe is a good idea to just log 1 line (per plugin) if everthing goes as supossed, something like: Successfully created: wp-search.json.

In order to not lose all the useful console logs, we can grab a new boolean variable debug at each plugin:

const { env, outputDirectory, outputName, debug = false } = nextConfig;

And set all console logs with an && operator based on that debug variable.

debug && console.log('....')

Like so, by default only one line is logged (a new final log added after plugin.postcreate, not conditional). The errors will still work as they are handle as throw new Error. Also in case is needed, we can pass the variable debug = true at next.config.js and activate all the logs accross the code.

shot

What do you think?

Documentation

We'll want ot add documentation for all of the features that can be configured

I think it would be nice to use WordPress to build the documentation - that way we're able to develop features around a real WordPress site

  • Updating the homepage via package.json
  • Updating / generating favicons
  • Updating site manifest file

Display post category and tags

We should show the categories and tags of posts

This ideally can be configurable so someone can turn them off from the display

We should also include these attributes in the search index, both in the search-index-compiler.js and in the fuse.js instance inside of useSearch

RSS Feed

We should generate an RSS feed

Can we take advantage of the work we're already doing for the search index?

Ignore OG Image Build Directory

  1. Would be interesting to add public/images/ to gitignore? (the socialImages plugin is pretty cool πŸ’ͺ )

Originally posted by @GuilleAngulo in #119 (comment)

Do we want to move the output directory to something like /og/ and ignore that? /images is a common directory name, so probably want to avoid using that if we're adding it to .gitignore

Related Posts

Could we do a simple implementation of "related posts"?

A basic idea could be loading more posts from the category of the active post

They could be added at the bottom of the page. More from [category]

Use Blocks instead of Rendered Content

The post content is currently using standard rendered content and using dangerouslySetInnerHTML to set that HTML content. We would ideally use Blocks to take full advantage of the Gutenberg editor when building content.

Typography

I'd really like to work on providing a great reading experience with this Starter by default

I'm not sure if there are any good drop-ins or if we want to manually work through the typography, but it definitely needs work, as it's kind of just droppped in right now

open to ideas to discuss this, but the solution should be CSS based

Add RSS Link to Website

New functionality was added in #81 to provide a feed.xml file that includes the blog posts.

We should now add the link to the feed somewhere in the UI that let's people easily find it should they want to subscribe to a page's feed.

This might work well in the footer? Perhaps a new column in the supplemental footer along with the category links? then we can add additional links there as needed for site related things

Pagination

Determine a strategy for pagination or loading more results for a list of posts

Questions

  • Should it be "load more"?
  • Should we use traditional pagination with numbers, next, and prev?
  • Would the pagination be page-based? Would that use getStaticPaths?

Before this is picked up, please propose a plan for moving forward on how to go about this both technically and how it will appear in the UI

ESC key should close search dialogue

When making a search, we should a dropdown with autocomplete results

If that dropdown is open and someone clicks outside of that box, we clear the search and close the modal

If someone presses ESC while that dropdown is open, we should similarly cleare the search and close the modal

we can use the document onClick functionality as an example of how to implement in Nav.js

addDocumentOnClick, removeDocumentOnClick, and handleOnDocumentClick

Add Search

WP Rest seems to have a search endpoint we can take advantage of

  • Search index exists using Webpack Compiler (can this be improved)?
  • Add a React hook that can be used throughout the application to make quick searches
  • Add Fuse.js for fuzzy search
  • Add clientside search to homepage

Post comments support

Description

Should the site have support for post comments? Would be an interesting feature? I'm thinking as starting point it could be:

  • Adding this new field at Metadata (at PostCard) with the number of comments per post
  • Render comments details at post detail page.

It implies to add the count of comments at getAllPosts query, and comments data and lib files (to manage its own queries and functions). What do you think?

Screenshots

comments

Add stylized code snippets

We should take advantage of libraries that allow us to provide syntax highlighting to code blocks. This may depend on moving to using Blocks for content #4

Questions

  • How can this be configured within the content editor?

Static Media

Should we pull down media on compile and download it locally or reference those items via a CDN like WordPress Photon?

WordPress Theme to Streamline Installation

There are a few core things needed to make this work with WordPress, maybe we can figure out a way to make a WordPress theme to easily get that done

Plugins

  • WP GraphQL
  • WP GraphiQL

Settings

  • Metadata: Twitter handle for twitter:site meta tag

Author URL should be based on Name

Currently because of the way that we have to query user data via GraphQL, we're using the slug for the author pages

We can instead parameterize the author's full name providing a friendlier URL

https://github.com/fyalavuz/node-parameterize

Doing this consistently, then using that value to look up the parameterized versions of the author's name in the all users query which we're already making can help us look that user up to grab their details

Parent / Child Page Support

We should support the ability to have both Parent and Child pages

I'm not sure how this would currently compile, but we need to investigate that and where the path would be

If for instance, we have a Parent / Child relationship with a parent page called Docs with a slug of docs and a child page of Getting Started with a slug of getting-started, the page should be available at /docs/getting-started

Some ideas around this:

  • Maybe by default we can provide a left or right sidebar on parent pages that have child pages with a list of all of those pages
  • Do we want to add dropdowns to the navigation menu items for the first level children?
  • We should have breadcrumbs on child pages that link to the top level page
  • How many levels of children does wordpress support?

Please propose a solution and plan before getting started on this

Use GraphQL to query WordPress data

Currently, we're using the WordPress REST API to fetch page data. This works, but we can optimize and simplify requests especially when moving into more complex features by using GraphQL queries using the WordPress GraphQL API.

Questions

  • Would this require an additional plugin or does this work "out of the box"?

Date fields

Add date fields like created and last edited to posts

SEO & Metadata

Verify all pages include unique meta tags including title, description, and open graph

There's a separate ticket to explore dynamic social media imges

Should also include generating a favicon image

  • Page Titles
  • Descriptions
  • og:url

Images will be handled in #13

Favicon

Should we include a different favicon or use what comes with the default Next.js template?

Adding a custom Favicon should be an easy thing to do from a Starter user perspective

Server Error => Error: Unexpected token < in JSON at position 0

This is what i get when i run command yarn dev

$ next dev
Loaded env from /mnt/f/Projects/next-wordpress-starter/.env.local
ready - started server on http://localhost:3000
[Feed] Compiling file public/feed.xml
[SearchIndex] Compiling file public/wp-search.json
[Feed] Failed to fetch metadata from http://172.26.32.1/wordpress/graphql: Error: Unexpected token < in JSON at position 0
[SearchIndex] Failed to fetch posts from http://172.26.32.1/wordpress/graphql: Error: Unexpected token < in JSON at position 0
[SearchIndex] Created directory ./public
[SearchIndex] Successfully wrote file to public/wp-search.json
[Feed] Failed to fetch posts from http://172.26.32.1/wordpress/graphql: Error: Unexpected token < in JSON at position 0
[Feed] Created directory ./public
[Feed] Successfully wrote file to public/feed.xml
event - compiled successfully
event - build page: /next/dist/pages/_error
wait  - compiling...
[SearchIndex] Compiling file public/wp-search.json
[Feed] Compiling file public/feed.xml
event - compiled successfully
[SearchIndex] Failed to fetch posts from http://172.26.32.1/wordpress/graphql: Error: Unexpected token < in JSON at position 0
[SearchIndex] Created directory ./public
[Feed] Failed to fetch metadata from http://172.26.32.1/wordpress/graphql: Error: Unexpected token < in JSON at position 0
[SearchIndex] Successfully wrote file to public/wp-search.json
ApolloError: Unexpected token < in JSON at position 0
    at new ApolloError (/mnt/f/Projects/next-wordpress-starter/node_modules/@apollo/client/errors/errors.cjs.js:31:28)
    at /mnt/f/Projects/next-wordpress-starter/node_modules/@apollo/client/core/core.cjs.js:1457:19
    at /mnt/f/Projects/next-wordpress-starter/node_modules/@apollo/client/utilities/utilities.cjs.js:936:69
    at new Promise (<anonymous>)
    at Object.error (/mnt/f/Projects/next-wordpress-starter/node_modules/@apollo/client/utilities/utilities.cjs.js:936:21)
    at notifySubscription (/mnt/f/Projects/next-wordpress-starter/node_modules/zen-observable/lib/Observable.js:140:18)
    at onNotify (/mnt/f/Projects/next-wordpress-starter/node_modules/zen-observable/lib/Observable.js:179:3)
    at SubscriptionObserver.error (/mnt/f/Projects/next-wordpress-starter/node_modules/zen-observable/lib/Observable.js:240:7)
    at /mnt/f/Projects/next-wordpress-starter/node_modules/@apollo/client/utilities/utilities.cjs.js:924:68
    at Array.forEach (<anonymous>) {
  graphQLErrors: [],
  networkError: SyntaxError [ServerParseError]: Unexpected token < in JSON at position 0
      at JSON.parse (<anonymous>)
      at /mnt/f/Projects/next-wordpress-starter/node_modules/@apollo/client/link/http/http.cjs.js:18:25
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (internal/process/task_queues.js:97:5) {
    response: Response {
      size: 0,
      timeout: 0,
      [Symbol(Body internals)]: [Object],
      [Symbol(Response internals)]: [Object]
    },
    statusCode: 404,
    bodyText: '<!DOCTYPE html>\r\n' +
      '<html lang="en-US">\r\n' +
      '<head>\r\n' +
      '<meta charset="UTF-8">\r\n' +
      '<meta name="viewport" content="width=device-width, initial-scale=1">\r\n' +
      '<link rel="profile" href="https://gmpg.org/xfn/11">\r\n' +
      '\r\n' +
      '<title>Page not found &#8211; Plants Valley</title>\n' +
      "<link rel='dns-prefetch' href='//fonts.googleapis.com' />\n" +

Features

optional feature image for blog home and each post or page
code blocks
nav / menu links
pinned / featured post
pagination
tags / related posts / categories
date field (posted and last edited )
social preview image

Social Preview Images

Can we provide generated social preview images for posts without requiring an additional service?

Responsive Site Navigation

I'm happy with the "desktop" experience for larger devices, but I didn't put a ton of thought into how mobile should look.

We should figure out a good way to present the navigation for all device sizes.

Things to consider:

  • How does this look on smaller devices (mobile)
  • How does this look on larger devices (desktop)
  • Is a hamburger menu appropriate? maybe instead do a sideways scrolling menu like how Google does it on mobile?
    image
  • How would search look on mobile?

A solution should be proposed before this Issue gets picked up to get worked on, ideally with a design mockup of sorts (doesn't need to be perfect for the mockup)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.