Our boilerplate is designed to help us quickly start a React Single Page App (SPA) frontend with a robust PHP backend powered by WordPress and ACF Pro.
This project follows a philosophy of limiting plugin use to keep the administration clean and consistent for clients, while embracing automation with Webpack. This setup allows us to play with node modules in ESM format, React, SASS/SCSS, codes minification and images compression.
Note
Although our philosophy is to use as few plugins as possible, you are free to install whatever you like without limitations. However, keep in mind that the project only makes sense if you develop your own code around React, ACF, and certain Node modules, rather than relying on numerous different WordPress plugins.
- React
- Webpack
- JavaScript minification with
terser-webpack-plugin
- SCSS or SASS with
sass
andsass-loader
- Image Compression with
image-minimizer-webpack-plugin
, (supports GIF, JPG, PNG, and SVG; WEBP support is not implemented yet) - App routing with
react-router-dom
- Helmet for managing document head
src > front > js > components > Metas.jsx
- Font Awesome with individual imports
- GSAP and SmoothScroller
src > front > js > components > Scroller.jsx
- Page transitions animated with GSAP
src > front > js > components > PageTransition.jsx
- Preloader
src > front > js > addons > Loader.js
- Anchor scrolling by
PageTransition.jsx
- 404 handling
- NodeJS (tested with v20.15.0)
- ACF Pro License Key
- PHP Version 8.2 or higher
- A premium or commercial subscription to the GSAP Club (ensure to add the .npmrc file in the constructor directory to authenticate your account)
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
(not tested)
location / {
try_files $uri /index.html;
}
- Clone this repository using Git.
- Navigate to the
constructor
directory. - Install WordPress by running
npm run get:wp
. - Authenticate your GSAP account by adding the
.npmrc
file. - Install the Node modules with
npm i
. - In the
src > back
directory, duplicatewp-config-sample.php
towp-config.php
and set it up. - In
src > front > js > App.jsx
, change the URLs within the global variablewindow.SYSTEM
to reflect your current URL. - In the
src > front > template
directory, add your server configuration file, such as your.htaccess
for Apache. - For the first setup of your project, run
npm run watch:front:back
ornpm run build:front:back
inconstructor
directory. After your first setup, you can continue watching or rebuild only the front or back withnpm run watch:front
ornpm run build:back
. When you are ready for production and want to compress, replacewatch
withprod
in your command line. Check yourpackage.json
to see the available command lines. - Go to your WordPress admin panel to complete the setup (e.g., yourdomain.com/admin).
- Activate the Champ Gauche Core Theme, Champ Gauche Core Plugin, and the ACF Plugin.
- Remove other default plugins, themes, pages, and posts.
- Create your Home Page and set it as a static page (e.g., yourdomain.com/admin/wp-admin/options-reading.php).
- Set your permalink structure to something other than Plain (the default option).
- Start working!
-
If you're unfamiliar with the headless ecosystem, the principle is to have a frontend that is separate from the backend. The frontend communicates with the backend via fetch requests.
-
Each page, post, custom post type, and some users (if you have pages for authors, etc.) that compose your routes need to be associated with a Component. To achieve this, you must have a field for each admin element that requires a display to populate the component name, and you need to map your Component in the ecosystem.
-
Pages and posts already have the field to manage the component name. If you want this field for another custom post type, you can go to (yourdomain.com/admin/wp-admin/admin.php?page=site-settings) and select your CPT in the
Modules
tab, under theComponent
section. For additional requirements, such as fetching the field for users, you need to edit where the field can be displayed in therender.php
file of the Champ Gauche Core Plugin, around line 670. -
To map your component in the ecosystem, simply add it to the
componentMap
object inApp.jsx
.
Tip
If you have many posts, etc., and don't want to populate a Component Name for each, you can create a logic in your App.jsx
or use some ACF hooks to populate the field automatically.
-
Add the attribute
data-transition="true"
to your link element. Without this attribute, the transition will be direct. (Use the<Link>
component fromreact-router-dom
, not<a ...>
) -
If you want to use
<a ...>
or another custom HTML element, you need to add the class namegoto
with the attributedata-href=""
(this attribute should only be added if it's not a<a ...>
element; for<a ...>
, usehref
). If yourdata-href
(orhref
) attribute is an anchor, you need to include the path of your current page if you want to stay on the same page and scroll to the anchor (e.g., if your page is your home page, you should add/
with your anchor, like/#my-id
). You don't need to do this with<Link>
. -
If your element is an anchor, you can control the scroll behavior by adding
data-behavior="smooth|instant"
. If the GSAP SmoothScroller Plugin is active, the default issmooth
. Without the plugin, the default isinstant
. -
Check the component
PageTransition.jsx
to edit your transition settings.
To get the Ajax base path and REST API base path, use the global object:
window.SYSTEM = {
baseUrl: 'https://wpp.test/',
adminUrl: 'https://wpp.test/admin/',
ajaxPath: '/admin/wp-admin/admin-ajax.php',
restPath: '/admin/wp-json/'
};
-
In most cases, the
main.min.css
file is loaded before the HTML body. However, with large files or slow connections, this may not always happen. If you suspect that your CSS file might not load before the body, it is advisable to use inline styles within your<head>
element for the preloader. You can check ifwindow.SYSTEM.loaded.css
istrue
before closing your preloader animation. -
The preloader is the first child element of the body, ensuring it displays without delay. This is why you may need to use inline styles for your preloader to prevent Flash of Unstyled Content (FOUC).
-
The
main.min.js
file is loaded. -
Loader initialization occurs, which includes the preloader animation and media download functionality. If you want to preload your media, you will need to implement your own download logic. Put a look on the file
src > front > js > addons > Loader.js
. -
The application is mounted.
-
A waiting page is displayed to prevent errors while waiting for routes to load.
-
Once the routes are ready, Scroller Component, PageTransition Component and your current page Component is initialized.
-
The WordPress frontend (not the React frontend, but the admin frontend part) redirects to the wp-admin. You can delete the
template_redirect
action hook infunctions.php
if you don't want this behavior. -
When you have a media file that isn't imported by your main JS App files, Webpack doesn't know you are using it and won't compile it. You need to force the import by using the appropriate JS file as needed (e.g., if you are using an audio file, go to
src > front > medias > audios
and import your file from theaudios.js
file). -
Currently, the project is not set up for multisite.
-
Using the admin side is not required. If you want to use only the frontend React part, you can stop fetching pages in the
App.jsx
file. -
You need to root the
dist
directory in your virtual host or push files from this directory to your hosting. Thedist
directory is created when you install WordPress with theget:wp
command and is populated throughout your progress. -
Minification and compression are only done in production.
- The preloader is implemented using a
Promise
. You can determine when the promise is resolved and call your animation inside this method:window.loader.then()
.
Default: French
Translation: en_US, en_CA, en_GB, en_AU, en_NZ, en_ZA
2024-09-08
- Updated boilerplate from v3 to v4.
Warning
v4 is not a continuation of v3 but a breaking change with a new headless ecosystem. If you need v3, Download here