Skip to content

This is a personal site and blog. Built on GatsbyJS, TS and a headless WordPress CMS.

License

Notifications You must be signed in to change notification settings

simulieren/simonhalimonov.com

Repository files navigation

type-coverage

This is a personal site and blog. Built on GatsbyJS, TS and a headless WordPress CMS.

This project is open source to educate and simplify the development process of future sites that use GatsbyJS and WordPress. The goal is to fully support these key features:

  • πŸ”₯ Fast load speeds. Under 1s on mobile and desktop.
  • πŸ’― High Lighthouse scores. Almost 100 on mobile and desktop.
  • πŸ“ Minimal and precise layout for easy customizations
  • 🌐 Multilingual support via i18n (Work in progress)
  • πŸ“ Editor-friendly setup with accurate previews
  • πŸ€– SEO optimised site
  • βœ… Accessibility optimised site (Work in progress)
  • πŸ”ƒ Programmatic page creation
  • ⭐️ Gutenberg support (Work in progress)
  • πŸ‘ WordPress theme optimised for headless
  • πŸ’― High TypeScript coverage and high type safety

Note: Some features are still work in progress.

Setup

Configure all environment variables and have your WordPress installation running ().

  1. Install all dependencies npm i or yarn
  2. Add environment variables
  3. Run development environment npm start or yarn start

Using the environment variables

GATSBY_WORDPRESS_URL_PATH the domain or IP to the WordPress installation

GATSBY_WORDPRESS_URL_PROTOCOL the HTTP protocol used

GATSBY_INSTAGRAM_SOURCE the name of the instagram account

GATSBY_TWITTER_SOURCE the name of the twitter account

GATSBY_GOOGLE_ANALYTICS the Google Analytics tracking id

WordPress Setup

You can read about how to setup a free WordPress site on GCP in my post.

  • Create a single post and page
    • Post
      • Add a tag (required)
      • Add featured image (required)
    • Page
      • Add featured image (required)
  • Settings
    • Use permalinks with post name (required)
    • Set a page as the front page (required)
    • Create a menu (required)
      • The first menu item will be used for the logo
      • All other menu items will be generated dynamically
  • Install the theme (required)
    • It will do following things:
      • It modifies REST API
      • It disables the WP frontend
      • It reroutes all pages to the REST API
  • Install plugins and activate them (required)
    • Polylang (required)
      • Configure a main language that reflect the gatsby-config.js
      • This site uses en_US as the default language
      • The secondary language is de_DE
      • Configure a menu for both languages
    • Polylang REST API (required)
    • WP REST API MENU (required)
    • WP Webhooks (optional)
      • Trigger a webhook on Vercel or Netlify for a redeploy
    • ACF (optional)

Setting up a cheap WordPress CMS

With a cloud provider, you can easily host a WordPress CMS installation for free with this setup. You can read how to setup a WordPress CMS on GCP here.

I recommend AWS or GCP, as they have very good free tiers. The lowest virtual machine instance should be enough. They also have 1-click install setups.


Features

i18n support

This project supports Polylang via REST API. Note: Full i18n support is a work in progress

Static, programatic and dynamic content

Static content is written directly in the codebase. Add pages in /src/pages/ to create new static content.

Programatic content is created during build time. This uses GatsbyJS source plugins to fetch data and translating that data into static HTML files.

Dynamic content is created on the client side. In GatsbyJS you can use a custom router to create dynamic pages. The preview feature uses this to show a preview of the revision of post/page.

HTML Parsing to custom React components

Content from WordPress is being parsed, converted to JSX and then rendered to HTML. This enables full control in the frontend to create rich interactive experiences with all features of React. Gutenberg Blocks translate into custom React components in the HMTL.tsx file.

Posts, Tags, Categories and Pages

Use posts, tags, categories and pages in WordPress to build your site.

Full TS support in Gatsby

This project aims to have a high type safety to reduce possible issues. Currently it has 92.43% type coverage. You can check the type coverage by running npm types or yarn types. This was possible due to this GitHub Gist and the credits mentioned in there.

Simple WordPress Template support

Use the default template attribute from wordpress to choose the file that will be used for the page template.

Page templates can be found in src/templates/... and the mapping happen in src/gatsby/node/index.ts.

Preview pages in the Gatsby frontend

Previews are being rendered in the Gatsby frontend by fetching the data from a modified WordPress REST API. The modified REST API exposes the latest revisions of the post. This means that you don't have to deal with nonce and other authentication strategies. By default all revisions are only available with an authenticated REST API. I did this, because it was a pain in the ass to get it properly working.

Custom WordPress Theme for a headless installation

This project uses a custom theme to deactivate most of the regular frontend of WordPress.


Folder structure

