This is a starter template created by Wunder for a decoupled website using the open-source Next.js for Drupal project by Chapter Three and contributors.
๐ช Check out the running demo at https://next-drupal-starterkit.dev.wdr.io !
The aims of this template are:
- automating local environment set up as much as possible, lowering the effort needed to get the decoupled system working
- presenting simple solutions for the most common feature requests for a Drupal site
- focusing on the multilingual aspect which presents interesting challenges, and is a very common requirement in our projects.
Setting up Next.js for Drupal normally requires various steps, in this template we have automated them using a combination of environment variables and the upcoming Distributions and Recipes initiative on the Drupal side.
The template includes all you need to have a working multi-language decoupled Drupal and Next.js site up and running in seconds, complete with demo content, an Elasticsearch-powered search interface, and more. Check the what's included section
This example is meant to be used together with the Silta hosting system by Wunder, but it can be used with any hosting system.
This starterkit can be used either with Lando or with DDEV. The only requirement is to have either one of those installed.
The minimum version of lando required is 3.21.
Check the
version
property in the.lando.yml
file to see which version of Lando is currently supported.
Instead of running npm operations in your host machine, this template requires you to use npm inside Lando or DDEV: this ensures the same node version is used by all developers participating in the project, and also that the node process has the right environment variables to connect to the backend without the need of additional configuration steps.
Just prefix all npm operations with lando
or ddev
.
So instead of npm install
, run lando npm install
or ddev npm install
, instead of npm run dev
run lando npm run dev
or ddev npm run dev
, etc.
โ ๏ธ โ ๏ธ For DDEV, when using npm commands you have to make sure that you are in thenext
directory.
If you have closed the terminal window where you were running the server with lando npm start
or lando npm run dev
, and you want to stop the running npm operation, you can use the specially created lando npm-stop
command that will log into the node container and kill all node processes there.
Follow this guide to get the backend and frontend up and running. You can either do it all in one go, or step by step to understand better what's going on.
- Clone this repository
- Choose which local environment you want to use: Lando or DDEV.
- Run the setup script corresponding to your chosen local environment:
./setup-lando.sh
or
./setup-ddev.sh
The script will execute a series of commands in sequence. If an error occurs, you can run the script again, and it will pick up where it left off.
If the script has failed on some step, and instead of continuing you want to start from scratch, you can run the script with the -c
flag:
./setup-[lando/ddev].sh -c
NOTE: the script will install the site from scratch. Export your database if you have started working with the template, and you have something valuable in it. :)
After the setup is complete, you can access the site at the following URLs:
Lando has two separate containers for the backend and frontend, so the URLs are different:
Backend | Frontend |
---|---|
https://next-drupal-starterkit.lndo.site/ | https://frontend.lndo.site/ |
You can get a more detailed list of all the services and their urls with the command:
lando info
DDEV has a single container for both the backend and frontend, so the URLs differ only by the port:
Backend | Frontend |
---|---|
https://next-drupal-starterkit.ddev.site | https://next-drupal-starterkit.ddev.site:3000 |
You can get a more detailed list of all the services and their urls with the command:
ddev describe
We try to add to the template what we think are the most commonly requested features in website projects. Most of these are based on the features provided by Next.js for Drupal, but we have paid special attention to making these work in a multilanguage setup.
The template is set up to allow editors to use Preview mode. Visit the node page on the Drupal side while the frontend is running to see a preview of the page.
The template includes On demand revalidation, when saving or editing a piece of content the corresponding page in the frontend will be recreated. Because the frontpage in this example site includes a list of articles, the frontpage paths have been manually added to the configuration for the article content type.
The template includes basic metatag support. Default metatag values are added to the frontend site's translation files, and content-based metatags are added to the Drupal backend using the Metatag module. The Next.js site will then create the metatags using a combination of these two data sources.
The site is set up to work with Elasticsearch to provide a complete search experience.
The Lando setup includes spinning up an Elasticsearch instance with the required plugins. The content normalization and index handling is managed via the custom wunder_search
module, which in turn makes use of the Elasticsearch helper Drupal contrib module.
On the frontend side, the search user uses the Elastic UI library.
The frontend site queries Elasticsearch via a simple proxy controller in Drupal, also provided by the included wunder_search
custom Drupal module.
The lando setup also includes ElasticVue, a tool to help you manage your Elasticsearch indexes.
The template includes a Drupal migration to populate the site with a set of translated demo content and some test users. This includes paragraphs, images, videos, etc. Most of the content has been generated by AI.
This template is set up to use three languages: English (default), Finnish and Swedish. These languages are added in Drupal using the usual translation modules, and to the frontend using the next-i18next npm package. The indexing in Elasticsearch takes into consideration the language of the content for analysis. The interface is translated. (Note: translations have mostly been done with Google Translate, so don't expect perfect Finnish or Swedish... or English for that matter. ๐ )
The Drupal setup includes the popular Webform module. The frontpage on the frontend displays a form that will post data back to Drupal's webform, with basic validation and a feedback message. We decided to implement a static form, in the sense that the "hardcoded" fields in the frontend need to match the fields in the webform in the backend. Creating a dynamic system of forms that automatically match what's coming from the user-defined webforms in Drupal is out of scope for this template.
The frontpage
and page
content types are configured to use the popular Paragraphs drupal module. The setup includes basic paragraph types to add images, videos, text, and also a nested paragraph type to demonstrate how to handle this in backend and frontend.
If the backend is not available momentarily, the frontend will try again to call it before returning an error.
The template includes the setup to allow users to log into the Drupal backend from the Next.js frontend, using Next-Auth.
- As an example, only registered users are allowed to post to the drupal
contact
webform, and parts of the interface in the frontend are available only for logged-in users. - Some test users are imported as part of the content migration (check the `users.csv' file for the credentials).
- New users can be created on the frontend using a simple registration form. Drupal will assign them the correct role, and will send them an email with the link to set their password.
The frontend uses TypeScript to provide type safety.
TypeScript is setup quite loosely by default to minimise friction and make it accessible to developers who are not familiar with it. It is recommended to increase type safety by enabling some of the disabled rules in next/eslint.json
.
The project uses GraphQL to fetch data from the backend. The queries are defined in the next/lib/graphql
directory. The queries are typed using the graphql-codegen
package, which generates TypeScript types from the queries. The types are then used to type the data fetched from the backend.
When adding or modifying queries and fragments, the codegen script needs to be run to generate the corresponding types from the schema. Though you can always run lando npm run graphql-codegen
or ddev npm run graphql-codegen
yourself if needed, you shouldn't normally need to: lando npm run build
or ddev npm run build
will run the codegen before the build, and lando npm run dev
or ddev npm run dev
will start the codegen in watch mode alongside starting Next.js in development mode. The output of the codegen is gitignored, as the same step will be run on the CI server.
Note that when there are changes on the GraphQL server schema itself, you will need to stop and start the command again to fetch the new schema definition (it will keep watching your changed files, but will only re-fetch the schema from the server when the codegen command first runs). Also, you might need to run lando drush cr
or ddev drush cr
to clear the Drupal cache.
The environment variables used by the frontend are also checked for type safety. If used correctly, a Zod error will prevent the frontend from building if the environment variables are not set according to the schema defined in next/env.ts
. To add a new environment variable:
- Add it to
.lando.yml
, under services > node > overrides > environment. or to.ddev/config.yaml
for DDEV. - Add it to
next/env.ts
. Note that it must be added twice there - once under server/client to define its schema, and once underruntimeEnv
to read the actual value. - Import it in the file where it's used with
import { env } from "@/env";
and use it likeenv.MY_ENV_VAR
. At this point, your environment variable should be working locally. - To ensure it also works in CircleCI and Silta, also add it to
.circleci/config.yml
andsilta-next.yml
.
The Next.js frontend will query the Drupal backend to generate a /sitemap.xml
path that can be submitted to search engines.
The project is set up to use Redis if available to cache the responses from the backend. Both the ddev and lando setup include redis by default.
The connection between next.js and redis is handled by the @neshca/cache-handler package. We added custom logic to our cache handler
to handle prepopulating the cache from builds for ISR. Checl the cache-handler.mjs
file in the next
directory for more information.
You can connect to Redis and interact with it using the redis cli in the local environment by running the following command:
If you are using Lando:
lando redis-cli
If you are using DDEV:
ddev redis
The template includes example tests to be run with Cypress. The Lando setup includes a headless browser and Cypress, so you can run the tests locally without the need to install anything else, but it won't be able to use the visual Cypress application. See below for more details.
To run the Cypress tests inside Lando:
- make sure the backend is running
- run
lando npm run build
to build the frontend - run
lando npm run start
to start serving the frontend - open another terminal and run
lando npm run cypress:run
to start the Cypress test runner
A video of the run will be recorded, and it will be available at next/cypress/videos
.
If you want to run the visual Cypress application, you will need to run cypress outside of Lando, on your host computer. For this to work:
- ensure you are using the correct node version, matching what we use inside Lando (see the
.lando.yml
file for details) - ensure your machine has the correct dependencies installed (see the Cypress docs for details)
- check which version of Cypress is specified in
next/package.json
and install the same version on your host computer globally withnpm install -g cypress@<version>
- while in the
/next
directory, runnpm run cypress:open
(notice that there's nolando
at the beginning of the command).
You can then run your tests inside the Cypress application.
The ui/
directory contains some reusable UI components that are used in the frontend. These components are based on the Wunder Component Library, which is a collection of reusable UI components designed to be used as a shared base for many projects. The components are meant to be used as a starting point, and should be modified, added and removed as required to fit the needs of the project.