Environment Variables

Configuration variables for Next.js and Drupal.


.env.local

# Required
NEXT_PUBLIC_DRUPAL_BASE_URL=http://localhost:8080
NEXT_IMAGE_DOMAIN=localhost
# Required for Preview Mode
DRUPAL_PREVIEW_SECRET=
# Required for On-demand Revalidation
DRUPAL_REVALIDATE_SECRET=
# Authentication (Bearer)
DRUPAL_CLIENT_ID=
DRUPAL_CLIENT_SECRET=
# Authentication (Basic)
DRUPAL_USERNAME=
DRUPAL_PASSWORD=
# Optional
DRUPAL_SITE_ID=
DRUPAL_FRONT_PAGE=

Required

These environment variables are required for initializing a new DrupalClient.

NameDescription
NEXT_PUBLIC_DRUPAL_BASE_URLThe base url for your Drupal site. Example: https://drupal.org
NEXT_IMAGE_DOMAINThe domain name for next/image. Example: drupal.org
const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL)

Preview Mode

Environment variables required for preview mode.

NameDescription
DRUPAL_PREVIEW_SECRETThe secret for preview mode. Example: 6SZy9xkdtR
const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL, {
previewSecret: process.env.DRUPAL_PREVIEW_SECRET,
})

On-demand Revalidation

Environment variables required for on-demand revalidation.

NameDescription
DRUPAL_REVALIDATE_SECRETThe secret for on-demand revalidation. Example: U2Y5bbkKJ08Ua8F

pages/api/revalidate.ts

export default async function handler(request, response) {
const secret = request.query.secret as string
// Validate secret.
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
return response.status(401).json({ message: "Invalid secret." })
}
}

Authentication (Bearer)

NameDescription
DRUPAL_CLIENT_IDThe OAuth client id. Example: a53b1d17-6b23-478d-8649-9aee63974c80
DRUPAL_CLIENT_SECRETThe OAuth client secret. Example: 3#9h$2DU#8qKb6&
const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL, {
auth: {
clientId: process.env.DRUPAL_CLIENT_ID,
clientSecret: process.env.DRUPAL_CLIENT_SECRET,
},
})

Authentication (Basic)

NameDescription
DRUPAL_USERNAMEThe Drupal username. Example: admin
DRUPAL_PASSWORDThe Drupal password. Example: password
const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL, {
auth: {
username: process.env.DRUPAL_USERNAME,
password: process.env.DRUPAL_PASSWORD,
},
})

Optional

These environment variables are optional and not required as of next-drupal 1.4.0.

NameDescription
DRUPAL_SITE_IDThe site id of the Next.js site on Drupal. Example: marketing_site. See the Filter by site guide for usage example.
DRUPAL_FRONT_PAGEThe path to the front page: Example: /front

DRUPAL_SITE_ID

// Fetch all articles with DRUPAL_SITE_ID in field_sites.
const nodes = await drupal.getResourceCollection<DrupalNode[]>(
"node--article",
{
params: {
filter: {
"field_sites.meta.drupal_internal__target_id":
process.env.DRUPAL_SITE_ID,
},
},
}
)

DRUPAL_FRONT_PAGE

const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL, {
frontPage: process.env.DRUPAL_FRONT_PAGE,
})