./src
β”œβ”€β”€ components # React components folder
β”‚   β”œβ”€β”€ AnimateWords
β”‚   β”‚   └── AnimateWords.tsx
β”‚   β”œβ”€β”€ Footer
β”‚   β”‚   └── index.tsx
β”‚   β”œβ”€β”€ GridHelper # Grid overlay
β”‚   β”‚   └── GridHelper.tsx
β”‚   β”œβ”€β”€ HTML
β”‚   β”‚   └── HTML.tsx
β”‚   β”œβ”€β”€ Header
β”‚   β”‚   └── index.tsx
β”‚   β”œβ”€β”€ Image
β”‚   β”‚   └── Image.tsx
β”‚   β”œβ”€β”€ Instagram
β”‚   β”‚   └── index.tsx
β”‚   β”œβ”€β”€ InviewMotion
β”‚   β”‚   └── InviewMotion.tsx
β”‚   β”œβ”€β”€ Layout # Common layout components
β”‚   β”‚   β”œβ”€β”€ PageTitleAnimation.tsx
β”‚   β”‚   β”œβ”€β”€ Section.tsx
β”‚   β”‚   └── index.tsx # Global layout wrapper
β”‚   β”œβ”€β”€ Layouts # Shared grid layouts
β”‚   β”‚   └── ContentWithSidebar.tsx
β”‚   β”œβ”€β”€ Logo
β”‚   β”‚   └── Logo.tsx
β”‚   β”œβ”€β”€ SEO # Component used for SEO
β”‚   β”‚   └── index.tsx
β”‚   β”œβ”€β”€ SocialSidebar
β”‚   β”‚   └── SocialSidebar.tsx
β”‚   β”œβ”€β”€ Transition # Component used for page transitions
β”‚   β”‚   └── Transition.tsx
β”‚   β”œβ”€β”€ Twitter
β”‚   β”‚   └── index.tsx
β”‚   β”œβ”€β”€ Typography # Folder for typography components
β”‚   β”‚   β”œβ”€β”€ H.tsx
β”‚   β”‚   β”œβ”€β”€ P.tsx
β”‚   β”‚   β”œβ”€β”€ S.tsx
β”‚   β”‚   β”œβ”€β”€ XL.tsx
β”‚   β”‚   β”œβ”€β”€ XS.tsx
β”‚   β”‚   └── index.tsx # Barrel file
β”‚   └── WorkSlider
β”‚       └── WorkSlider.tsx
β”œβ”€β”€ contracts # TypeScript definitions
β”‚   β”œβ”€β”€ page.ts
β”‚   β”œβ”€β”€ post.ts
β”‚   β”œβ”€β”€ preview.ts
β”‚   β”œβ”€β”€ templates.ts
β”‚   └── util.ts
β”œβ”€β”€ gatsby # Internal GatsbyJS code
β”‚   β”œβ”€β”€ config
β”‚   β”‚   └── index.ts
β”‚   β”œβ”€β”€ node
β”‚   β”‚   └── index.ts
β”‚   β”œβ”€β”€ shouldUpdateScroll.js
β”‚   └── wrapPageElement.js
β”œβ”€β”€ gatsby-plugin-theme-ui
β”‚   └── index.js # Theme settings
β”œβ”€β”€ images
β”‚   β”œβ”€β”€ leaf-01.png # folder
β”‚   ...
β”œβ”€β”€ pages # Static pages
β”‚   β”œβ”€β”€ 404.tsx # Custom 404 page
β”‚   β”œβ”€β”€ kitchensink.tsx # Page to test WordPress Gutenberg Blocks
β”‚   └── preview.tsx # Page used for previews
β”œβ”€β”€ templates
β”‚   β”œβ”€β”€ Blog # All post related page templates
β”‚   β”‚   β”œβ”€β”€ BlogCategoryPosts.tsx
β”‚   β”‚   β”œβ”€β”€ BlogPost.tsx
β”‚   β”‚   β”œβ”€β”€ BlogPostTeaser.tsx
β”‚   β”‚   β”œβ”€β”€ BlogPosts.tsx
β”‚   β”‚   └── BlogTagPosts.tsx
β”‚   └── Page # All custom page templates
β”‚       β”œβ”€β”€ CoverPage.tsx
β”‚       β”œβ”€β”€ DefaultPage.tsx
β”‚       β”œβ”€β”€ FullWidthPage.tsx
β”‚       β”œβ”€β”€ HomePage.tsx # Page used for the homepage
β”‚       β”œβ”€β”€ WorkIndex.tsx # Page used to display all work projects
β”‚       └── WorkIndexTeaser.tsx # Component used in WorkIndex page
└── utils # Utility function folder
    β”œβ”€β”€ index.ts
    └── useInterval.tsx

Missing Features and To-Do's

  • Add WordPress Theme to repository
    • Add instructions for local development
    • Add instructions for deployment
  • Add i18n support
    • Static pages
    • Footer
    • Overview pages
  • Add draft preview support
  • Add support for more essential Gutenberg Blocks

Credits

This is based on a version of Gatsby Wordpress Typescript Blog Boilerplate by sagar7993. It is heavily modified from the original.

The WordPress theme is based on Postlight's Headless WordPress + React Starter Kit by Postlight. It was modified by including revisions in the REST API response and adding some page templates.

GatsbyJS + TypeScript was possible because of this GitHub Gist and the credits mentioned in there.

About

This is a personal site and blog. Built on GatsbyJS, TS and a headless WordPress CMS.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published