Faust.js Documentation
repository·canary·Indexed 23 days ago
https://github.com/wpengine/faustjsA toolkit for building Next.js applications for headless WordPress sites. Faust.js provides specialized tooling for data fetching, authentication, previews, and rendering strategies (SSR and SSG). The ecosystem includes @faustwp/core, @faustwp/blocks for Gutenberg block rendering, @faustwp/block-editor-utils for converting React components into blocks, and the @faustwp/cli for application management.
What's inside Faust.js
- Faust.js® is a toolkit designed to simplify the process of building headless WordPress applications using Next.js. It provides a modular set of tools that developers can pick and choose from, covering essential headless requirements such as data fetching and Block component rendering.
Introduction to Faust.js
canaryFaust.js is a toolkit designed for building Next.js applications for headless WordPress sites. It provides specialized tooling to address common challenges in headless WordPress development, specifically around:
- Data fetching
- Authentication
- Previews
- SSR (Server-Side Rendering) and SSG (Static Site Generation)
It aims to provide a high-quality experience for both developers and content publishers.
Overview of @faustwp/core
canaryFaust is a toolkit designed to streamline headless WordPress development. It aims to provide a developer and publisher experience that is as seamless as classic WordPress while leveraging the power of headless architectures.Use @faustwp/cli to manage Faust.js Next.js apps
canaryThe@faustwp/clipackage provides a command-line interface designed to help developers develop, build, and serve Next.js applications built using the Faust.js framework.How WordPress blocks and fragments work together
canaryWhen rendering blocks in Faust.js, there is a three-part relationship between the block components, the GraphQL fragments, and the rendering components:
- Block Mapping: You define a mapping of block names to React components in a central file (e.g.,
wp-blocks/index.js). This is passed toWordPressBlocksProvider. - Data Fetching (Fragments): Because blocks can have complex nested data, you must use the
.fragments.entryproperty of each block object in your GraphQL query to fetch the required fields. You also use.fragments.keyto spread the fragment into theeditorBlocksselection set so the data is available for the component. - Hierarchical Rendering: WordPress returns blocks as a flat list. To render nested blocks (like columns containing paragraphs), you must use
flatListToHierarchicalto reconstruct the tree structure before passing it toWordPressBlocksViewer.
- Block Mapping: You define a mapping of block names to React components in a central file (e.g.,
How the Faust Plugin System Filters work
canaryThe Faust Plugin System uses a filter pattern to allow developers to intercept and modify core system data.
Every filter callback accepts two parameters:
- The filtered data: The current value of the data being processed (e.g., a string array, a configuration object, or a URL).
- The context object: An object containing metadata relevant to the specific filter, which can be used to make informed modifications to the data.
To use these filters, you typically use the
addFiltermethod within a plugin'sapplymethod.Use the Faust WP Template System
canaryThe WP Template Hierarchy allows you to define individual components for specific WordPress templates. These components are rendered automatically based on the route being visited. Common templates include:
front-page.js: For the site's front page.single.js: For single posts.page.js: For static pages.category.js: For category archive index pages.
You can check the browser dev console when visiting a page to see which templates Faust might match for that route.
How the Seed Query works in faust.js
canaryThe Seed Query is an initial GraphQL request sent to WordPress to determine the type and basic properties of the content requested via a URI. It does not fetch the full content itself; instead, it provides the metadata necessary for faust.js to determine which template to render.
Workflow
- User Request: A user requests a page with a specific URI (e.g.,
/sample-page/). - Seed Query: faust.js sends the
SEED_QUERYto WordPress. - Metadata Return: The query returns the
__typenameand essential properties (liketemplateNameorcontentType). - Template Specific Query: Based on the seed query results, faust.js sends a secondary, more detailed query to retrieve the full content required for that specific template.
- Rendering: The determined template is rendered with the retrieved content.
Implementation Details
The seed query utilizes WpGraphQL's
nodeByUrifor standard requests andcontentNodefor preview requests ($asPreview: true). It uses several fragments to ensure the response structure adapts to the content type (e.g.,Post,Page,User,TermNode,MediaItem).// Example of the SEED_QUERY structure used by faust.js export const SEED_QUERY = gql` query GetSeedNode( $id: ID! = 0 $uri: String! = "" $asPreview: Boolean = false ) { ... on RootQuery @skip(if: $asPreview) { nodeByUri(uri: $uri) { __typename ...GetNode } } ... on RootQuery @include(if: $asPreview) { contentNode(id: $id, idType: DATABASE_ID, asPreview: true) { __typename ...GetNode } } };- User Request: A user requests a page with a specific URI (e.g.,
Understand theme.json transformations in BlocksTheme
canaryWhen
fromThemeJsonprocesses atheme.jsonfile, it transforms nestedsettingsinto top-level properties on theBlocksThemeobject for easier access.Property Mappings
theme.json Path BlocksTheme Property settings.color.palettetheme.palettesettings.spacing.spacingSizestheme.spacingSizessettings.typography.fontFamiliestheme.fontFamiliessettings.typography.fontSizestheme.fontSizessettings.layouttheme.layout(copied as is)Example Transformation
Input
theme.jsonsnippet:{ "settings": { "color": { "palette": [ { "color": "#ffffff", "name": "Base", "slug": "base" }, { "color": "#000000", "name": "Contrast", "slug": "contrast" } ] } } }Resulting
BlocksThemeobject:theme.palette = { "base": "#ffffff", "contrast": "#000000" }Understand the purpose of styles/wpcore
canaryThe
styles/wpcoredirectory contains stylesheets ported directly from WordPress core. These styles are intended to provide the standard WordPress look and feel for specific elements (like the admin bar and dashicons) within the Faust.js environment.Important: These stylesheets should remain untouched to ensure compatibility with the original WordPress core design and functionality.
How redirect-based authentication works in Faust.js
canaryThe default authentication strategy in the Faust.js toolkit is Redirect-based authentication. This is ideal for use cases where authenticated users are admins, editors, or staff (e.g., for previewing posts/pages) and do not require a custom "white label" login experience.
The Flow:
- The user attempts to access a protected route in the Next.js application.
- The application redirects the user to WordPress to authenticate.
- After successful authentication in WordPress, the user is redirected back to the Next.js application with an authorization code.
- The toolkit uses this code to request a refresh and access token, completing the login process.
Understand Apollo Client in Faust.js
canaryFaust.js uses
@apollo/client@3to perform GraphQL operations against your WordPress backend. To work effectively with Faust.js, you should be familiar with the following core Apollo Client concepts:- Queries: Used to retrieve data from your WordPress site.
- Fragments: Used to modularize queries, making them more maintainable and reusable.
- Mutations: Used to update or change data in your WordPress backend.
- Apollo Client Cache: Used to cache responses to minimize network usage and improve performance.